use std::{ffi::c_int, result};
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)]
pub enum Error {
#[error("key/data pair already exists")]
KeyExist,
#[error("no matching key/data pair found")]
NotFound,
#[error("the cursor is already at the end of data")]
NoData,
#[error("requested page not found")]
PageNotFound,
#[error("database is corrupted")]
Corrupted,
#[error("fatal environment error")]
Panic,
#[error("DB version mismatch")]
VersionMismatch,
#[error("file is not an MDBX file")]
Invalid,
#[error("environment map size limit reached")]
MapFull,
#[error("too many DBI-handles (maxdbs reached)")]
DbsFull,
#[error("too many readers (maxreaders reached)")]
ReadersFull,
#[error("transaction has too many dirty pages (i.e., the transaction is too big)")]
TxnFull,
#[error("cursor stack limit reached")]
CursorFull,
#[error("page has no more space")]
PageFull,
#[error("database engine was unable to extend mapping")]
UnableExtendMapSize,
#[error("environment or database is not compatible with the requested operation or flags")]
Incompatible,
#[error("invalid reuse of reader locktable slot")]
BadRslot,
#[error("transaction is not valid for requested operation")]
BadTxn,
#[error("invalid size or alignment of key or data for the target database")]
BadValSize,
#[error("the specified DBI-handle is invalid")]
BadDbi,
#[error("unexpected internal error")]
Problem,
#[error("another write transaction is running")]
Busy,
#[error("the specified key has more than one associated value")]
Multival,
#[error("wrong signature of a runtime object(s)")]
BadSignature,
#[error("database should be recovered, but cannot be done automatically since it's in read-only mode")]
WannaRecovery,
#[error("the given key value is mismatched to the current cursor position")]
KeyMismatch,
#[error("invalid parameter specified")]
DecodeError,
#[error("the environment opened in read-only")]
Access,
#[error("database is too large for the current system")]
TooLarge,
#[error("invalid parameter specified or active write transaction")]
DecodeErrorLenDiff,
#[error("nested transactions are not supported with WriteMap")]
NestedTransactionsUnsupportedWithWriteMap,
#[error("write transactions are not supported in read-only mode")]
WriteTransactionUnsupportedInReadOnlyMode,
#[error("read transaction has been timed out")]
ReadTransactionTimeout,
#[error("permission denied to setup database")]
Permission,
#[error("unknown error code: {0}")]
Other(i32),
}
impl Error {
pub const fn from_err_code(err_code: c_int) -> Self {
match err_code {
ffi::MDBX_KEYEXIST => Self::KeyExist,
ffi::MDBX_NOTFOUND => Self::NotFound,
ffi::MDBX_ENODATA => Self::NoData,
ffi::MDBX_PAGE_NOTFOUND => Self::PageNotFound,
ffi::MDBX_CORRUPTED => Self::Corrupted,
ffi::MDBX_PANIC => Self::Panic,
ffi::MDBX_VERSION_MISMATCH => Self::VersionMismatch,
ffi::MDBX_INVALID => Self::Invalid,
ffi::MDBX_MAP_FULL => Self::MapFull,
ffi::MDBX_DBS_FULL => Self::DbsFull,
ffi::MDBX_READERS_FULL => Self::ReadersFull,
ffi::MDBX_TXN_FULL => Self::TxnFull,
ffi::MDBX_CURSOR_FULL => Self::CursorFull,
ffi::MDBX_PAGE_FULL => Self::PageFull,
ffi::MDBX_UNABLE_EXTEND_MAPSIZE => Self::UnableExtendMapSize,
ffi::MDBX_INCOMPATIBLE => Self::Incompatible,
ffi::MDBX_BAD_RSLOT => Self::BadRslot,
ffi::MDBX_BAD_TXN => Self::BadTxn,
ffi::MDBX_BAD_VALSIZE => Self::BadValSize,
ffi::MDBX_BAD_DBI => Self::BadDbi,
ffi::MDBX_PROBLEM => Self::Problem,
ffi::MDBX_BUSY => Self::Busy,
ffi::MDBX_EMULTIVAL => Self::Multival,
ffi::MDBX_WANNA_RECOVERY => Self::WannaRecovery,
ffi::MDBX_EKEYMISMATCH => Self::KeyMismatch,
ffi::MDBX_EINVAL => Self::DecodeError,
ffi::MDBX_EACCESS => Self::Access,
ffi::MDBX_TOO_LARGE => Self::TooLarge,
ffi::MDBX_EBADSIGN => Self::BadSignature,
ffi::MDBX_EPERM => Self::Permission,
other => Self::Other(other),
}
}
pub const fn to_err_code(&self) -> i32 {
match self {
Self::KeyExist => ffi::MDBX_KEYEXIST,
Self::NotFound => ffi::MDBX_NOTFOUND,
Self::NoData => ffi::MDBX_ENODATA,
Self::PageNotFound => ffi::MDBX_PAGE_NOTFOUND,
Self::Corrupted => ffi::MDBX_CORRUPTED,
Self::Panic => ffi::MDBX_PANIC,
Self::VersionMismatch => ffi::MDBX_VERSION_MISMATCH,
Self::Invalid => ffi::MDBX_INVALID,
Self::MapFull => ffi::MDBX_MAP_FULL,
Self::DbsFull => ffi::MDBX_DBS_FULL,
Self::ReadersFull => ffi::MDBX_READERS_FULL,
Self::TxnFull => ffi::MDBX_TXN_FULL,
Self::CursorFull => ffi::MDBX_CURSOR_FULL,
Self::PageFull => ffi::MDBX_PAGE_FULL,
Self::UnableExtendMapSize => ffi::MDBX_UNABLE_EXTEND_MAPSIZE,
Self::Incompatible => ffi::MDBX_INCOMPATIBLE,
Self::BadRslot => ffi::MDBX_BAD_RSLOT,
Self::BadTxn => ffi::MDBX_BAD_TXN,
Self::BadValSize => ffi::MDBX_BAD_VALSIZE,
Self::BadDbi => ffi::MDBX_BAD_DBI,
Self::Problem => ffi::MDBX_PROBLEM,
Self::Busy => ffi::MDBX_BUSY,
Self::Multival => ffi::MDBX_EMULTIVAL,
Self::WannaRecovery => ffi::MDBX_WANNA_RECOVERY,
Self::KeyMismatch => ffi::MDBX_EKEYMISMATCH,
Self::DecodeErrorLenDiff | Self::DecodeError => ffi::MDBX_EINVAL,
Self::TooLarge => ffi::MDBX_TOO_LARGE,
Self::BadSignature => ffi::MDBX_EBADSIGN,
Self::Access |
Self::WriteTransactionUnsupportedInReadOnlyMode |
Self::NestedTransactionsUnsupportedWithWriteMap => ffi::MDBX_EACCESS,
Self::ReadTransactionTimeout => -96000, Self::Permission => ffi::MDBX_EPERM,
Self::Other(err_code) => *err_code,
}
}
}
impl From<Error> for i32 {
fn from(value: Error) -> Self {
value.to_err_code()
}
}
#[inline]
pub(crate) const fn mdbx_result(err_code: c_int) -> Result<bool> {
match err_code {
ffi::MDBX_SUCCESS => Ok(false),
ffi::MDBX_RESULT_TRUE => Ok(true),
other => Err(Error::from_err_code(other)),
}
}
#[macro_export]
macro_rules! mdbx_try_optional {
($expr:expr) => {{
match $expr {
Err(Error::NotFound | Error::NoData) => return Ok(None),
Err(e) => return Err(e),
Ok(v) => v,
}
}};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_description() {
assert_eq!("the environment opened in read-only", Error::from_err_code(13).to_string());
assert_eq!("file is not an MDBX file", Error::Invalid.to_string());
}
#[test]
fn test_conversion() {
assert_eq!(Error::from_err_code(ffi::MDBX_KEYEXIST), Error::KeyExist);
}
}