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/// case CONTRACT_EXECUTABLE_EXTERNAL_REF:
14///     ContractExecutableExternalRef external_ref;
15/// };
16/// ```
17///
18// union with discriminant ContractExecutableType
19#[cfg_attr(feature = "serde", cfg_eval::cfg_eval)]
20#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
21#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
22#[cfg_attr(
23    all(feature = "serde", feature = "alloc"),
24    serde_with::serde_as,
25    derive(serde::Serialize, serde::Deserialize),
26    serde(rename_all = "snake_case")
27)]
28#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
29#[allow(clippy::large_enum_variant)]
30pub enum ContractExecutable {
31    Wasm(Hash),
32    StellarAsset,
33    ExternalRef(ContractExecutableExternalRef),
34}
35
36#[cfg(feature = "alloc")]
37impl Default for ContractExecutable {
38    fn default() -> Self {
39        Self::Wasm(Hash::default())
40    }
41}
42
43impl ContractExecutable {
44    const _VARIANTS: &[ContractExecutableType] = &[
45        ContractExecutableType::Wasm,
46        ContractExecutableType::StellarAsset,
47        ContractExecutableType::ExternalRef,
48    ];
49    pub const VARIANTS: [ContractExecutableType; Self::_VARIANTS.len()] = {
50        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
51        let mut i = 1;
52        while i < Self::_VARIANTS.len() {
53            arr[i] = Self::_VARIANTS[i];
54            i += 1;
55        }
56        arr
57    };
58    const _VARIANTS_STR: &[&str] = &["Wasm", "StellarAsset", "ExternalRef"];
59    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
60        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
61        let mut i = 1;
62        while i < Self::_VARIANTS_STR.len() {
63            arr[i] = Self::_VARIANTS_STR[i];
64            i += 1;
65        }
66        arr
67    };
68
69    #[must_use]
70    pub const fn name(&self) -> &'static str {
71        match self {
72            Self::Wasm(_) => "Wasm",
73            Self::StellarAsset => "StellarAsset",
74            Self::ExternalRef(_) => "ExternalRef",
75        }
76    }
77
78    #[must_use]
79    pub const fn discriminant(&self) -> ContractExecutableType {
80        #[allow(clippy::match_same_arms)]
81        match self {
82            Self::Wasm(_) => ContractExecutableType::Wasm,
83            Self::StellarAsset => ContractExecutableType::StellarAsset,
84            Self::ExternalRef(_) => ContractExecutableType::ExternalRef,
85        }
86    }
87
88    #[must_use]
89    pub const fn variants() -> [ContractExecutableType; Self::_VARIANTS.len()] {
90        Self::VARIANTS
91    }
92}
93
94impl Name for ContractExecutable {
95    #[must_use]
96    fn name(&self) -> &'static str {
97        Self::name(self)
98    }
99}
100
101impl Discriminant<ContractExecutableType> for ContractExecutable {
102    #[must_use]
103    fn discriminant(&self) -> ContractExecutableType {
104        Self::discriminant(self)
105    }
106}
107
108impl Variants<ContractExecutableType> for ContractExecutable {
109    fn variants() -> slice::Iter<'static, ContractExecutableType> {
110        Self::VARIANTS.iter()
111    }
112}
113
114impl Union<ContractExecutableType> for ContractExecutable {}
115
116impl ReadXdr for ContractExecutable {
117    #[cfg(feature = "std")]
118    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
119        r.with_limited_depth(|r| {
120            let dv: ContractExecutableType = <ContractExecutableType as ReadXdr>::read_xdr(r)?;
121            #[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
122            let v = match dv {
123                ContractExecutableType::Wasm => Self::Wasm(Hash::read_xdr(r)?),
124                ContractExecutableType::StellarAsset => Self::StellarAsset,
125                ContractExecutableType::ExternalRef => {
126                    Self::ExternalRef(ContractExecutableExternalRef::read_xdr(r)?)
127                }
128                #[allow(unreachable_patterns)]
129                _ => return Err(Error::Invalid),
130            };
131            Ok(v)
132        })
133    }
134}
135
136impl WriteXdr for ContractExecutable {
137    #[cfg(feature = "std")]
138    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
139        w.with_limited_depth(|w| {
140            self.discriminant().write_xdr(w)?;
141            #[allow(clippy::match_same_arms)]
142            match self {
143                Self::Wasm(v) => v.write_xdr(w)?,
144                Self::StellarAsset => ().write_xdr(w)?,
145                Self::ExternalRef(v) => v.write_xdr(w)?,
146            };
147            Ok(())
148        })
149    }
150}