spl_pod/
error.rs

1//! Error types
2use {
3    solana_program_error::{ProgramError, ToStr},
4    std::num::TryFromIntError,
5};
6
7/// Errors that may be returned by the spl-pod library.
8#[repr(u32)]
9#[derive(
10    Debug,
11    Clone,
12    PartialEq,
13    Eq,
14    thiserror::Error,
15    num_enum::TryFromPrimitive,
16    num_derive::FromPrimitive,
17)]
18pub enum PodSliceError {
19    /// Error in checked math operation
20    #[error("Error in checked math operation")]
21    CalculationFailure,
22    /// Provided byte buffer too small for expected type
23    #[error("Provided byte buffer too small for expected type")]
24    BufferTooSmall,
25    /// Provided byte buffer too large for expected type
26    #[error("Provided byte buffer too large for expected type")]
27    BufferTooLarge,
28    /// An integer conversion failed because the value was out of range for the target type
29    #[error("An integer conversion failed because the value was out of range for the target type")]
30    ValueOutOfRange,
31}
32
33impl From<PodSliceError> for ProgramError {
34    fn from(e: PodSliceError) -> Self {
35        ProgramError::Custom(e as u32)
36    }
37}
38
39impl ToStr for PodSliceError {
40    fn to_str(&self) -> &'static str {
41        match self {
42            PodSliceError::CalculationFailure => "Error in checked math operation",
43            PodSliceError::BufferTooSmall => "Provided byte buffer too small for expected type",
44            PodSliceError::BufferTooLarge => "Provided byte buffer too large for expected type",
45            PodSliceError::ValueOutOfRange => "An integer conversion failed because the value was out of range for the target type"
46        }
47    }
48}
49
50impl From<TryFromIntError> for PodSliceError {
51    fn from(_: TryFromIntError) -> Self {
52        PodSliceError::ValueOutOfRange
53    }
54}