forest-filecoin 0.33.2

Rust Filecoin implementation.
Documentation
// Copyright 2019-2026 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use std::{borrow::Cow, fmt::Debug};

use crate::blocks::CreateTipsetError;
use cid::Error as CidErr;
use fil_actors_shared::fvm_ipld_amt::Error as AmtErr;
use fvm_ipld_encoding::Error as EncErr;
use thiserror::Error;

/// Chain error
#[derive(Debug, Error)]
pub enum Error {
    /// Key was not found
    #[error("Invalid tipset: {0}")]
    UndefinedKey(String),
    /// Key not found in database
    #[error("{0} not found")]
    NotFound(Cow<'static, str>),
    /// Error originating constructing blockchain structures
    #[error(transparent)]
    Blockchain(#[from] CreateTipsetError),
    /// Error originating from encoding arbitrary data
    #[error("{0}")]
    Encoding(String),
    /// Error originating from Cid creation
    #[error(transparent)]
    Cid(#[from] CidErr),
    /// Amt error
    #[error("State error: {0}")]
    State(Cow<'static, str>),
    /// Other chain error
    #[error("{0}")]
    Other(String),
}

impl From<EncErr> for Error {
    fn from(e: EncErr) -> Error {
        Error::Encoding(e.to_string())
    }
}

impl From<AmtErr> for Error {
    fn from(e: AmtErr) -> Error {
        Error::state(e.to_string())
    }
}

impl From<String> for Error {
    fn from(e: String) -> Self {
        Error::Other(e)
    }
}

impl From<anyhow::Error> for Error {
    fn from(e: anyhow::Error) -> Self {
        Error::Other(format!("{e:#}"))
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Error::Other(e.to_string())
    }
}

impl<T> From<flume::SendError<T>> for Error {
    fn from(e: flume::SendError<T>) -> Self {
        Error::Other(e.to_string())
    }
}

impl Error {
    pub fn state(msg: impl Into<Cow<'static, str>>) -> Self {
        Self::State(msg.into())
    }
}