use alloc::{boxed::Box, string::String};
use thiserror::Error;
use crate::merkle::{
MerkleError,
smt::{
SmtLeafError, SmtProofError, TreeId, VersionId,
large_forest::{backend::BackendError, history::error::HistoryError, root::LineageId},
},
};
#[derive(Debug, Error)]
pub enum LargeSmtForestError {
#[error("Version {provided} is not newer than latest-known {latest}")]
BadVersion { provided: VersionId, latest: VersionId },
#[error("Duplicate lineage ID {0} provided")]
DuplicateLineage(LineageId),
#[error(transparent)]
Fatal(Box<dyn core::error::Error + Sync + Send>),
#[error(transparent)]
History(#[from] HistoryError),
#[error(transparent)]
Merkle(#[from] MerkleError),
#[error(transparent)]
SmtLeaf(#[from] SmtLeafError),
#[error(transparent)]
SmtProof(#[from] SmtProofError),
#[error("The lineage {0:?} is not in the forest")]
UnknownLineage(LineageId),
#[error("The tree {0} is not in the forest")]
UnknownTree(TreeId),
#[error("The version {0} is not known by the forest")]
UnknownVersion(VersionId),
#[error(transparent)]
Other(#[from] Box<dyn core::error::Error + Sync + Send>),
#[error("Unspecified error: {0}")]
Unspecified(String),
}
impl LargeSmtForestError {
pub fn fatal_from<E: core::error::Error + Sync + Send + 'static>(e: E) -> Self {
Self::Fatal(Box::new(e))
}
}
impl From<BackendError> for LargeSmtForestError {
fn from(value: BackendError) -> Self {
match value {
BackendError::CorruptedData(_) => LargeSmtForestError::fatal_from(value),
BackendError::DuplicateLineage(l) => LargeSmtForestError::DuplicateLineage(l),
BackendError::Internal(e) => LargeSmtForestError::Fatal(e),
BackendError::Merkle(e) => LargeSmtForestError::from(e),
BackendError::Other(e) => LargeSmtForestError::from(e),
BackendError::UnknownLineage(t) => LargeSmtForestError::UnknownLineage(t),
BackendError::Unspecified(msg) => LargeSmtForestError::Unspecified(msg),
}
}
}
pub type Result<T> = core::result::Result<T, LargeSmtForestError>;