Skip to main content

stellar_xdr/generated/
allow_trust_result.rs

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