Skip to main content

stellar_xdr/generated/
stellar_value_ext.rs

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