1#[macro_export]
2macro_rules! error_parse {
4 ($($t:tt)*) => {
5 $crate::Error::Parse($crate::anyhow!($($t)*))
6 };
7}
8
9#[macro_export]
10macro_rules! error_fetch {
12 ($($t:tt)*) => {
13 $crate::Error::Fetch($crate::anyhow!($($t)*))
14 };
15}
16
17#[derive(Debug, thiserror::Error)]
19#[non_exhaustive]
20pub enum Error {
21 #[error(transparent)]
23 Parse(anyhow::Error),
24 #[error(transparent)]
26 Fetch(anyhow::Error),
27 #[error(transparent)]
28 Io(#[from] std::io::Error),
29 #[error("extra input left")]
31 ExtraInputLeft,
32 #[error("end of input")]
34 EndOfInput,
35 #[error("address index out of bounds")]
37 AddressOutOfBounds,
38 #[error("hash resolution mismatch")]
40 ResolutionMismatch,
41 #[error("full hash mismatch")]
43 FullHashMismatch,
44 #[error("discriminant overflow")]
46 DiscriminantOverflow,
47 #[error("zero")]
49 Zero,
50 #[error("out of bounds")]
52 OutOfBounds,
53 #[error("length out of bounds")]
55 UnsupportedLength,
56 #[error(transparent)]
58 Utf8(#[from] std::string::FromUtf8Error),
59 #[error("unknown extension")]
61 UnknownExtension,
62 #[error("wrong extension type")]
64 ExtensionType,
65 #[error("not implemented")]
66 Unimplemented,
67 #[error("hash not found in the resolve")]
68 HashNotFound,
69}
70
71impl Error {
72 pub fn parse(e: impl Into<anyhow::Error>) -> Self {
74 Self::Parse(e.into())
75 }
76
77 pub fn fetch(e: impl Into<anyhow::Error>) -> Self {
79 Self::Fetch(e.into())
80 }
81
82 pub fn io(e: impl Into<std::io::Error>) -> Self {
83 e.into().into()
84 }
85
86 fn io_kind(&self) -> std::io::ErrorKind {
87 use std::io::ErrorKind;
88 match self {
89 Error::Parse(_) => ErrorKind::InvalidData,
90 Error::Fetch(_) => ErrorKind::Other,
91 Error::Io(e) => e.kind(),
92 Error::ExtraInputLeft => ErrorKind::InvalidData,
93 Error::EndOfInput => ErrorKind::UnexpectedEof,
94 Error::AddressOutOfBounds => ErrorKind::Other,
95 Error::ResolutionMismatch => ErrorKind::InvalidData,
96 Error::FullHashMismatch => ErrorKind::InvalidData,
97 Error::DiscriminantOverflow => ErrorKind::InvalidData,
98 Error::Zero => ErrorKind::InvalidData,
99 Error::OutOfBounds => ErrorKind::InvalidData,
100 Error::UnsupportedLength => ErrorKind::FileTooLarge,
101 Error::Utf8(_) => ErrorKind::InvalidData,
102 Error::UnknownExtension => ErrorKind::Other,
103 Error::ExtensionType => ErrorKind::Other,
104 Error::Unimplemented => ErrorKind::Unsupported,
105 Error::HashNotFound => ErrorKind::NotFound,
106 }
107 }
108}
109
110impl From<Error> for std::io::Error {
111 fn from(value: Error) -> Self {
112 match value {
113 Error::Io(e) => e,
114 e => Self::new(e.io_kind(), e),
115 }
116 }
117}
118
119pub type Result<T> = std::result::Result<T, Error>;