Skip to main content

stellar_xdr/generated/
hash_id_preimage.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// HashIdPreimage is an XDR Union defined as:
5///
6/// ```text
7/// union HashIDPreimage switch (EnvelopeType type)
8/// {
9/// case ENVELOPE_TYPE_OP_ID:
10///     struct
11///     {
12///         AccountID sourceAccount;
13///         SequenceNumber seqNum;
14///         uint32 opNum;
15///     } operationID;
16/// case ENVELOPE_TYPE_POOL_REVOKE_OP_ID:
17///     struct
18///     {
19///         AccountID sourceAccount;
20///         SequenceNumber seqNum;
21///         uint32 opNum;
22///         PoolID liquidityPoolID;
23///         Asset asset;
24///     } revokeID;
25/// case ENVELOPE_TYPE_CONTRACT_ID:
26///     struct
27///     {
28///         Hash networkID;
29///         ContractIDPreimage contractIDPreimage;
30///     } contractID;
31/// case ENVELOPE_TYPE_SOROBAN_AUTHORIZATION:
32///     struct
33///     {
34///         Hash networkID;
35///         int64 nonce;
36///         uint32 signatureExpirationLedger;
37///         SorobanAuthorizedInvocation invocation;
38///     } sorobanAuthorization;
39/// case ENVELOPE_TYPE_SOROBAN_AUTHORIZATION_WITH_ADDRESS:
40///     struct
41///     {
42///         Hash networkID;
43///         int64 nonce;
44///         uint32 signatureExpirationLedger;
45///         SCAddress address;
46///         SorobanAuthorizedInvocation invocation;
47///     } sorobanAuthorizationWithAddress;
48/// };
49/// ```
50///
51// union with discriminant EnvelopeType
52#[cfg_attr(feature = "serde", cfg_eval::cfg_eval)]
53#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
54#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
55#[cfg_attr(
56    all(feature = "serde", feature = "alloc"),
57    serde_with::serde_as,
58    derive(serde::Serialize, serde::Deserialize),
59    serde(rename_all = "snake_case")
60)]
61#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
62#[allow(clippy::large_enum_variant)]
63pub enum HashIdPreimage {
64    OpId(HashIdPreimageOperationId),
65    PoolRevokeOpId(HashIdPreimageRevokeId),
66    ContractId(HashIdPreimageContractId),
67    SorobanAuthorization(HashIdPreimageSorobanAuthorization),
68    SorobanAuthorizationWithAddress(HashIdPreimageSorobanAuthorizationWithAddress),
69}
70
71#[cfg(feature = "alloc")]
72impl Default for HashIdPreimage {
73    fn default() -> Self {
74        Self::OpId(HashIdPreimageOperationId::default())
75    }
76}
77
78impl HashIdPreimage {
79    const _VARIANTS: &[EnvelopeType] = &[
80        EnvelopeType::OpId,
81        EnvelopeType::PoolRevokeOpId,
82        EnvelopeType::ContractId,
83        EnvelopeType::SorobanAuthorization,
84        EnvelopeType::SorobanAuthorizationWithAddress,
85    ];
86    pub const VARIANTS: [EnvelopeType; Self::_VARIANTS.len()] = {
87        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
88        let mut i = 1;
89        while i < Self::_VARIANTS.len() {
90            arr[i] = Self::_VARIANTS[i];
91            i += 1;
92        }
93        arr
94    };
95    const _VARIANTS_STR: &[&str] = &[
96        "OpId",
97        "PoolRevokeOpId",
98        "ContractId",
99        "SorobanAuthorization",
100        "SorobanAuthorizationWithAddress",
101    ];
102    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
103        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
104        let mut i = 1;
105        while i < Self::_VARIANTS_STR.len() {
106            arr[i] = Self::_VARIANTS_STR[i];
107            i += 1;
108        }
109        arr
110    };
111
112    #[must_use]
113    pub const fn name(&self) -> &'static str {
114        match self {
115            Self::OpId(_) => "OpId",
116            Self::PoolRevokeOpId(_) => "PoolRevokeOpId",
117            Self::ContractId(_) => "ContractId",
118            Self::SorobanAuthorization(_) => "SorobanAuthorization",
119            Self::SorobanAuthorizationWithAddress(_) => "SorobanAuthorizationWithAddress",
120        }
121    }
122
123    #[must_use]
124    pub const fn discriminant(&self) -> EnvelopeType {
125        #[allow(clippy::match_same_arms)]
126        match self {
127            Self::OpId(_) => EnvelopeType::OpId,
128            Self::PoolRevokeOpId(_) => EnvelopeType::PoolRevokeOpId,
129            Self::ContractId(_) => EnvelopeType::ContractId,
130            Self::SorobanAuthorization(_) => EnvelopeType::SorobanAuthorization,
131            Self::SorobanAuthorizationWithAddress(_) => {
132                EnvelopeType::SorobanAuthorizationWithAddress
133            }
134        }
135    }
136
137    #[must_use]
138    pub const fn variants() -> [EnvelopeType; Self::_VARIANTS.len()] {
139        Self::VARIANTS
140    }
141}
142
143impl Name for HashIdPreimage {
144    #[must_use]
145    fn name(&self) -> &'static str {
146        Self::name(self)
147    }
148}
149
150impl Discriminant<EnvelopeType> for HashIdPreimage {
151    #[must_use]
152    fn discriminant(&self) -> EnvelopeType {
153        Self::discriminant(self)
154    }
155}
156
157impl Variants<EnvelopeType> for HashIdPreimage {
158    fn variants() -> slice::Iter<'static, EnvelopeType> {
159        Self::VARIANTS.iter()
160    }
161}
162
163impl Union<EnvelopeType> for HashIdPreimage {}
164
165impl ReadXdr for HashIdPreimage {
166    #[cfg(feature = "std")]
167    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
168        r.with_limited_depth(|r| {
169            let dv: EnvelopeType = <EnvelopeType as ReadXdr>::read_xdr(r)?;
170            #[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
171            let v = match dv {
172                EnvelopeType::OpId => Self::OpId(HashIdPreimageOperationId::read_xdr(r)?),
173                EnvelopeType::PoolRevokeOpId => {
174                    Self::PoolRevokeOpId(HashIdPreimageRevokeId::read_xdr(r)?)
175                }
176                EnvelopeType::ContractId => {
177                    Self::ContractId(HashIdPreimageContractId::read_xdr(r)?)
178                }
179                EnvelopeType::SorobanAuthorization => {
180                    Self::SorobanAuthorization(HashIdPreimageSorobanAuthorization::read_xdr(r)?)
181                }
182                EnvelopeType::SorobanAuthorizationWithAddress => {
183                    Self::SorobanAuthorizationWithAddress(
184                        HashIdPreimageSorobanAuthorizationWithAddress::read_xdr(r)?,
185                    )
186                }
187                #[allow(unreachable_patterns)]
188                _ => return Err(Error::Invalid),
189            };
190            Ok(v)
191        })
192    }
193}
194
195impl WriteXdr for HashIdPreimage {
196    #[cfg(feature = "std")]
197    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
198        w.with_limited_depth(|w| {
199            self.discriminant().write_xdr(w)?;
200            #[allow(clippy::match_same_arms)]
201            match self {
202                Self::OpId(v) => v.write_xdr(w)?,
203                Self::PoolRevokeOpId(v) => v.write_xdr(w)?,
204                Self::ContractId(v) => v.write_xdr(w)?,
205                Self::SorobanAuthorization(v) => v.write_xdr(w)?,
206                Self::SorobanAuthorizationWithAddress(v) => v.write_xdr(w)?,
207            };
208            Ok(())
209        })
210    }
211}