monoutils_store/
error.rs

1use std::{
2    error::Error,
3    fmt::{self, Display},
4};
5
6use libipld::Cid;
7use thiserror::Error;
8
9use super::Codec;
10
11//--------------------------------------------------------------------------------------------------
12// Types
13//--------------------------------------------------------------------------------------------------
14
15/// The result of a file system operation.
16pub type StoreResult<T> = Result<T, StoreError>;
17
18/// An error that occurred during a file system operation.
19#[derive(Debug, Error, PartialEq)]
20pub enum StoreError {
21    /// The block was not found.
22    #[error("Block not found: {0}")]
23    BlockNotFound(Cid),
24
25    /// The node block is too large.
26    #[error("Node block too large: {0} > {1}")]
27    NodeBlockTooLarge(u64, u64),
28
29    /// The raw block is too large.
30    #[error("Raw block too large: {0} > {1}")]
31    RawBlockTooLarge(u64, u64),
32
33    /// Codec not supported.
34    #[error("Unsupported Codec: {0}")]
35    UnsupportedCodec(u64),
36
37    /// Expected block codec does not match the actual codec.
38    #[error("Unexpected block codec: expected: {0:?} got: {1:?}")]
39    UnexpectedBlockCodec(Codec, Codec),
40
41    /// Custom error.
42    #[error("Custom error: {0}")]
43    Custom(#[from] AnyError),
44
45    /// Layout error.
46    #[error("Layout error: {0}")]
47    LayoutError(#[from] LayoutError),
48}
49
50/// An error that occurred during a layout operation.
51#[derive(Debug, Error, PartialEq)]
52pub enum LayoutError {
53    /// No leaf block found.
54    #[error("No leaf block found")]
55    NoLeafBlock,
56}
57
58/// An error that can represent any error.
59#[derive(Debug)]
60pub struct AnyError {
61    error: anyhow::Error,
62}
63
64//--------------------------------------------------------------------------------------------------
65// Methods
66//--------------------------------------------------------------------------------------------------
67
68impl StoreError {
69    /// Creates a new `Err` result.
70    pub fn custom(error: impl Into<anyhow::Error>) -> StoreError {
71        StoreError::Custom(AnyError {
72            error: error.into(),
73        })
74    }
75}
76
77impl AnyError {
78    /// Downcasts the error to a `T`.
79    pub fn downcast<T>(&self) -> Option<&T>
80    where
81        T: Display + fmt::Debug + Send + Sync + 'static,
82    {
83        self.error.downcast_ref::<T>()
84    }
85}
86
87//--------------------------------------------------------------------------------------------------
88// Functions
89//--------------------------------------------------------------------------------------------------
90
91/// Creates an `Ok` `FsResult` d.
92#[allow(non_snake_case)]
93pub fn Ok<T>(value: T) -> StoreResult<T> {
94    Result::Ok(value)
95}
96
97//--------------------------------------------------------------------------------------------------
98// Trait Implementations
99//--------------------------------------------------------------------------------------------------
100
101impl PartialEq for AnyError {
102    fn eq(&self, other: &Self) -> bool {
103        self.error.to_string() == other.error.to_string()
104    }
105}
106
107impl Display for AnyError {
108    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109        write!(f, "{}", self.error)
110    }
111}
112
113impl Error for AnyError {}