Skip to main content

stellar_xdr/generated/
asset.rs

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