Skip to main content

stellar_xdr/generated/
scp_statement_pledges.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// ScpStatementPledges is an XDR NestedUnion defined as:
5///
6/// ```text
7/// union switch (SCPStatementType type)
8///     {
9///     case SCP_ST_PREPARE:
10///         struct
11///         {
12///             Hash quorumSetHash;       // D
13///             SCPBallot ballot;         // b
14///             SCPBallot* prepared;      // p
15///             SCPBallot* preparedPrime; // p'
16///             uint32 nC;                // c.n
17///             uint32 nH;                // h.n
18///         } prepare;
19///     case SCP_ST_CONFIRM:
20///         struct
21///         {
22///             SCPBallot ballot;   // b
23///             uint32 nPrepared;   // p.n
24///             uint32 nCommit;     // c.n
25///             uint32 nH;          // h.n
26///             Hash quorumSetHash; // D
27///         } confirm;
28///     case SCP_ST_EXTERNALIZE:
29///         struct
30///         {
31///             SCPBallot commit;         // c
32///             uint32 nH;                // h.n
33///             Hash commitQuorumSetHash; // D used before EXTERNALIZE
34///         } externalize;
35///     case SCP_ST_NOMINATE:
36///         SCPNomination nominate;
37///     }
38/// ```
39///
40// union with discriminant ScpStatementType
41#[cfg_attr(feature = "serde", cfg_eval::cfg_eval)]
42#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
43#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
44#[cfg_attr(
45    all(feature = "serde", feature = "alloc"),
46    serde_with::serde_as,
47    derive(serde::Serialize, serde::Deserialize),
48    serde(rename_all = "snake_case")
49)]
50#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
51#[allow(clippy::large_enum_variant)]
52pub enum ScpStatementPledges {
53    Prepare(ScpStatementPrepare),
54    Confirm(ScpStatementConfirm),
55    Externalize(ScpStatementExternalize),
56    Nominate(ScpNomination),
57}
58
59#[cfg(feature = "alloc")]
60impl Default for ScpStatementPledges {
61    fn default() -> Self {
62        Self::Prepare(ScpStatementPrepare::default())
63    }
64}
65
66impl ScpStatementPledges {
67    const _VARIANTS: &[ScpStatementType] = &[
68        ScpStatementType::Prepare,
69        ScpStatementType::Confirm,
70        ScpStatementType::Externalize,
71        ScpStatementType::Nominate,
72    ];
73    pub const VARIANTS: [ScpStatementType; Self::_VARIANTS.len()] = {
74        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
75        let mut i = 1;
76        while i < Self::_VARIANTS.len() {
77            arr[i] = Self::_VARIANTS[i];
78            i += 1;
79        }
80        arr
81    };
82    const _VARIANTS_STR: &[&str] = &["Prepare", "Confirm", "Externalize", "Nominate"];
83    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
84        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
85        let mut i = 1;
86        while i < Self::_VARIANTS_STR.len() {
87            arr[i] = Self::_VARIANTS_STR[i];
88            i += 1;
89        }
90        arr
91    };
92
93    #[must_use]
94    pub const fn name(&self) -> &'static str {
95        match self {
96            Self::Prepare(_) => "Prepare",
97            Self::Confirm(_) => "Confirm",
98            Self::Externalize(_) => "Externalize",
99            Self::Nominate(_) => "Nominate",
100        }
101    }
102
103    #[must_use]
104    pub const fn discriminant(&self) -> ScpStatementType {
105        #[allow(clippy::match_same_arms)]
106        match self {
107            Self::Prepare(_) => ScpStatementType::Prepare,
108            Self::Confirm(_) => ScpStatementType::Confirm,
109            Self::Externalize(_) => ScpStatementType::Externalize,
110            Self::Nominate(_) => ScpStatementType::Nominate,
111        }
112    }
113
114    #[must_use]
115    pub const fn variants() -> [ScpStatementType; Self::_VARIANTS.len()] {
116        Self::VARIANTS
117    }
118}
119
120impl Name for ScpStatementPledges {
121    #[must_use]
122    fn name(&self) -> &'static str {
123        Self::name(self)
124    }
125}
126
127impl Discriminant<ScpStatementType> for ScpStatementPledges {
128    #[must_use]
129    fn discriminant(&self) -> ScpStatementType {
130        Self::discriminant(self)
131    }
132}
133
134impl Variants<ScpStatementType> for ScpStatementPledges {
135    fn variants() -> slice::Iter<'static, ScpStatementType> {
136        Self::VARIANTS.iter()
137    }
138}
139
140impl Union<ScpStatementType> for ScpStatementPledges {}
141
142impl ReadXdr for ScpStatementPledges {
143    #[cfg(feature = "std")]
144    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
145        r.with_limited_depth(|r| {
146            let dv: ScpStatementType = <ScpStatementType as ReadXdr>::read_xdr(r)?;
147            #[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
148            let v = match dv {
149                ScpStatementType::Prepare => Self::Prepare(ScpStatementPrepare::read_xdr(r)?),
150                ScpStatementType::Confirm => Self::Confirm(ScpStatementConfirm::read_xdr(r)?),
151                ScpStatementType::Externalize => {
152                    Self::Externalize(ScpStatementExternalize::read_xdr(r)?)
153                }
154                ScpStatementType::Nominate => Self::Nominate(ScpNomination::read_xdr(r)?),
155                #[allow(unreachable_patterns)]
156                _ => return Err(Error::Invalid),
157            };
158            Ok(v)
159        })
160    }
161}
162
163impl WriteXdr for ScpStatementPledges {
164    #[cfg(feature = "std")]
165    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
166        w.with_limited_depth(|w| {
167            self.discriminant().write_xdr(w)?;
168            #[allow(clippy::match_same_arms)]
169            match self {
170                Self::Prepare(v) => v.write_xdr(w)?,
171                Self::Confirm(v) => v.write_xdr(w)?,
172                Self::Externalize(v) => v.write_xdr(w)?,
173                Self::Nominate(v) => v.write_xdr(w)?,
174            };
175            Ok(())
176        })
177    }
178}