solana_precompile_error/
lib.rs

1/// Precompile errors
2use {core::fmt, solana_decode_error::DecodeError};
3
4/// Precompile errors
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum PrecompileError {
7    InvalidPublicKey,
8    InvalidRecoveryId,
9    InvalidSignature,
10    InvalidDataOffsets,
11    InvalidInstructionDataSize,
12}
13
14impl num_traits::FromPrimitive for PrecompileError {
15    #[inline]
16    fn from_i64(n: i64) -> Option<Self> {
17        if n == PrecompileError::InvalidPublicKey as i64 {
18            Some(PrecompileError::InvalidPublicKey)
19        } else if n == PrecompileError::InvalidRecoveryId as i64 {
20            Some(PrecompileError::InvalidRecoveryId)
21        } else if n == PrecompileError::InvalidSignature as i64 {
22            Some(PrecompileError::InvalidSignature)
23        } else if n == PrecompileError::InvalidDataOffsets as i64 {
24            Some(PrecompileError::InvalidDataOffsets)
25        } else if n == PrecompileError::InvalidInstructionDataSize as i64 {
26            Some(PrecompileError::InvalidInstructionDataSize)
27        } else {
28            None
29        }
30    }
31    #[inline]
32    fn from_u64(n: u64) -> Option<Self> {
33        Self::from_i64(n as i64)
34    }
35}
36
37impl num_traits::ToPrimitive for PrecompileError {
38    #[inline]
39    fn to_i64(&self) -> Option<i64> {
40        Some(match *self {
41            PrecompileError::InvalidPublicKey => PrecompileError::InvalidPublicKey as i64,
42            PrecompileError::InvalidRecoveryId => PrecompileError::InvalidRecoveryId as i64,
43            PrecompileError::InvalidSignature => PrecompileError::InvalidSignature as i64,
44            PrecompileError::InvalidDataOffsets => PrecompileError::InvalidDataOffsets as i64,
45            PrecompileError::InvalidInstructionDataSize => {
46                PrecompileError::InvalidInstructionDataSize as i64
47            }
48        })
49    }
50    #[inline]
51    fn to_u64(&self) -> Option<u64> {
52        self.to_i64().map(|x| x as u64)
53    }
54}
55
56impl std::error::Error for PrecompileError {}
57
58impl fmt::Display for PrecompileError {
59    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60        match self {
61            PrecompileError::InvalidPublicKey => f.write_str("public key is not valid"),
62            PrecompileError::InvalidRecoveryId => f.write_str("id is not valid"),
63            PrecompileError::InvalidSignature => f.write_str("signature is not valid"),
64            PrecompileError::InvalidDataOffsets => f.write_str("offset not valid"),
65            PrecompileError::InvalidInstructionDataSize => {
66                f.write_str("instruction is incorrect size")
67            }
68        }
69    }
70}
71
72impl<T> DecodeError<T> for PrecompileError {
73    fn type_of() -> &'static str {
74        "PrecompileError"
75    }
76}