Skip to main content

stellar_xdr/generated/
trust_line_asset.rs

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