Skip to main content

stellar_xdr/generated/
sc_error.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// ScError is an XDR Union defined as:
5///
6/// ```text
7/// union SCError switch (SCErrorType type)
8/// {
9/// case SCE_CONTRACT:
10///     uint32 contractCode;
11/// case SCE_WASM_VM:
12/// case SCE_CONTEXT:
13/// case SCE_STORAGE:
14/// case SCE_OBJECT:
15/// case SCE_CRYPTO:
16/// case SCE_EVENTS:
17/// case SCE_BUDGET:
18/// case SCE_VALUE:
19/// case SCE_AUTH:
20///     SCErrorCode code;
21/// };
22/// ```
23///
24// union with discriminant ScErrorType
25#[cfg_attr(feature = "serde", cfg_eval::cfg_eval)]
26#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
27#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
28#[cfg_attr(
29    all(feature = "serde", feature = "alloc"),
30    serde_with::serde_as,
31    derive(serde::Serialize, serde::Deserialize),
32    serde(rename_all = "snake_case")
33)]
34#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
35#[allow(clippy::large_enum_variant)]
36pub enum ScError {
37    Contract(u32),
38    WasmVm(ScErrorCode),
39    Context(ScErrorCode),
40    Storage(ScErrorCode),
41    Object(ScErrorCode),
42    Crypto(ScErrorCode),
43    Events(ScErrorCode),
44    Budget(ScErrorCode),
45    Value(ScErrorCode),
46    Auth(ScErrorCode),
47}
48
49#[cfg(feature = "alloc")]
50impl Default for ScError {
51    fn default() -> Self {
52        Self::Contract(u32::default())
53    }
54}
55
56impl ScError {
57    const _VARIANTS: &[ScErrorType] = &[
58        ScErrorType::Contract,
59        ScErrorType::WasmVm,
60        ScErrorType::Context,
61        ScErrorType::Storage,
62        ScErrorType::Object,
63        ScErrorType::Crypto,
64        ScErrorType::Events,
65        ScErrorType::Budget,
66        ScErrorType::Value,
67        ScErrorType::Auth,
68    ];
69    pub const VARIANTS: [ScErrorType; Self::_VARIANTS.len()] = {
70        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
71        let mut i = 1;
72        while i < Self::_VARIANTS.len() {
73            arr[i] = Self::_VARIANTS[i];
74            i += 1;
75        }
76        arr
77    };
78    const _VARIANTS_STR: &[&str] = &[
79        "Contract", "WasmVm", "Context", "Storage", "Object", "Crypto", "Events", "Budget",
80        "Value", "Auth",
81    ];
82    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
83        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
84        let mut i = 1;
85        while i < Self::_VARIANTS_STR.len() {
86            arr[i] = Self::_VARIANTS_STR[i];
87            i += 1;
88        }
89        arr
90    };
91
92    #[must_use]
93    pub const fn name(&self) -> &'static str {
94        match self {
95            Self::Contract(_) => "Contract",
96            Self::WasmVm(_) => "WasmVm",
97            Self::Context(_) => "Context",
98            Self::Storage(_) => "Storage",
99            Self::Object(_) => "Object",
100            Self::Crypto(_) => "Crypto",
101            Self::Events(_) => "Events",
102            Self::Budget(_) => "Budget",
103            Self::Value(_) => "Value",
104            Self::Auth(_) => "Auth",
105        }
106    }
107
108    #[must_use]
109    pub const fn discriminant(&self) -> ScErrorType {
110        #[allow(clippy::match_same_arms)]
111        match self {
112            Self::Contract(_) => ScErrorType::Contract,
113            Self::WasmVm(_) => ScErrorType::WasmVm,
114            Self::Context(_) => ScErrorType::Context,
115            Self::Storage(_) => ScErrorType::Storage,
116            Self::Object(_) => ScErrorType::Object,
117            Self::Crypto(_) => ScErrorType::Crypto,
118            Self::Events(_) => ScErrorType::Events,
119            Self::Budget(_) => ScErrorType::Budget,
120            Self::Value(_) => ScErrorType::Value,
121            Self::Auth(_) => ScErrorType::Auth,
122        }
123    }
124
125    #[must_use]
126    pub const fn variants() -> [ScErrorType; Self::_VARIANTS.len()] {
127        Self::VARIANTS
128    }
129}
130
131impl Name for ScError {
132    #[must_use]
133    fn name(&self) -> &'static str {
134        Self::name(self)
135    }
136}
137
138impl Discriminant<ScErrorType> for ScError {
139    #[must_use]
140    fn discriminant(&self) -> ScErrorType {
141        Self::discriminant(self)
142    }
143}
144
145impl Variants<ScErrorType> for ScError {
146    fn variants() -> slice::Iter<'static, ScErrorType> {
147        Self::VARIANTS.iter()
148    }
149}
150
151impl Union<ScErrorType> for ScError {}
152
153impl ReadXdr for ScError {
154    #[cfg(feature = "std")]
155    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
156        r.with_limited_depth(|r| {
157            let dv: ScErrorType = <ScErrorType as ReadXdr>::read_xdr(r)?;
158            #[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
159            let v = match dv {
160                ScErrorType::Contract => Self::Contract(u32::read_xdr(r)?),
161                ScErrorType::WasmVm => Self::WasmVm(ScErrorCode::read_xdr(r)?),
162                ScErrorType::Context => Self::Context(ScErrorCode::read_xdr(r)?),
163                ScErrorType::Storage => Self::Storage(ScErrorCode::read_xdr(r)?),
164                ScErrorType::Object => Self::Object(ScErrorCode::read_xdr(r)?),
165                ScErrorType::Crypto => Self::Crypto(ScErrorCode::read_xdr(r)?),
166                ScErrorType::Events => Self::Events(ScErrorCode::read_xdr(r)?),
167                ScErrorType::Budget => Self::Budget(ScErrorCode::read_xdr(r)?),
168                ScErrorType::Value => Self::Value(ScErrorCode::read_xdr(r)?),
169                ScErrorType::Auth => Self::Auth(ScErrorCode::read_xdr(r)?),
170                #[allow(unreachable_patterns)]
171                _ => return Err(Error::Invalid),
172            };
173            Ok(v)
174        })
175    }
176}
177
178impl WriteXdr for ScError {
179    #[cfg(feature = "std")]
180    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
181        w.with_limited_depth(|w| {
182            self.discriminant().write_xdr(w)?;
183            #[allow(clippy::match_same_arms)]
184            match self {
185                Self::Contract(v) => v.write_xdr(w)?,
186                Self::WasmVm(v) => v.write_xdr(w)?,
187                Self::Context(v) => v.write_xdr(w)?,
188                Self::Storage(v) => v.write_xdr(w)?,
189                Self::Object(v) => v.write_xdr(w)?,
190                Self::Crypto(v) => v.write_xdr(w)?,
191                Self::Events(v) => v.write_xdr(w)?,
192                Self::Budget(v) => v.write_xdr(w)?,
193                Self::Value(v) => v.write_xdr(w)?,
194                Self::Auth(v) => v.write_xdr(w)?,
195            };
196            Ok(())
197        })
198    }
199}