Skip to main content

stellar_xdr/generated/
sc_error_code.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// ScErrorCode is an XDR Enum defined as:
5///
6/// ```text
7/// enum SCErrorCode
8/// {
9///     SCEC_ARITH_DOMAIN = 0,      // Some arithmetic was undefined (overflow, divide-by-zero).
10///     SCEC_INDEX_BOUNDS = 1,      // Something was indexed beyond its bounds.
11///     SCEC_INVALID_INPUT = 2,     // User provided some otherwise-bad data.
12///     SCEC_MISSING_VALUE = 3,     // Some value was required but not provided.
13///     SCEC_EXISTING_VALUE = 4,    // Some value was provided where not allowed.
14///     SCEC_EXCEEDED_LIMIT = 5,    // Some arbitrary limit -- gas or otherwise -- was hit.
15///     SCEC_INVALID_ACTION = 6,    // Data was valid but action requested was not.
16///     SCEC_INTERNAL_ERROR = 7,    // The host detected an error in its own logic.
17///     SCEC_UNEXPECTED_TYPE = 8,   // Some type wasn't as expected.
18///     SCEC_UNEXPECTED_SIZE = 9    // Something's size wasn't as expected.
19/// };
20/// ```
21///
22// enum
23#[cfg_attr(feature = "alloc", derive(Default))]
24#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
25#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
26#[cfg_attr(
27    all(feature = "serde", feature = "alloc"),
28    derive(serde::Serialize, serde::Deserialize),
29    serde(rename_all = "snake_case")
30)]
31#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
32#[repr(i32)]
33pub enum ScErrorCode {
34    #[cfg_attr(feature = "alloc", default)]
35    ArithDomain = 0,
36    IndexBounds = 1,
37    InvalidInput = 2,
38    MissingValue = 3,
39    ExistingValue = 4,
40    ExceededLimit = 5,
41    InvalidAction = 6,
42    InternalError = 7,
43    UnexpectedType = 8,
44    UnexpectedSize = 9,
45}
46
47impl ScErrorCode {
48    const _VARIANTS: &[ScErrorCode] = &[
49        ScErrorCode::ArithDomain,
50        ScErrorCode::IndexBounds,
51        ScErrorCode::InvalidInput,
52        ScErrorCode::MissingValue,
53        ScErrorCode::ExistingValue,
54        ScErrorCode::ExceededLimit,
55        ScErrorCode::InvalidAction,
56        ScErrorCode::InternalError,
57        ScErrorCode::UnexpectedType,
58        ScErrorCode::UnexpectedSize,
59    ];
60    pub const VARIANTS: [ScErrorCode; Self::_VARIANTS.len()] = {
61        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
62        let mut i = 1;
63        while i < Self::_VARIANTS.len() {
64            arr[i] = Self::_VARIANTS[i];
65            i += 1;
66        }
67        arr
68    };
69    const _VARIANTS_STR: &[&str] = &[
70        "ArithDomain",
71        "IndexBounds",
72        "InvalidInput",
73        "MissingValue",
74        "ExistingValue",
75        "ExceededLimit",
76        "InvalidAction",
77        "InternalError",
78        "UnexpectedType",
79        "UnexpectedSize",
80    ];
81    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
82        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
83        let mut i = 1;
84        while i < Self::_VARIANTS_STR.len() {
85            arr[i] = Self::_VARIANTS_STR[i];
86            i += 1;
87        }
88        arr
89    };
90
91    #[must_use]
92    pub const fn name(&self) -> &'static str {
93        match self {
94            Self::ArithDomain => "ArithDomain",
95            Self::IndexBounds => "IndexBounds",
96            Self::InvalidInput => "InvalidInput",
97            Self::MissingValue => "MissingValue",
98            Self::ExistingValue => "ExistingValue",
99            Self::ExceededLimit => "ExceededLimit",
100            Self::InvalidAction => "InvalidAction",
101            Self::InternalError => "InternalError",
102            Self::UnexpectedType => "UnexpectedType",
103            Self::UnexpectedSize => "UnexpectedSize",
104        }
105    }
106
107    #[must_use]
108    pub const fn variants() -> [ScErrorCode; Self::_VARIANTS.len()] {
109        Self::VARIANTS
110    }
111}
112
113impl Name for ScErrorCode {
114    #[must_use]
115    fn name(&self) -> &'static str {
116        Self::name(self)
117    }
118}
119
120impl Variants<ScErrorCode> for ScErrorCode {
121    fn variants() -> slice::Iter<'static, ScErrorCode> {
122        Self::VARIANTS.iter()
123    }
124}
125
126impl Enum for ScErrorCode {}
127
128impl fmt::Display for ScErrorCode {
129    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130        f.write_str(self.name())
131    }
132}
133
134impl TryFrom<i32> for ScErrorCode {
135    type Error = Error;
136
137    fn try_from(i: i32) -> Result<Self, Error> {
138        let e = match i {
139            0 => ScErrorCode::ArithDomain,
140            1 => ScErrorCode::IndexBounds,
141            2 => ScErrorCode::InvalidInput,
142            3 => ScErrorCode::MissingValue,
143            4 => ScErrorCode::ExistingValue,
144            5 => ScErrorCode::ExceededLimit,
145            6 => ScErrorCode::InvalidAction,
146            7 => ScErrorCode::InternalError,
147            8 => ScErrorCode::UnexpectedType,
148            9 => ScErrorCode::UnexpectedSize,
149            #[allow(unreachable_patterns)]
150            _ => return Err(Error::Invalid),
151        };
152        Ok(e)
153    }
154}
155
156impl From<ScErrorCode> for i32 {
157    #[must_use]
158    fn from(e: ScErrorCode) -> Self {
159        e as Self
160    }
161}
162
163impl ReadXdr for ScErrorCode {
164    #[cfg(feature = "std")]
165    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
166        r.with_limited_depth(|r| {
167            let e = i32::read_xdr(r)?;
168            let v: Self = e.try_into()?;
169            Ok(v)
170        })
171    }
172}
173
174impl WriteXdr for ScErrorCode {
175    #[cfg(feature = "std")]
176    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
177        w.with_limited_depth(|w| {
178            let i: i32 = (*self).into();
179            i.write_xdr(w)
180        })
181    }
182}