use alloc::{
boxed::Box,
format,
string::{String, ToString},
vec::Vec,
};
use core::{
fmt::{Debug, Display},
str::FromStr,
};
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum DatabaseError {
#[error("failed to open the database: {_0}")]
Open(DatabaseErrorInfo),
#[error("failed to create a table: {_0}")]
CreateTable(DatabaseErrorInfo),
#[error(transparent)]
Write(Box<DatabaseWriteError>),
#[error("failed to read a value from a database table: {_0}")]
Read(DatabaseErrorInfo),
#[error("database delete error code: {_0}")]
Delete(DatabaseErrorInfo),
#[error("failed to commit transaction changes: {_0}")]
Commit(DatabaseErrorInfo),
#[error("failed to initialize a transaction: {_0}")]
InitTx(DatabaseErrorInfo),
#[error("failed to initialize a cursor: {_0}")]
InitCursor(DatabaseErrorInfo),
#[error("failed to decode a key from a table")]
Decode,
#[error("failed to get stats: {_0}")]
Stats(DatabaseErrorInfo),
#[error("log level {_0:?} is not available")]
LogLevelUnavailable(LogLevel),
#[error("{_0}")]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, derive_more::Display)]
#[display("{message} ({code})")]
pub struct DatabaseErrorInfo {
pub message: Box<str>,
pub code: i32,
}
impl<E> From<E> for DatabaseErrorInfo
where
E: Display + Into<i32>,
{
#[inline]
fn from(error: E) -> Self {
Self { message: error.to_string().into(), code: error.into() }
}
}
impl From<DatabaseWriteError> for DatabaseError {
#[inline]
fn from(error: DatabaseWriteError) -> Self {
Self::Write(Box::new(error))
}
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("write operation {:?} failed for key \"{}\" in table {}: {}",
self.operation,
alloy_primitives::hex::encode(&self.key),
self.table_name,
self.info)]
pub struct DatabaseWriteError {
pub info: DatabaseErrorInfo,
pub operation: DatabaseWriteOperation,
pub table_name: &'static str,
pub key: Vec<u8>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DatabaseWriteOperation {
CursorAppend,
CursorUpsert,
CursorInsert,
CursorAppendDup,
Put,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum LogLevel {
Fatal,
Error,
Warn,
Notice,
Verbose,
Debug,
Trace,
Extra,
}
impl LogLevel {
pub const fn value_variants() -> &'static [Self] {
&[
Self::Fatal,
Self::Error,
Self::Warn,
Self::Notice,
Self::Verbose,
Self::Debug,
Self::Trace,
Self::Extra,
]
}
pub const fn variant_name(&self) -> &'static str {
match self {
Self::Fatal => "fatal",
Self::Error => "error",
Self::Warn => "warn",
Self::Notice => "notice",
Self::Verbose => "verbose",
Self::Debug => "debug",
Self::Trace => "trace",
Self::Extra => "extra",
}
}
pub const fn help_message(&self) -> &'static str {
match self {
Self::Fatal => "Enables logging for critical conditions, i.e. assertion failures",
Self::Error => "Enables logging for error conditions",
Self::Warn => "Enables logging for warning conditions",
Self::Notice => "Enables logging for normal but significant condition",
Self::Verbose => "Enables logging for verbose informational",
Self::Debug => "Enables logging for debug-level messages",
Self::Trace => "Enables logging for trace debug-level messages",
Self::Extra => "Enables logging for extra debug-level messages",
}
}
}
impl FromStr for LogLevel {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"fatal" => Ok(Self::Fatal),
"error" => Ok(Self::Error),
"warn" => Ok(Self::Warn),
"notice" => Ok(Self::Notice),
"verbose" => Ok(Self::Verbose),
"debug" => Ok(Self::Debug),
"trace" => Ok(Self::Trace),
"extra" => Ok(Self::Extra),
_ => Err(format!("Invalid log level: {s}")),
}
}
}