solana_feature_gate_interface/
error.rs

1//! Program error types.
2
3use solana_program_error::{ProgramError, ToStr};
4
5/// Program error types.
6#[cfg_attr(test, derive(strum_macros::FromRepr, strum_macros::EnumIter))]
7#[cfg_attr(
8    feature = "serde",
9    derive(serde_derive::Deserialize, serde_derive::Serialize)
10)]
11#[derive(Clone, Debug, PartialEq, Eq)]
12#[repr(u32)]
13pub enum FeatureGateError {
14    /// Feature already activated
15    FeatureAlreadyActivated,
16}
17
18impl core::error::Error for FeatureGateError {}
19
20impl core::fmt::Display for FeatureGateError {
21    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
22        f.write_str(self.to_str())
23    }
24}
25
26impl ToStr for FeatureGateError {
27    fn to_str(&self) -> &'static str {
28        match self {
29            FeatureGateError::FeatureAlreadyActivated => "Feature already activated",
30        }
31    }
32}
33
34impl From<FeatureGateError> for ProgramError {
35    fn from(e: FeatureGateError) -> Self {
36        ProgramError::Custom(e as u32)
37    }
38}
39
40impl TryFrom<u32> for FeatureGateError {
41    type Error = ProgramError;
42    fn try_from(error: u32) -> Result<Self, Self::Error> {
43        match error {
44            0 => Ok(FeatureGateError::FeatureAlreadyActivated),
45            _ => Err(ProgramError::InvalidArgument),
46        }
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use {super::FeatureGateError, strum::IntoEnumIterator};
53
54    #[test]
55    fn test_system_error_from_primitive_exhaustive() {
56        for variant in FeatureGateError::iter() {
57            let variant_u32 = variant.clone() as u32;
58            assert_eq!(FeatureGateError::from_repr(variant_u32).unwrap(), variant);
59            assert_eq!(FeatureGateError::try_from(variant_u32).unwrap(), variant);
60        }
61    }
62}