use std::error::Error as StdError;
use fvm_ipld_encoding::Error as EncodingError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("Maximum depth reached")]
MaxDepth,
#[error("HashBits does not support retrieving more than 8 bits")]
InvalidHashBitLen,
#[error("Invalid HAMT format, node cannot have 0 pointers")]
ZeroPointers,
#[error("Cid ({0}) did not match any in database")]
CidNotFound(String),
#[error("Iteration starting key not found in HAMT")]
StartKeyNotFound,
#[error("{0}")]
Dynamic(anyhow::Error),
}
impl From<String> for Error {
fn from(e: String) -> Self {
Self::Dynamic(anyhow::anyhow!(e))
}
}
impl From<&'static str> for Error {
fn from(e: &'static str) -> Self {
Self::Dynamic(anyhow::anyhow!(e))
}
}
impl From<anyhow::Error> for Error {
fn from(e: anyhow::Error) -> Self {
e.downcast::<Error>().unwrap_or_else(Self::Dynamic)
}
}
impl From<EncodingError> for Error {
fn from(e: EncodingError) -> Self {
Self::Dynamic(anyhow::anyhow!(e))
}
}
impl From<Box<dyn StdError + Send + Sync>> for Error {
fn from(e: Box<dyn StdError + Send + Sync>) -> Self {
Self::Dynamic(anyhow::anyhow!(e))
}
}