Skip to main content

stellar_xdr/generated/
contract_executable.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// ContractExecutable is an XDR Union defined as:
5///
6/// ```text
7/// union ContractExecutable switch (ContractExecutableType type)
8/// {
9/// case CONTRACT_EXECUTABLE_WASM:
10///     Hash wasm_hash;
11/// case CONTRACT_EXECUTABLE_STELLAR_ASSET:
12///     void;
13/// };
14/// ```
15///
16// union with discriminant ContractExecutableType
17#[cfg_attr(feature = "serde", cfg_eval::cfg_eval)]
18#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
19#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
20#[cfg_attr(
21    all(feature = "serde", feature = "alloc"),
22    serde_with::serde_as,
23    derive(serde::Serialize, serde::Deserialize),
24    serde(rename_all = "snake_case")
25)]
26#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
27#[allow(clippy::large_enum_variant)]
28pub enum ContractExecutable {
29    Wasm(Hash),
30    StellarAsset,
31}
32
33#[cfg(feature = "alloc")]
34impl Default for ContractExecutable {
35    fn default() -> Self {
36        Self::Wasm(Hash::default())
37    }
38}
39
40impl ContractExecutable {
41    const _VARIANTS: &[ContractExecutableType] = &[
42        ContractExecutableType::Wasm,
43        ContractExecutableType::StellarAsset,
44    ];
45    pub const VARIANTS: [ContractExecutableType; 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] = &["Wasm", "StellarAsset"];
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::Wasm(_) => "Wasm",
69            Self::StellarAsset => "StellarAsset",
70        }
71    }
72
73    #[must_use]
74    pub const fn discriminant(&self) -> ContractExecutableType {
75        #[allow(clippy::match_same_arms)]
76        match self {
77            Self::Wasm(_) => ContractExecutableType::Wasm,
78            Self::StellarAsset => ContractExecutableType::StellarAsset,
79        }
80    }
81
82    #[must_use]
83    pub const fn variants() -> [ContractExecutableType; Self::_VARIANTS.len()] {
84        Self::VARIANTS
85    }
86}
87
88impl Name for ContractExecutable {
89    #[must_use]
90    fn name(&self) -> &'static str {
91        Self::name(self)
92    }
93}
94
95impl Discriminant<ContractExecutableType> for ContractExecutable {
96    #[must_use]
97    fn discriminant(&self) -> ContractExecutableType {
98        Self::discriminant(self)
99    }
100}
101
102impl Variants<ContractExecutableType> for ContractExecutable {
103    fn variants() -> slice::Iter<'static, ContractExecutableType> {
104        Self::VARIANTS.iter()
105    }
106}
107
108impl Union<ContractExecutableType> for ContractExecutable {}
109
110impl ReadXdr for ContractExecutable {
111    #[cfg(feature = "std")]
112    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
113        r.with_limited_depth(|r| {
114            let dv: ContractExecutableType = <ContractExecutableType as ReadXdr>::read_xdr(r)?;
115            #[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
116            let v = match dv {
117                ContractExecutableType::Wasm => Self::Wasm(Hash::read_xdr(r)?),
118                ContractExecutableType::StellarAsset => Self::StellarAsset,
119                #[allow(unreachable_patterns)]
120                _ => return Err(Error::Invalid),
121            };
122            Ok(v)
123        })
124    }
125}
126
127impl WriteXdr for ContractExecutable {
128    #[cfg(feature = "std")]
129    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
130        w.with_limited_depth(|w| {
131            self.discriminant().write_xdr(w)?;
132            #[allow(clippy::match_same_arms)]
133            match self {
134                Self::Wasm(v) => v.write_xdr(w)?,
135                Self::StellarAsset => ().write_xdr(w)?,
136            };
137            Ok(())
138        })
139    }
140}