solana_address/
error.rs

1use {core::fmt, solana_program_error::ProgramError};
2
3#[cfg_attr(feature = "serde", derive(serde_derive::Serialize))]
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum AddressError {
6    /// Length of the seed is too long for address generation
7    MaxSeedLengthExceeded,
8    InvalidSeeds,
9    IllegalOwner,
10}
11
12impl core::error::Error for AddressError {}
13
14impl fmt::Display for AddressError {
15    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16        match self {
17            AddressError::MaxSeedLengthExceeded => {
18                f.write_str("Length of the seed is too long for address generation")
19            }
20            AddressError::InvalidSeeds => {
21                f.write_str("Provided seeds do not result in a valid address")
22            }
23            AddressError::IllegalOwner => f.write_str("Provided owner is not allowed"),
24        }
25    }
26}
27
28impl From<u64> for AddressError {
29    fn from(error: u64) -> Self {
30        match error {
31            0 => AddressError::MaxSeedLengthExceeded,
32            1 => AddressError::InvalidSeeds,
33            2 => AddressError::IllegalOwner,
34            _ => panic!("Unsupported AddressError"),
35        }
36    }
37}
38
39impl From<AddressError> for ProgramError {
40    fn from(error: AddressError) -> Self {
41        match error {
42            AddressError::MaxSeedLengthExceeded => Self::MaxSeedLengthExceeded,
43            AddressError::InvalidSeeds => Self::InvalidSeeds,
44            AddressError::IllegalOwner => Self::IllegalOwner,
45        }
46    }
47}
48
49#[cfg_attr(feature = "serde", derive(serde_derive::Serialize))]
50#[cfg(feature = "decode")]
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub enum ParseAddressError {
53    WrongSize,
54    Invalid,
55}
56
57#[cfg(feature = "decode")]
58impl core::error::Error for ParseAddressError {}
59
60#[cfg(feature = "decode")]
61impl fmt::Display for ParseAddressError {
62    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63        match self {
64            ParseAddressError::WrongSize => f.write_str("String is the wrong size"),
65            ParseAddressError::Invalid => f.write_str("Invalid Base58 string"),
66        }
67    }
68}
69
70#[cfg(feature = "decode")]
71impl From<core::convert::Infallible> for ParseAddressError {
72    fn from(_: core::convert::Infallible) -> Self {
73        unreachable!("Infallible uninhabited");
74    }
75}