Skip to main content

stellar_xdr/generated/
contract_id_preimage.rs

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