Skip to main content

stellar_xdr/generated/
allow_trust_result_code.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// AllowTrustResultCode is an XDR Enum defined as:
5///
6/// ```text
7/// enum AllowTrustResultCode
8/// {
9///     // codes considered as "success" for the operation
10///     ALLOW_TRUST_SUCCESS = 0,
11///     // codes considered as "failure" for the operation
12///     ALLOW_TRUST_MALFORMED = -1,     // asset is not ASSET_TYPE_ALPHANUM
13///     ALLOW_TRUST_NO_TRUST_LINE = -2, // trustor does not have a trustline
14///                                     // source account does not require trust
15///     ALLOW_TRUST_TRUST_NOT_REQUIRED = -3,
16///     ALLOW_TRUST_CANT_REVOKE = -4,      // source account can't revoke trust,
17///     ALLOW_TRUST_SELF_NOT_ALLOWED = -5, // trusting self is not allowed
18///     ALLOW_TRUST_LOW_RESERVE = -6       // claimable balances can't be created
19///                                        // on revoke due to low reserves
20/// };
21/// ```
22///
23// enum
24#[cfg_attr(feature = "alloc", derive(Default))]
25#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
26#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
27#[cfg_attr(
28    all(feature = "serde", feature = "alloc"),
29    derive(serde::Serialize, serde::Deserialize),
30    serde(rename_all = "snake_case")
31)]
32#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
33#[repr(i32)]
34pub enum AllowTrustResultCode {
35    #[cfg_attr(feature = "alloc", default)]
36    Success = 0,
37    Malformed = -1,
38    NoTrustLine = -2,
39    TrustNotRequired = -3,
40    CantRevoke = -4,
41    SelfNotAllowed = -5,
42    LowReserve = -6,
43}
44
45impl AllowTrustResultCode {
46    const _VARIANTS: &[AllowTrustResultCode] = &[
47        AllowTrustResultCode::Success,
48        AllowTrustResultCode::Malformed,
49        AllowTrustResultCode::NoTrustLine,
50        AllowTrustResultCode::TrustNotRequired,
51        AllowTrustResultCode::CantRevoke,
52        AllowTrustResultCode::SelfNotAllowed,
53        AllowTrustResultCode::LowReserve,
54    ];
55    pub const VARIANTS: [AllowTrustResultCode; 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] = &[
65        "Success",
66        "Malformed",
67        "NoTrustLine",
68        "TrustNotRequired",
69        "CantRevoke",
70        "SelfNotAllowed",
71        "LowReserve",
72    ];
73    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
74        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
75        let mut i = 1;
76        while i < Self::_VARIANTS_STR.len() {
77            arr[i] = Self::_VARIANTS_STR[i];
78            i += 1;
79        }
80        arr
81    };
82
83    #[must_use]
84    pub const fn name(&self) -> &'static str {
85        match self {
86            Self::Success => "Success",
87            Self::Malformed => "Malformed",
88            Self::NoTrustLine => "NoTrustLine",
89            Self::TrustNotRequired => "TrustNotRequired",
90            Self::CantRevoke => "CantRevoke",
91            Self::SelfNotAllowed => "SelfNotAllowed",
92            Self::LowReserve => "LowReserve",
93        }
94    }
95
96    #[must_use]
97    pub const fn variants() -> [AllowTrustResultCode; Self::_VARIANTS.len()] {
98        Self::VARIANTS
99    }
100}
101
102impl Name for AllowTrustResultCode {
103    #[must_use]
104    fn name(&self) -> &'static str {
105        Self::name(self)
106    }
107}
108
109impl Variants<AllowTrustResultCode> for AllowTrustResultCode {
110    fn variants() -> slice::Iter<'static, AllowTrustResultCode> {
111        Self::VARIANTS.iter()
112    }
113}
114
115impl Enum for AllowTrustResultCode {}
116
117impl fmt::Display for AllowTrustResultCode {
118    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119        f.write_str(self.name())
120    }
121}
122
123impl TryFrom<i32> for AllowTrustResultCode {
124    type Error = Error;
125
126    fn try_from(i: i32) -> Result<Self, Error> {
127        let e = match i {
128            0 => AllowTrustResultCode::Success,
129            -1 => AllowTrustResultCode::Malformed,
130            -2 => AllowTrustResultCode::NoTrustLine,
131            -3 => AllowTrustResultCode::TrustNotRequired,
132            -4 => AllowTrustResultCode::CantRevoke,
133            -5 => AllowTrustResultCode::SelfNotAllowed,
134            -6 => AllowTrustResultCode::LowReserve,
135            #[allow(unreachable_patterns)]
136            _ => return Err(Error::Invalid),
137        };
138        Ok(e)
139    }
140}
141
142impl From<AllowTrustResultCode> for i32 {
143    #[must_use]
144    fn from(e: AllowTrustResultCode) -> Self {
145        e as Self
146    }
147}
148
149impl ReadXdr for AllowTrustResultCode {
150    #[cfg(feature = "std")]
151    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
152        r.with_limited_depth(|r| {
153            let e = i32::read_xdr(r)?;
154            let v: Self = e.try_into()?;
155            Ok(v)
156        })
157    }
158}
159
160impl WriteXdr for AllowTrustResultCode {
161    #[cfg(feature = "std")]
162    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
163        w.with_limited_depth(|w| {
164            let i: i32 = (*self).into();
165            i.write_xdr(w)
166        })
167    }
168}