Skip to main content

stellar_xdr/generated/
error_code.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// ErrorCode is an XDR Enum defined as:
5///
6/// ```text
7/// enum ErrorCode
8/// {
9///     ERR_MISC = 0, // Unspecific error
10///     ERR_DATA = 1, // Malformed data
11///     ERR_CONF = 2, // Misconfiguration error
12///     ERR_AUTH = 3, // Authentication failure
13///     ERR_LOAD = 4  // System overloaded
14/// };
15/// ```
16///
17// enum
18#[cfg_attr(feature = "alloc", derive(Default))]
19#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
20#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
21#[cfg_attr(
22    all(feature = "serde", feature = "alloc"),
23    derive(serde::Serialize, serde::Deserialize),
24    serde(rename_all = "snake_case")
25)]
26#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
27#[repr(i32)]
28pub enum ErrorCode {
29    #[cfg_attr(feature = "alloc", default)]
30    Misc = 0,
31    Data = 1,
32    Conf = 2,
33    Auth = 3,
34    Load = 4,
35}
36
37impl ErrorCode {
38    const _VARIANTS: &[ErrorCode] = &[
39        ErrorCode::Misc,
40        ErrorCode::Data,
41        ErrorCode::Conf,
42        ErrorCode::Auth,
43        ErrorCode::Load,
44    ];
45    pub const VARIANTS: [ErrorCode; Self::_VARIANTS.len()] = {
46        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
47        let mut i = 1;
48        while i < Self::_VARIANTS.len() {
49            arr[i] = Self::_VARIANTS[i];
50            i += 1;
51        }
52        arr
53    };
54    const _VARIANTS_STR: &[&str] = &["Misc", "Data", "Conf", "Auth", "Load"];
55    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
56        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
57        let mut i = 1;
58        while i < Self::_VARIANTS_STR.len() {
59            arr[i] = Self::_VARIANTS_STR[i];
60            i += 1;
61        }
62        arr
63    };
64
65    #[must_use]
66    pub const fn name(&self) -> &'static str {
67        match self {
68            Self::Misc => "Misc",
69            Self::Data => "Data",
70            Self::Conf => "Conf",
71            Self::Auth => "Auth",
72            Self::Load => "Load",
73        }
74    }
75
76    #[must_use]
77    pub const fn variants() -> [ErrorCode; Self::_VARIANTS.len()] {
78        Self::VARIANTS
79    }
80}
81
82impl Name for ErrorCode {
83    #[must_use]
84    fn name(&self) -> &'static str {
85        Self::name(self)
86    }
87}
88
89impl Variants<ErrorCode> for ErrorCode {
90    fn variants() -> slice::Iter<'static, ErrorCode> {
91        Self::VARIANTS.iter()
92    }
93}
94
95impl Enum for ErrorCode {}
96
97impl fmt::Display for ErrorCode {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        f.write_str(self.name())
100    }
101}
102
103impl TryFrom<i32> for ErrorCode {
104    type Error = Error;
105
106    fn try_from(i: i32) -> Result<Self, Error> {
107        let e = match i {
108            0 => ErrorCode::Misc,
109            1 => ErrorCode::Data,
110            2 => ErrorCode::Conf,
111            3 => ErrorCode::Auth,
112            4 => ErrorCode::Load,
113            #[allow(unreachable_patterns)]
114            _ => return Err(Error::Invalid),
115        };
116        Ok(e)
117    }
118}
119
120impl From<ErrorCode> for i32 {
121    #[must_use]
122    fn from(e: ErrorCode) -> Self {
123        e as Self
124    }
125}
126
127impl ReadXdr for ErrorCode {
128    #[cfg(feature = "std")]
129    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
130        r.with_limited_depth(|r| {
131            let e = i32::read_xdr(r)?;
132            let v: Self = e.try_into()?;
133            Ok(v)
134        })
135    }
136}
137
138impl WriteXdr for ErrorCode {
139    #[cfg(feature = "std")]
140    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
141        w.with_limited_depth(|w| {
142            let i: i32 = (*self).into();
143            i.write_xdr(w)
144        })
145    }
146}