casper_types/transfer/
error.rs

1use core::{
2    array::TryFromSliceError,
3    fmt::{self, Debug, Display, Formatter},
4};
5#[cfg(feature = "std")]
6use std::error::Error as StdError;
7
8/// Error returned when decoding a `TransferAddr` from a formatted string.
9#[derive(Debug, Clone)]
10#[non_exhaustive]
11pub enum TransferFromStrError {
12    /// The prefix is invalid.
13    InvalidPrefix,
14    /// The address is not valid hex.
15    Hex(base16::DecodeError),
16    /// The slice is the wrong length.
17    Length(TryFromSliceError),
18}
19
20impl From<base16::DecodeError> for TransferFromStrError {
21    fn from(error: base16::DecodeError) -> Self {
22        TransferFromStrError::Hex(error)
23    }
24}
25
26impl From<TryFromSliceError> for TransferFromStrError {
27    fn from(error: TryFromSliceError) -> Self {
28        TransferFromStrError::Length(error)
29    }
30}
31
32impl Display for TransferFromStrError {
33    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
34        match self {
35            TransferFromStrError::InvalidPrefix => {
36                write!(formatter, "transfer addr prefix is invalid",)
37            }
38            TransferFromStrError::Hex(error) => {
39                write!(
40                    formatter,
41                    "failed to decode address portion of transfer addr from hex: {}",
42                    error
43                )
44            }
45            TransferFromStrError::Length(error) => write!(
46                formatter,
47                "address portion of transfer addr is wrong length: {}",
48                error
49            ),
50        }
51    }
52}
53
54#[cfg(feature = "std")]
55impl StdError for TransferFromStrError {
56    fn source(&self) -> Option<&(dyn StdError + 'static)> {
57        match self {
58            TransferFromStrError::InvalidPrefix => None,
59            TransferFromStrError::Hex(error) => Some(error),
60            TransferFromStrError::Length(error) => Some(error),
61        }
62    }
63}