libipld_core/
error.rs

1//! `Ipld` error definitions.
2#[cfg(feature = "serde-codec")]
3use alloc::string::ToString;
4use alloc::{string::String, vec::Vec};
5
6use crate::cid::Cid;
7use crate::ipld::{Ipld, IpldIndex};
8pub use anyhow::{Error, Result};
9#[cfg(feature = "std")]
10use thiserror::Error;
11
12/// Block exceeds 1MiB.
13#[derive(Clone, Copy, Debug)]
14#[cfg_attr(feature = "std", derive(Error), error("Block size {0} exceeds 1MiB."))]
15pub struct BlockTooLarge(pub usize);
16
17/// The codec is unsupported.
18#[derive(Clone, Copy, Debug)]
19#[cfg_attr(feature = "std", derive(Error), error("Unsupported codec {0:?}."))]
20pub struct UnsupportedCodec(pub u64);
21
22/// The multihash is unsupported.
23#[derive(Clone, Copy, Debug)]
24#[cfg_attr(feature = "std", derive(Error), error("Unsupported multihash {0:?}."))]
25pub struct UnsupportedMultihash(pub u64);
26
27/// Hash does not match the CID.
28#[derive(Clone, Debug)]
29#[cfg_attr(
30    feature = "std",
31    derive(Error),
32    error("Hash of data does not match the CID.")
33)]
34pub struct InvalidMultihash(pub Vec<u8>);
35
36/// The block wasn't found. The supplied string is a CID.
37#[derive(Clone, Copy, Debug)]
38#[cfg_attr(feature = "std", derive(Error), error("Failed to retrieve block {0}."))]
39pub struct BlockNotFound(pub Cid);
40
41/// Error during Serde operations.
42#[cfg(feature = "serde-codec")]
43#[derive(Clone, Debug)]
44pub struct SerdeError(String);
45
46#[cfg(feature = "serde-codec")]
47impl core::fmt::Display for SerdeError {
48    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
49        write!(f, "Serde error: {}", self.0)
50    }
51}
52
53#[cfg(feature = "serde-codec")]
54impl serde::de::Error for SerdeError {
55    fn custom<T: core::fmt::Display>(msg: T) -> Self {
56        Self(msg.to_string())
57    }
58}
59
60#[cfg(feature = "serde-codec")]
61impl serde::ser::Error for SerdeError {
62    fn custom<T: core::fmt::Display>(msg: T) -> Self {
63        Self(msg.to_string())
64    }
65}
66
67#[cfg(feature = "serde-codec")]
68impl serde::ser::StdError for SerdeError {}
69
70/// Type error.
71#[derive(Clone, Debug)]
72#[cfg_attr(
73    feature = "std",
74    derive(Error),
75    error("Expected {expected:?} but found {found:?}")
76)]
77pub struct TypeError {
78    /// The expected type.
79    pub expected: TypeErrorType,
80    /// The actual type.
81    pub found: TypeErrorType,
82}
83
84impl TypeError {
85    /// Creates a new type error.
86    pub fn new<A: Into<TypeErrorType>, B: Into<TypeErrorType>>(expected: A, found: B) -> Self {
87        Self {
88            expected: expected.into(),
89            found: found.into(),
90        }
91    }
92}
93
94#[cfg(not(feature = "std"))]
95impl core::fmt::Display for TypeError {
96    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
97        write!(f, "Expected {:?} but found {:?}", self.expected, self.found)
98    }
99}
100
101/// Type error type.
102#[derive(Clone, Debug)]
103pub enum TypeErrorType {
104    /// Null type.
105    Null,
106    /// Boolean type.
107    Bool,
108    /// Integer type.
109    Integer,
110    /// Float type.
111    Float,
112    /// String type.
113    String,
114    /// Bytes type.
115    Bytes,
116    /// List type.
117    List,
118    /// Map type.
119    Map,
120    /// Link type.
121    Link,
122    /// Key type.
123    Key(String),
124    /// Index type.
125    Index(usize),
126}
127
128impl From<Ipld> for TypeErrorType {
129    fn from(ipld: Ipld) -> Self {
130        Self::from(&ipld)
131    }
132}
133
134impl From<&Ipld> for TypeErrorType {
135    fn from(ipld: &Ipld) -> Self {
136        match ipld {
137            Ipld::Null => Self::Null,
138            Ipld::Bool(_) => Self::Bool,
139            Ipld::Integer(_) => Self::Integer,
140            Ipld::Float(_) => Self::Float,
141            Ipld::String(_) => Self::String,
142            Ipld::Bytes(_) => Self::Bytes,
143            Ipld::List(_) => Self::List,
144            Ipld::Map(_) => Self::Map,
145            Ipld::Link(_) => Self::Link,
146        }
147    }
148}
149
150impl From<IpldIndex<'_>> for TypeErrorType {
151    fn from(index: IpldIndex<'_>) -> Self {
152        match index {
153            IpldIndex::List(i) => Self::Index(i),
154            IpldIndex::Map(s) => Self::Key(s),
155            IpldIndex::MapRef(s) => Self::Key(s.into()),
156        }
157    }
158}