Skip to main content

stellar_xdr/generated/
muxed_account.rs

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