1use std::{
2 error::Error,
3 fmt::{self, Display},
4};
5
6use libipld::Cid;
7use thiserror::Error;
8
9use super::Codec;
10
11pub type StoreResult<T> = Result<T, StoreError>;
17
18#[derive(Debug, Error, PartialEq)]
20pub enum StoreError {
21 #[error("Block not found: {0}")]
23 BlockNotFound(Cid),
24
25 #[error("Node block too large: {0} > {1}")]
27 NodeBlockTooLarge(u64, u64),
28
29 #[error("Raw block too large: {0} > {1}")]
31 RawBlockTooLarge(u64, u64),
32
33 #[error("Unsupported Codec: {0}")]
35 UnsupportedCodec(u64),
36
37 #[error("Unexpected block codec: expected: {0:?} got: {1:?}")]
39 UnexpectedBlockCodec(Codec, Codec),
40
41 #[error("Custom error: {0}")]
43 Custom(#[from] AnyError),
44
45 #[error("Layout error: {0}")]
47 LayoutError(#[from] LayoutError),
48}
49
50#[derive(Debug, Error, PartialEq)]
52pub enum LayoutError {
53 #[error("No leaf block found")]
55 NoLeafBlock,
56}
57
58#[derive(Debug)]
60pub struct AnyError {
61 error: anyhow::Error,
62}
63
64impl StoreError {
69 pub fn custom(error: impl Into<anyhow::Error>) -> StoreError {
71 StoreError::Custom(AnyError {
72 error: error.into(),
73 })
74 }
75}
76
77impl AnyError {
78 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#[allow(non_snake_case)]
93pub fn Ok<T>(value: T) -> StoreResult<T> {
94 Result::Ok(value)
95}
96
97impl 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 {}