use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("rocksdb error: {0}")]
RocksDb(#[from] rocksdb::Error),
#[cfg(feature = "surrealdb-storage")]
#[error("surrealdb error: {0}")]
SurrealDb(String),
#[error("serialization error: {0}")]
Serialization(String),
#[error("not found: {kind} {id}")]
NotFound {
kind: &'static str,
id: String,
},
#[error("export/import format error: {0}")]
Format(String),
#[error("io error: {0}")]
Io(#[from] std::io::Error),
#[error(transparent)]
External(#[from] anyhow::Error),
}
pub type Result<T, E = Error> = std::result::Result<T, E>;
impl From<bincode::error::EncodeError> for Error {
fn from(err: bincode::error::EncodeError) -> Self {
Self::Serialization(err.to_string())
}
}
impl From<bincode::error::DecodeError> for Error {
fn from(err: bincode::error::DecodeError) -> Self {
Self::Serialization(err.to_string())
}
}
impl Error {
#[must_use]
pub fn kind(&self) -> &'static str {
match self {
Self::RocksDb(_) => "rocksdb",
#[cfg(feature = "surrealdb-storage")]
Self::SurrealDb(_) => "surrealdb",
Self::Serialization(_) => "serialization",
Self::NotFound { .. } => "not_found",
Self::Format(_) => "format",
Self::Io(_) => "io",
Self::External(_) => "external",
}
}
}