object_rainbow/
error.rs

1#[macro_export]
2/// Construct [`Error::Parse`].
3macro_rules! error_parse {
4    ($($t:tt)*) => {
5        $crate::Error::Parse($crate::anyhow!($($t)*))
6    };
7}
8
9#[macro_export]
10/// Construct [`Error::Fetch`].
11macro_rules! error_fetch {
12    ($($t:tt)*) => {
13        $crate::Error::Fetch($crate::anyhow!($($t)*))
14    };
15}
16
17/// Errors encountered during fetching an object. Mostly related to parsing.
18#[derive(Debug, thiserror::Error)]
19#[non_exhaustive]
20pub enum Error {
21    /// Arbitrary parsing error.
22    #[error(transparent)]
23    Parse(anyhow::Error),
24    /// Arbitrary fetching error.
25    #[error(transparent)]
26    Fetch(anyhow::Error),
27    #[error(transparent)]
28    Io(#[from] std::io::Error),
29    /// Data left after an [`Inline`] got parsed.
30    #[error("extra input left")]
31    ExtraInputLeft,
32    /// EOF.
33    #[error("end of input")]
34    EndOfInput,
35    /// Overran [`PointInput`]'s [`Address`] vector.
36    #[error("address index out of bounds")]
37    AddressOutOfBounds,
38    /// [`Address::hash`] doesn't match what [`Resolve`] returned.
39    #[error("hash resolution mismatch")]
40    ResolutionMismatch,
41    /// [`FullHash::full_hash`] doesn't match [`Singular::hash`].
42    #[error("data hash mismatch")]
43    DataMismatch,
44    /// Discriminant out of range for an [`Enum`].
45    #[error("discriminant overflow")]
46    DiscriminantOverflow,
47    /// Unepxected zero for a non-zero value.
48    #[error("zero")]
49    Zero,
50    /// Value out of bounds for a certain type.
51    #[error("out of bounds")]
52    OutOfBounds,
53    /// Current architecture (32-bit) is unable to handle lengths of this size.
54    #[error("length out of bounds")]
55    UnsupportedLength,
56    /// Not UTF-8.
57    #[error(transparent)]
58    Utf8(#[from] std::string::FromUtf8Error),
59    /// [`Resolve::extension`] (or related things) were unable to resolve the extension.
60    #[error("unknown extension")]
61    UnknownExtension,
62    /// Extension type didn't match what we asked for. This might be turned into panic later.
63    #[error("wrong extension type")]
64    ExtensionType,
65    #[error("not implemented")]
66    Unimplemented,
67    #[error("hash not found in the resolver")]
68    HashNotFound,
69}
70
71impl Error {
72    /// Construct [`Error::Parse`] from another error.
73    pub fn parse(e: impl Into<anyhow::Error>) -> Self {
74        Self::Parse(e.into())
75    }
76
77    /// Construct [`Error::Fetch`] from another error.
78    pub fn fetch(e: impl Into<anyhow::Error>) -> Self {
79        Self::Fetch(e.into())
80    }
81}
82
83pub type Result<T> = std::result::Result<T, Error>;