casper_types/transfer/
error.rs

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
use core::{
    array::TryFromSliceError,
    fmt::{self, Debug, Display, Formatter},
};
#[cfg(feature = "std")]
use std::error::Error as StdError;

/// Error returned when decoding a `TransferAddr` from a formatted string.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum TransferFromStrError {
    /// The prefix is invalid.
    InvalidPrefix,
    /// The address is not valid hex.
    Hex(base16::DecodeError),
    /// The slice is the wrong length.
    Length(TryFromSliceError),
}

impl From<base16::DecodeError> for TransferFromStrError {
    fn from(error: base16::DecodeError) -> Self {
        TransferFromStrError::Hex(error)
    }
}

impl From<TryFromSliceError> for TransferFromStrError {
    fn from(error: TryFromSliceError) -> Self {
        TransferFromStrError::Length(error)
    }
}

impl Display for TransferFromStrError {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            TransferFromStrError::InvalidPrefix => {
                write!(formatter, "transfer addr prefix is invalid",)
            }
            TransferFromStrError::Hex(error) => {
                write!(
                    formatter,
                    "failed to decode address portion of transfer addr from hex: {}",
                    error
                )
            }
            TransferFromStrError::Length(error) => write!(
                formatter,
                "address portion of transfer addr is wrong length: {}",
                error
            ),
        }
    }
}

#[cfg(feature = "std")]
impl StdError for TransferFromStrError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            TransferFromStrError::InvalidPrefix => None,
            TransferFromStrError::Hex(error) => Some(error),
            TransferFromStrError::Length(error) => Some(error),
        }
    }
}