Skip to main content

stellar_xdr/generated/
ledger_key.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// LedgerKey is an XDR Union defined as:
5///
6/// ```text
7/// union LedgerKey switch (LedgerEntryType type)
8/// {
9/// case ACCOUNT:
10///     struct
11///     {
12///         AccountID accountID;
13///     } account;
14///
15/// case TRUSTLINE:
16///     struct
17///     {
18///         AccountID accountID;
19///         TrustLineAsset asset;
20///     } trustLine;
21///
22/// case OFFER:
23///     struct
24///     {
25///         AccountID sellerID;
26///         int64 offerID;
27///     } offer;
28///
29/// case DATA:
30///     struct
31///     {
32///         AccountID accountID;
33///         string64 dataName;
34///     } data;
35///
36/// case CLAIMABLE_BALANCE:
37///     struct
38///     {
39///         ClaimableBalanceID balanceID;
40///     } claimableBalance;
41///
42/// case LIQUIDITY_POOL:
43///     struct
44///     {
45///         PoolID liquidityPoolID;
46///     } liquidityPool;
47/// case CONTRACT_DATA:
48///     struct
49///     {
50///         SCAddress contract;
51///         SCVal key;
52///         ContractDataDurability durability;
53///     } contractData;
54/// case CONTRACT_CODE:
55///     struct
56///     {
57///         Hash hash;
58///     } contractCode;
59/// case CONFIG_SETTING:
60///     struct
61///     {
62///         ConfigSettingID configSettingID;
63///     } configSetting;
64/// case TTL:
65///     struct
66///     {
67///         // Hash of the LedgerKey that is associated with this TTLEntry
68///         Hash keyHash;
69///     } ttl;
70/// };
71/// ```
72///
73// union with discriminant LedgerEntryType
74#[cfg_attr(feature = "serde", cfg_eval::cfg_eval)]
75#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
76#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
77#[cfg_attr(
78    all(feature = "serde", feature = "alloc"),
79    serde_with::serde_as,
80    derive(serde::Serialize, serde::Deserialize),
81    serde(rename_all = "snake_case")
82)]
83#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
84#[allow(clippy::large_enum_variant)]
85pub enum LedgerKey {
86    Account(LedgerKeyAccount),
87    Trustline(LedgerKeyTrustLine),
88    Offer(LedgerKeyOffer),
89    Data(LedgerKeyData),
90    ClaimableBalance(LedgerKeyClaimableBalance),
91    LiquidityPool(LedgerKeyLiquidityPool),
92    ContractData(LedgerKeyContractData),
93    ContractCode(LedgerKeyContractCode),
94    ConfigSetting(LedgerKeyConfigSetting),
95    Ttl(LedgerKeyTtl),
96}
97
98#[cfg(feature = "alloc")]
99impl Default for LedgerKey {
100    fn default() -> Self {
101        Self::Account(LedgerKeyAccount::default())
102    }
103}
104
105impl LedgerKey {
106    const _VARIANTS: &[LedgerEntryType] = &[
107        LedgerEntryType::Account,
108        LedgerEntryType::Trustline,
109        LedgerEntryType::Offer,
110        LedgerEntryType::Data,
111        LedgerEntryType::ClaimableBalance,
112        LedgerEntryType::LiquidityPool,
113        LedgerEntryType::ContractData,
114        LedgerEntryType::ContractCode,
115        LedgerEntryType::ConfigSetting,
116        LedgerEntryType::Ttl,
117    ];
118    pub const VARIANTS: [LedgerEntryType; Self::_VARIANTS.len()] = {
119        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
120        let mut i = 1;
121        while i < Self::_VARIANTS.len() {
122            arr[i] = Self::_VARIANTS[i];
123            i += 1;
124        }
125        arr
126    };
127    const _VARIANTS_STR: &[&str] = &[
128        "Account",
129        "Trustline",
130        "Offer",
131        "Data",
132        "ClaimableBalance",
133        "LiquidityPool",
134        "ContractData",
135        "ContractCode",
136        "ConfigSetting",
137        "Ttl",
138    ];
139    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
140        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
141        let mut i = 1;
142        while i < Self::_VARIANTS_STR.len() {
143            arr[i] = Self::_VARIANTS_STR[i];
144            i += 1;
145        }
146        arr
147    };
148
149    #[must_use]
150    pub const fn name(&self) -> &'static str {
151        match self {
152            Self::Account(_) => "Account",
153            Self::Trustline(_) => "Trustline",
154            Self::Offer(_) => "Offer",
155            Self::Data(_) => "Data",
156            Self::ClaimableBalance(_) => "ClaimableBalance",
157            Self::LiquidityPool(_) => "LiquidityPool",
158            Self::ContractData(_) => "ContractData",
159            Self::ContractCode(_) => "ContractCode",
160            Self::ConfigSetting(_) => "ConfigSetting",
161            Self::Ttl(_) => "Ttl",
162        }
163    }
164
165    #[must_use]
166    pub const fn discriminant(&self) -> LedgerEntryType {
167        #[allow(clippy::match_same_arms)]
168        match self {
169            Self::Account(_) => LedgerEntryType::Account,
170            Self::Trustline(_) => LedgerEntryType::Trustline,
171            Self::Offer(_) => LedgerEntryType::Offer,
172            Self::Data(_) => LedgerEntryType::Data,
173            Self::ClaimableBalance(_) => LedgerEntryType::ClaimableBalance,
174            Self::LiquidityPool(_) => LedgerEntryType::LiquidityPool,
175            Self::ContractData(_) => LedgerEntryType::ContractData,
176            Self::ContractCode(_) => LedgerEntryType::ContractCode,
177            Self::ConfigSetting(_) => LedgerEntryType::ConfigSetting,
178            Self::Ttl(_) => LedgerEntryType::Ttl,
179        }
180    }
181
182    #[must_use]
183    pub const fn variants() -> [LedgerEntryType; Self::_VARIANTS.len()] {
184        Self::VARIANTS
185    }
186}
187
188impl Name for LedgerKey {
189    #[must_use]
190    fn name(&self) -> &'static str {
191        Self::name(self)
192    }
193}
194
195impl Discriminant<LedgerEntryType> for LedgerKey {
196    #[must_use]
197    fn discriminant(&self) -> LedgerEntryType {
198        Self::discriminant(self)
199    }
200}
201
202impl Variants<LedgerEntryType> for LedgerKey {
203    fn variants() -> slice::Iter<'static, LedgerEntryType> {
204        Self::VARIANTS.iter()
205    }
206}
207
208impl Union<LedgerEntryType> for LedgerKey {}
209
210impl ReadXdr for LedgerKey {
211    #[cfg(feature = "std")]
212    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
213        r.with_limited_depth(|r| {
214            let dv: LedgerEntryType = <LedgerEntryType as ReadXdr>::read_xdr(r)?;
215            #[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
216            let v = match dv {
217                LedgerEntryType::Account => Self::Account(LedgerKeyAccount::read_xdr(r)?),
218                LedgerEntryType::Trustline => Self::Trustline(LedgerKeyTrustLine::read_xdr(r)?),
219                LedgerEntryType::Offer => Self::Offer(LedgerKeyOffer::read_xdr(r)?),
220                LedgerEntryType::Data => Self::Data(LedgerKeyData::read_xdr(r)?),
221                LedgerEntryType::ClaimableBalance => {
222                    Self::ClaimableBalance(LedgerKeyClaimableBalance::read_xdr(r)?)
223                }
224                LedgerEntryType::LiquidityPool => {
225                    Self::LiquidityPool(LedgerKeyLiquidityPool::read_xdr(r)?)
226                }
227                LedgerEntryType::ContractData => {
228                    Self::ContractData(LedgerKeyContractData::read_xdr(r)?)
229                }
230                LedgerEntryType::ContractCode => {
231                    Self::ContractCode(LedgerKeyContractCode::read_xdr(r)?)
232                }
233                LedgerEntryType::ConfigSetting => {
234                    Self::ConfigSetting(LedgerKeyConfigSetting::read_xdr(r)?)
235                }
236                LedgerEntryType::Ttl => Self::Ttl(LedgerKeyTtl::read_xdr(r)?),
237                #[allow(unreachable_patterns)]
238                _ => return Err(Error::Invalid),
239            };
240            Ok(v)
241        })
242    }
243}
244
245impl WriteXdr for LedgerKey {
246    #[cfg(feature = "std")]
247    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
248        w.with_limited_depth(|w| {
249            self.discriminant().write_xdr(w)?;
250            #[allow(clippy::match_same_arms)]
251            match self {
252                Self::Account(v) => v.write_xdr(w)?,
253                Self::Trustline(v) => v.write_xdr(w)?,
254                Self::Offer(v) => v.write_xdr(w)?,
255                Self::Data(v) => v.write_xdr(w)?,
256                Self::ClaimableBalance(v) => v.write_xdr(w)?,
257                Self::LiquidityPool(v) => v.write_xdr(w)?,
258                Self::ContractData(v) => v.write_xdr(w)?,
259                Self::ContractCode(v) => v.write_xdr(w)?,
260                Self::ConfigSetting(v) => v.write_xdr(w)?,
261                Self::Ttl(v) => v.write_xdr(w)?,
262            };
263            Ok(())
264        })
265    }
266}