1#[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#[derive(Clone, Copy, Debug)]
14#[cfg_attr(feature = "std", derive(Error), error("Block size {0} exceeds 1MiB."))]
15pub struct BlockTooLarge(pub usize);
16
17#[derive(Clone, Copy, Debug)]
19#[cfg_attr(feature = "std", derive(Error), error("Unsupported codec {0:?}."))]
20pub struct UnsupportedCodec(pub u64);
21
22#[derive(Clone, Copy, Debug)]
24#[cfg_attr(feature = "std", derive(Error), error("Unsupported multihash {0:?}."))]
25pub struct UnsupportedMultihash(pub u64);
26
27#[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#[derive(Clone, Copy, Debug)]
38#[cfg_attr(feature = "std", derive(Error), error("Failed to retrieve block {0}."))]
39pub struct BlockNotFound(pub Cid);
40
41#[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#[derive(Clone, Debug)]
72#[cfg_attr(
73 feature = "std",
74 derive(Error),
75 error("Expected {expected:?} but found {found:?}")
76)]
77pub struct TypeError {
78 pub expected: TypeErrorType,
80 pub found: TypeErrorType,
82}
83
84impl TypeError {
85 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#[derive(Clone, Debug)]
103pub enum TypeErrorType {
104 Null,
106 Bool,
108 Integer,
110 Float,
112 String,
114 Bytes,
116 List,
118 Map,
120 Link,
122 Key(String),
124 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}