Skip to main content

stellar_xdr/generated/
sc_spec_entry.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// ScSpecEntry is an XDR Union defined as:
5///
6/// ```text
7/// union SCSpecEntry switch (SCSpecEntryKind kind)
8/// {
9/// case SC_SPEC_ENTRY_FUNCTION_V0:
10///     SCSpecFunctionV0 functionV0;
11/// case SC_SPEC_ENTRY_UDT_STRUCT_V0:
12///     SCSpecUDTStructV0 udtStructV0;
13/// case SC_SPEC_ENTRY_UDT_UNION_V0:
14///     SCSpecUDTUnionV0 udtUnionV0;
15/// case SC_SPEC_ENTRY_UDT_ENUM_V0:
16///     SCSpecUDTEnumV0 udtEnumV0;
17/// case SC_SPEC_ENTRY_UDT_ERROR_ENUM_V0:
18///     SCSpecUDTErrorEnumV0 udtErrorEnumV0;
19/// case SC_SPEC_ENTRY_EVENT_V0:
20///     SCSpecEventV0 eventV0;
21/// };
22/// ```
23///
24// union with discriminant ScSpecEntryKind
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 ScSpecEntry {
37    FunctionV0(ScSpecFunctionV0),
38    UdtStructV0(ScSpecUdtStructV0),
39    UdtUnionV0(ScSpecUdtUnionV0),
40    UdtEnumV0(ScSpecUdtEnumV0),
41    UdtErrorEnumV0(ScSpecUdtErrorEnumV0),
42    EventV0(ScSpecEventV0),
43}
44
45#[cfg(feature = "alloc")]
46impl Default for ScSpecEntry {
47    fn default() -> Self {
48        Self::FunctionV0(ScSpecFunctionV0::default())
49    }
50}
51
52impl ScSpecEntry {
53    const _VARIANTS: &[ScSpecEntryKind] = &[
54        ScSpecEntryKind::FunctionV0,
55        ScSpecEntryKind::UdtStructV0,
56        ScSpecEntryKind::UdtUnionV0,
57        ScSpecEntryKind::UdtEnumV0,
58        ScSpecEntryKind::UdtErrorEnumV0,
59        ScSpecEntryKind::EventV0,
60    ];
61    pub const VARIANTS: [ScSpecEntryKind; Self::_VARIANTS.len()] = {
62        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
63        let mut i = 1;
64        while i < Self::_VARIANTS.len() {
65            arr[i] = Self::_VARIANTS[i];
66            i += 1;
67        }
68        arr
69    };
70    const _VARIANTS_STR: &[&str] = &[
71        "FunctionV0",
72        "UdtStructV0",
73        "UdtUnionV0",
74        "UdtEnumV0",
75        "UdtErrorEnumV0",
76        "EventV0",
77    ];
78    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
79        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
80        let mut i = 1;
81        while i < Self::_VARIANTS_STR.len() {
82            arr[i] = Self::_VARIANTS_STR[i];
83            i += 1;
84        }
85        arr
86    };
87
88    #[must_use]
89    pub const fn name(&self) -> &'static str {
90        match self {
91            Self::FunctionV0(_) => "FunctionV0",
92            Self::UdtStructV0(_) => "UdtStructV0",
93            Self::UdtUnionV0(_) => "UdtUnionV0",
94            Self::UdtEnumV0(_) => "UdtEnumV0",
95            Self::UdtErrorEnumV0(_) => "UdtErrorEnumV0",
96            Self::EventV0(_) => "EventV0",
97        }
98    }
99
100    #[must_use]
101    pub const fn discriminant(&self) -> ScSpecEntryKind {
102        #[allow(clippy::match_same_arms)]
103        match self {
104            Self::FunctionV0(_) => ScSpecEntryKind::FunctionV0,
105            Self::UdtStructV0(_) => ScSpecEntryKind::UdtStructV0,
106            Self::UdtUnionV0(_) => ScSpecEntryKind::UdtUnionV0,
107            Self::UdtEnumV0(_) => ScSpecEntryKind::UdtEnumV0,
108            Self::UdtErrorEnumV0(_) => ScSpecEntryKind::UdtErrorEnumV0,
109            Self::EventV0(_) => ScSpecEntryKind::EventV0,
110        }
111    }
112
113    #[must_use]
114    pub const fn variants() -> [ScSpecEntryKind; Self::_VARIANTS.len()] {
115        Self::VARIANTS
116    }
117}
118
119impl Name for ScSpecEntry {
120    #[must_use]
121    fn name(&self) -> &'static str {
122        Self::name(self)
123    }
124}
125
126impl Discriminant<ScSpecEntryKind> for ScSpecEntry {
127    #[must_use]
128    fn discriminant(&self) -> ScSpecEntryKind {
129        Self::discriminant(self)
130    }
131}
132
133impl Variants<ScSpecEntryKind> for ScSpecEntry {
134    fn variants() -> slice::Iter<'static, ScSpecEntryKind> {
135        Self::VARIANTS.iter()
136    }
137}
138
139impl Union<ScSpecEntryKind> for ScSpecEntry {}
140
141impl ReadXdr for ScSpecEntry {
142    #[cfg(feature = "std")]
143    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
144        r.with_limited_depth(|r| {
145            let dv: ScSpecEntryKind = <ScSpecEntryKind as ReadXdr>::read_xdr(r)?;
146            #[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
147            let v = match dv {
148                ScSpecEntryKind::FunctionV0 => Self::FunctionV0(ScSpecFunctionV0::read_xdr(r)?),
149                ScSpecEntryKind::UdtStructV0 => Self::UdtStructV0(ScSpecUdtStructV0::read_xdr(r)?),
150                ScSpecEntryKind::UdtUnionV0 => Self::UdtUnionV0(ScSpecUdtUnionV0::read_xdr(r)?),
151                ScSpecEntryKind::UdtEnumV0 => Self::UdtEnumV0(ScSpecUdtEnumV0::read_xdr(r)?),
152                ScSpecEntryKind::UdtErrorEnumV0 => {
153                    Self::UdtErrorEnumV0(ScSpecUdtErrorEnumV0::read_xdr(r)?)
154                }
155                ScSpecEntryKind::EventV0 => Self::EventV0(ScSpecEventV0::read_xdr(r)?),
156                #[allow(unreachable_patterns)]
157                _ => return Err(Error::Invalid),
158            };
159            Ok(v)
160        })
161    }
162}
163
164impl WriteXdr for ScSpecEntry {
165    #[cfg(feature = "std")]
166    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
167        w.with_limited_depth(|w| {
168            self.discriminant().write_xdr(w)?;
169            #[allow(clippy::match_same_arms)]
170            match self {
171                Self::FunctionV0(v) => v.write_xdr(w)?,
172                Self::UdtStructV0(v) => v.write_xdr(w)?,
173                Self::UdtUnionV0(v) => v.write_xdr(w)?,
174                Self::UdtEnumV0(v) => v.write_xdr(w)?,
175                Self::UdtErrorEnumV0(v) => v.write_xdr(w)?,
176                Self::EventV0(v) => v.write_xdr(w)?,
177            };
178            Ok(())
179        })
180    }
181}