1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#[cfg(not(feature = "std"))]
use core2::{error::Error as StdError, io};
#[cfg(feature = "std")]
use std::{error::Error as StdError, io};

use unsigned_varint::decode;

/// Opaque error struct for operations involving a [`Multihash`](crate::Multihash).
#[derive(Debug)]
pub struct Error {
    kind: Kind,
}

impl Error {
    pub(crate) const fn invalid_size(size: u64) -> Self {
        Self {
            kind: Kind::InvalidSize(size),
        }
    }

    #[cfg(not(feature = "std"))]
    pub(crate) const fn insufficient_varint_bytes() -> Self {
        Self {
            kind: Kind::Varint(decode::Error::Insufficient),
        }
    }

    #[cfg(not(feature = "std"))]
    pub(crate) const fn varint_overflow() -> Self {
        Self {
            kind: Kind::Varint(decode::Error::Overflow),
        }
    }
}

impl core::fmt::Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        self.kind.fmt(f)
    }
}

#[derive(Debug)]
enum Kind {
    /// Io error.
    Io(io::Error),
    /// Invalid multihash size.
    InvalidSize(u64),
    /// Invalid varint.
    Varint(decode::Error),
}

#[cfg(feature = "std")]
pub(crate) fn unsigned_varint_to_multihash_error(err: unsigned_varint::io::ReadError) -> Error {
    match err {
        unsigned_varint::io::ReadError::Io(err) => io_to_multihash_error(err),
        unsigned_varint::io::ReadError::Decode(err) => Error {
            kind: Kind::Varint(err),
        },
        other => io_to_multihash_error(io::Error::new(io::ErrorKind::Other, other)),
    }
}

#[cfg(not(feature = "std"))]
pub(crate) fn unsigned_varint_decode_to_multihash_error(
    err: unsigned_varint::decode::Error,
) -> Error {
    Error {
        kind: Kind::Varint(err),
    }
}

pub(crate) fn io_to_multihash_error(err: io::Error) -> Error {
    Error {
        kind: Kind::Io(err),
    }
}

impl core::fmt::Display for Kind {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        match self {
            Self::Io(err) => write!(f, "{err}"),
            Self::InvalidSize(size) => write!(f, "Invalid multihash size {size}."),
            Self::Varint(err) => write!(f, "{err}"),
        }
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match &self.kind {
            Kind::Io(inner) => Some(inner),
            Kind::InvalidSize(_) => None,
            Kind::Varint(_) => None, // FIXME: Does not implement `core2::Error`.
        }
    }
}