base58ck/
error.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Error code for the `base58` crate.
4
5use core::fmt;
6
7use internals::write_err;
8
9/// An error occurred during base58 decoding (with checksum).
10#[derive(Debug, Clone, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum Error {
13    /// Invalid character while decoding.
14    Decode(InvalidCharacterError),
15    /// Checksum was not correct.
16    IncorrectChecksum(IncorrectChecksumError),
17    /// Checked data was too short.
18    TooShort(TooShortError),
19}
20
21internals::impl_from_infallible!(Error);
22
23impl fmt::Display for Error {
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        use Error::*;
26
27        match *self {
28            Decode(ref e) => write_err!(f, "decode"; e),
29            IncorrectChecksum(ref e) => write_err!(f, "incorrect checksum"; e),
30            TooShort(ref e) => write_err!(f, "too short"; e),
31        }
32    }
33}
34
35#[cfg(feature = "std")]
36impl std::error::Error for Error {
37    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
38        use Error::*;
39
40        match *self {
41            Decode(ref e) => Some(e),
42            IncorrectChecksum(ref e) => Some(e),
43            TooShort(ref e) => Some(e),
44        }
45    }
46}
47
48impl From<InvalidCharacterError> for Error {
49    #[inline]
50    fn from(e: InvalidCharacterError) -> Self { Self::Decode(e) }
51}
52
53impl From<IncorrectChecksumError> for Error {
54    #[inline]
55    fn from(e: IncorrectChecksumError) -> Self { Self::IncorrectChecksum(e) }
56}
57
58impl From<TooShortError> for Error {
59    #[inline]
60    fn from(e: TooShortError) -> Self { Self::TooShort(e) }
61}
62
63/// Checksum was not correct.
64#[derive(Debug, Clone, PartialEq, Eq)]
65pub struct IncorrectChecksumError {
66    /// The incorrect checksum.
67    pub(super) incorrect: u32,
68    /// The expected checksum.
69    pub(super) expected: u32,
70}
71
72impl IncorrectChecksumError {
73    /// Returns the incorrect checksum along with the expected checksum.
74    pub fn incorrect_checksum(&self) -> (u32, u32) { (self.incorrect, self.expected) }
75}
76
77impl fmt::Display for IncorrectChecksumError {
78    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79        write!(
80            f,
81            "base58 checksum {:#x} does not match expected {:#x}",
82            self.incorrect, self.expected
83        )
84    }
85}
86
87#[cfg(feature = "std")]
88impl std::error::Error for IncorrectChecksumError {}
89
90/// The decode base58 data was too short (require at least 4 bytes for checksum).
91#[derive(Debug, Clone, PartialEq, Eq)]
92pub struct TooShortError {
93    /// The length of the decoded data.
94    pub(super) length: usize,
95}
96
97impl TooShortError {
98    /// Returns the invalid base58 string length (require at least 4 bytes for checksum).
99    pub fn invalid_base58_length(&self) -> usize { self.length }
100}
101
102impl fmt::Display for TooShortError {
103    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
104        write!(
105            f,
106            "base58 decoded data was not long enough, must be at least 4 byte: {}",
107            self.length
108        )
109    }
110}
111
112#[cfg(feature = "std")]
113impl std::error::Error for TooShortError {}
114
115/// Found a invalid ASCII byte while decoding base58 string.
116#[derive(Debug, Clone, PartialEq, Eq)]
117pub struct InvalidCharacterError {
118    pub(super) invalid: u8,
119}
120
121impl InvalidCharacterError {
122    /// Returns the ASCII byte that is not a valid base58 character.
123    pub fn invalid_base58_character(&self) -> u8 { self.invalid }
124}
125
126impl fmt::Display for InvalidCharacterError {
127    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128        write!(f, "invalid base58 character {:#x}", self.invalid)
129    }
130}
131
132#[cfg(feature = "std")]
133impl std::error::Error for InvalidCharacterError {}