Skip to main content

stellar_xdr/generated/
asset_code.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// AssetCode is an XDR Union defined as:
5///
6/// ```text
7/// union AssetCode switch (AssetType type)
8/// {
9/// case ASSET_TYPE_CREDIT_ALPHANUM4:
10///     AssetCode4 assetCode4;
11///
12/// case ASSET_TYPE_CREDIT_ALPHANUM12:
13///     AssetCode12 assetCode12;
14///
15///     // add other asset types here in the future
16/// };
17/// ```
18///
19// union with discriminant AssetType
20#[cfg_attr(feature = "serde", cfg_eval::cfg_eval)]
21#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
22#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
23#[cfg_attr(
24    all(feature = "serde", feature = "alloc"),
25    derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
26)]
27#[allow(clippy::large_enum_variant)]
28pub enum AssetCode {
29    CreditAlphanum4(AssetCode4),
30    CreditAlphanum12(AssetCode12),
31}
32
33#[cfg(feature = "alloc")]
34impl Default for AssetCode {
35    fn default() -> Self {
36        Self::CreditAlphanum4(AssetCode4::default())
37    }
38}
39
40impl AssetCode {
41    const _VARIANTS: &[AssetType] = &[AssetType::CreditAlphanum4, AssetType::CreditAlphanum12];
42    pub const VARIANTS: [AssetType; Self::_VARIANTS.len()] = {
43        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
44        let mut i = 1;
45        while i < Self::_VARIANTS.len() {
46            arr[i] = Self::_VARIANTS[i];
47            i += 1;
48        }
49        arr
50    };
51    const _VARIANTS_STR: &[&str] = &["CreditAlphanum4", "CreditAlphanum12"];
52    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
53        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
54        let mut i = 1;
55        while i < Self::_VARIANTS_STR.len() {
56            arr[i] = Self::_VARIANTS_STR[i];
57            i += 1;
58        }
59        arr
60    };
61
62    #[must_use]
63    pub const fn name(&self) -> &'static str {
64        match self {
65            Self::CreditAlphanum4(_) => "CreditAlphanum4",
66            Self::CreditAlphanum12(_) => "CreditAlphanum12",
67        }
68    }
69
70    #[must_use]
71    pub const fn discriminant(&self) -> AssetType {
72        #[allow(clippy::match_same_arms)]
73        match self {
74            Self::CreditAlphanum4(_) => AssetType::CreditAlphanum4,
75            Self::CreditAlphanum12(_) => AssetType::CreditAlphanum12,
76        }
77    }
78
79    #[must_use]
80    pub const fn variants() -> [AssetType; Self::_VARIANTS.len()] {
81        Self::VARIANTS
82    }
83}
84
85impl Name for AssetCode {
86    #[must_use]
87    fn name(&self) -> &'static str {
88        Self::name(self)
89    }
90}
91
92impl Discriminant<AssetType> for AssetCode {
93    #[must_use]
94    fn discriminant(&self) -> AssetType {
95        Self::discriminant(self)
96    }
97}
98
99impl Variants<AssetType> for AssetCode {
100    fn variants() -> slice::Iter<'static, AssetType> {
101        Self::VARIANTS.iter()
102    }
103}
104
105impl Union<AssetType> for AssetCode {}
106
107impl ReadXdr for AssetCode {
108    #[cfg(feature = "std")]
109    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
110        r.with_limited_depth(|r| {
111            let dv: AssetType = <AssetType as ReadXdr>::read_xdr(r)?;
112            #[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
113            let v = match dv {
114                AssetType::CreditAlphanum4 => Self::CreditAlphanum4(AssetCode4::read_xdr(r)?),
115                AssetType::CreditAlphanum12 => Self::CreditAlphanum12(AssetCode12::read_xdr(r)?),
116                #[allow(unreachable_patterns)]
117                _ => return Err(Error::Invalid),
118            };
119            Ok(v)
120        })
121    }
122}
123
124impl WriteXdr for AssetCode {
125    #[cfg(feature = "std")]
126    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
127        w.with_limited_depth(|w| {
128            self.discriminant().write_xdr(w)?;
129            #[allow(clippy::match_same_arms)]
130            match self {
131                Self::CreditAlphanum4(v) => v.write_xdr(w)?,
132                Self::CreditAlphanum12(v) => v.write_xdr(w)?,
133            };
134            Ok(())
135        })
136    }
137}