1use thiserror::Error;
29
30pub type Result<T> = std::result::Result<T, Error>;
32
33#[derive(Debug, Error)]
38pub enum Error {
39 #[error("IO error: {0}")]
41 Io(#[from] std::io::Error),
42
43 #[error("Block not found: {0}")]
45 BlockNotFound(String),
46
47 #[error("CID error: {0}")]
49 Cid(String),
50
51 #[error("Serialization error: {0}")]
53 Serialization(String),
54
55 #[error("Deserialization error: {0}")]
57 Deserialization(String),
58
59 #[error("Network error: {0}")]
61 Network(String),
62
63 #[error("Storage error: {0}")]
65 Storage(String),
66
67 #[error("Encryption error: {0}")]
69 Encryption(String),
70
71 #[error("Invalid data: {0}")]
73 InvalidData(String),
74
75 #[error("Invalid input: {0}")]
77 InvalidInput(String),
78
79 #[error("Not found: {0}")]
81 NotFound(String),
82
83 #[error("Protocol error: {0}")]
85 Protocol(String),
86
87 #[error("Not implemented: {0}")]
89 NotImplemented(String),
90
91 #[error("Internal error: {0}")]
93 Internal(String),
94
95 #[error("Initialization error: {0}")]
97 Initialization(String),
98
99 #[error("Verification error: {0}")]
101 Verification(String),
102}
103
104impl Error {
105 #[inline]
107 pub const fn is_io(&self) -> bool {
108 matches!(self, Error::Io(_))
109 }
110
111 #[inline]
113 pub const fn is_not_found(&self) -> bool {
114 matches!(self, Error::BlockNotFound(_) | Error::NotFound(_))
115 }
116
117 #[inline]
119 pub const fn is_serialization(&self) -> bool {
120 matches!(self, Error::Serialization(_) | Error::Deserialization(_))
121 }
122
123 #[inline]
125 pub const fn is_network(&self) -> bool {
126 matches!(self, Error::Network(_))
127 }
128
129 #[inline]
131 pub const fn is_storage(&self) -> bool {
132 matches!(self, Error::Storage(_))
133 }
134
135 #[inline]
137 pub const fn is_validation(&self) -> bool {
138 matches!(self, Error::InvalidData(_) | Error::InvalidInput(_))
139 }
140
141 #[inline]
143 pub const fn is_cid(&self) -> bool {
144 matches!(self, Error::Cid(_))
145 }
146
147 #[inline]
149 pub const fn is_verification(&self) -> bool {
150 matches!(self, Error::Verification(_))
151 }
152
153 pub const fn category(&self) -> &'static str {
155 match self {
156 Error::Io(_) => "io",
157 Error::BlockNotFound(_) => "not_found",
158 Error::Cid(_) => "cid",
159 Error::Serialization(_) => "serialization",
160 Error::Deserialization(_) => "deserialization",
161 Error::Network(_) => "network",
162 Error::Storage(_) => "storage",
163 Error::Encryption(_) => "encryption",
164 Error::InvalidData(_) => "invalid_data",
165 Error::InvalidInput(_) => "invalid_input",
166 Error::NotFound(_) => "not_found",
167 Error::Protocol(_) => "protocol",
168 Error::NotImplemented(_) => "not_implemented",
169 Error::Internal(_) => "internal",
170 Error::Initialization(_) => "initialization",
171 Error::Verification(_) => "verification",
172 }
173 }
174
175 pub const fn is_recoverable(&self) -> bool {
177 matches!(self, Error::Io(_) | Error::Network(_) | Error::Storage(_))
178 }
179
180 pub const fn is_internal(&self) -> bool {
182 matches!(self, Error::Internal(_))
183 }
184}