Skip to main content

stellar_xdr/generated/
sc_address.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// ScAddress is an XDR Union defined as:
5///
6/// ```text
7/// union SCAddress switch (SCAddressType type)
8/// {
9/// case SC_ADDRESS_TYPE_ACCOUNT:
10///     AccountID accountId;
11/// case SC_ADDRESS_TYPE_CONTRACT:
12///     ContractID contractId;
13/// case SC_ADDRESS_TYPE_MUXED_ACCOUNT:
14///     MuxedEd25519Account muxedAccount;
15/// case SC_ADDRESS_TYPE_CLAIMABLE_BALANCE:
16///     ClaimableBalanceID claimableBalanceId;
17/// case SC_ADDRESS_TYPE_LIQUIDITY_POOL:
18///     PoolID liquidityPoolId;
19/// };
20/// ```
21///
22// union with discriminant ScAddressType
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    derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
29)]
30#[allow(clippy::large_enum_variant)]
31pub enum ScAddress {
32    Account(AccountId),
33    Contract(ContractId),
34    MuxedAccount(MuxedEd25519Account),
35    ClaimableBalance(ClaimableBalanceId),
36    LiquidityPool(PoolId),
37}
38
39#[cfg(feature = "alloc")]
40impl Default for ScAddress {
41    fn default() -> Self {
42        Self::Account(AccountId::default())
43    }
44}
45
46impl ScAddress {
47    const _VARIANTS: &[ScAddressType] = &[
48        ScAddressType::Account,
49        ScAddressType::Contract,
50        ScAddressType::MuxedAccount,
51        ScAddressType::ClaimableBalance,
52        ScAddressType::LiquidityPool,
53    ];
54    pub const VARIANTS: [ScAddressType; Self::_VARIANTS.len()] = {
55        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
56        let mut i = 1;
57        while i < Self::_VARIANTS.len() {
58            arr[i] = Self::_VARIANTS[i];
59            i += 1;
60        }
61        arr
62    };
63    const _VARIANTS_STR: &[&str] = &[
64        "Account",
65        "Contract",
66        "MuxedAccount",
67        "ClaimableBalance",
68        "LiquidityPool",
69    ];
70    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
71        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
72        let mut i = 1;
73        while i < Self::_VARIANTS_STR.len() {
74            arr[i] = Self::_VARIANTS_STR[i];
75            i += 1;
76        }
77        arr
78    };
79
80    #[must_use]
81    pub const fn name(&self) -> &'static str {
82        match self {
83            Self::Account(_) => "Account",
84            Self::Contract(_) => "Contract",
85            Self::MuxedAccount(_) => "MuxedAccount",
86            Self::ClaimableBalance(_) => "ClaimableBalance",
87            Self::LiquidityPool(_) => "LiquidityPool",
88        }
89    }
90
91    #[must_use]
92    pub const fn discriminant(&self) -> ScAddressType {
93        #[allow(clippy::match_same_arms)]
94        match self {
95            Self::Account(_) => ScAddressType::Account,
96            Self::Contract(_) => ScAddressType::Contract,
97            Self::MuxedAccount(_) => ScAddressType::MuxedAccount,
98            Self::ClaimableBalance(_) => ScAddressType::ClaimableBalance,
99            Self::LiquidityPool(_) => ScAddressType::LiquidityPool,
100        }
101    }
102
103    #[must_use]
104    pub const fn variants() -> [ScAddressType; Self::_VARIANTS.len()] {
105        Self::VARIANTS
106    }
107}
108
109impl Name for ScAddress {
110    #[must_use]
111    fn name(&self) -> &'static str {
112        Self::name(self)
113    }
114}
115
116impl Discriminant<ScAddressType> for ScAddress {
117    #[must_use]
118    fn discriminant(&self) -> ScAddressType {
119        Self::discriminant(self)
120    }
121}
122
123impl Variants<ScAddressType> for ScAddress {
124    fn variants() -> slice::Iter<'static, ScAddressType> {
125        Self::VARIANTS.iter()
126    }
127}
128
129impl Union<ScAddressType> for ScAddress {}
130
131impl ReadXdr for ScAddress {
132    #[cfg(feature = "std")]
133    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
134        r.with_limited_depth(|r| {
135            let dv: ScAddressType = <ScAddressType as ReadXdr>::read_xdr(r)?;
136            #[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
137            let v = match dv {
138                ScAddressType::Account => Self::Account(AccountId::read_xdr(r)?),
139                ScAddressType::Contract => Self::Contract(ContractId::read_xdr(r)?),
140                ScAddressType::MuxedAccount => {
141                    Self::MuxedAccount(MuxedEd25519Account::read_xdr(r)?)
142                }
143                ScAddressType::ClaimableBalance => {
144                    Self::ClaimableBalance(ClaimableBalanceId::read_xdr(r)?)
145                }
146                ScAddressType::LiquidityPool => Self::LiquidityPool(PoolId::read_xdr(r)?),
147                #[allow(unreachable_patterns)]
148                _ => return Err(Error::Invalid),
149            };
150            Ok(v)
151        })
152    }
153}
154
155impl WriteXdr for ScAddress {
156    #[cfg(feature = "std")]
157    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
158        w.with_limited_depth(|w| {
159            self.discriminant().write_xdr(w)?;
160            #[allow(clippy::match_same_arms)]
161            match self {
162                Self::Account(v) => v.write_xdr(w)?,
163                Self::Contract(v) => v.write_xdr(w)?,
164                Self::MuxedAccount(v) => v.write_xdr(w)?,
165                Self::ClaimableBalance(v) => v.write_xdr(w)?,
166                Self::LiquidityPool(v) => v.write_xdr(w)?,
167            };
168            Ok(())
169        })
170    }
171}