Skip to main content

stellar_xdr/generated/
change_trust_result_code.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// ChangeTrustResultCode is an XDR Enum defined as:
5///
6/// ```text
7/// enum ChangeTrustResultCode
8/// {
9///     // codes considered as "success" for the operation
10///     CHANGE_TRUST_SUCCESS = 0,
11///     // codes considered as "failure" for the operation
12///     CHANGE_TRUST_MALFORMED = -1,     // bad input
13///     CHANGE_TRUST_NO_ISSUER = -2,     // could not find issuer
14///     CHANGE_TRUST_INVALID_LIMIT = -3, // cannot drop limit below balance
15///                                      // cannot create with a limit of 0
16///     CHANGE_TRUST_LOW_RESERVE =
17///         -4, // not enough funds to create a new trust line,
18///     CHANGE_TRUST_SELF_NOT_ALLOWED = -5,   // trusting self is not allowed
19///     CHANGE_TRUST_TRUST_LINE_MISSING = -6, // Asset trustline is missing for pool
20///     CHANGE_TRUST_CANNOT_DELETE =
21///         -7, // Asset trustline is still referenced in a pool
22///     CHANGE_TRUST_NOT_AUTH_MAINTAIN_LIABILITIES =
23///         -8 // Asset trustline is deauthorized
24/// };
25/// ```
26///
27// enum
28#[cfg_attr(feature = "alloc", derive(Default))]
29#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
30#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
31#[cfg_attr(
32    all(feature = "serde", feature = "alloc"),
33    derive(serde::Serialize, serde::Deserialize),
34    serde(rename_all = "snake_case")
35)]
36#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
37#[repr(i32)]
38pub enum ChangeTrustResultCode {
39    #[cfg_attr(feature = "alloc", default)]
40    Success = 0,
41    Malformed = -1,
42    NoIssuer = -2,
43    InvalidLimit = -3,
44    LowReserve = -4,
45    SelfNotAllowed = -5,
46    TrustLineMissing = -6,
47    CannotDelete = -7,
48    NotAuthMaintainLiabilities = -8,
49}
50
51impl ChangeTrustResultCode {
52    const _VARIANTS: &[ChangeTrustResultCode] = &[
53        ChangeTrustResultCode::Success,
54        ChangeTrustResultCode::Malformed,
55        ChangeTrustResultCode::NoIssuer,
56        ChangeTrustResultCode::InvalidLimit,
57        ChangeTrustResultCode::LowReserve,
58        ChangeTrustResultCode::SelfNotAllowed,
59        ChangeTrustResultCode::TrustLineMissing,
60        ChangeTrustResultCode::CannotDelete,
61        ChangeTrustResultCode::NotAuthMaintainLiabilities,
62    ];
63    pub const VARIANTS: [ChangeTrustResultCode; Self::_VARIANTS.len()] = {
64        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
65        let mut i = 1;
66        while i < Self::_VARIANTS.len() {
67            arr[i] = Self::_VARIANTS[i];
68            i += 1;
69        }
70        arr
71    };
72    const _VARIANTS_STR: &[&str] = &[
73        "Success",
74        "Malformed",
75        "NoIssuer",
76        "InvalidLimit",
77        "LowReserve",
78        "SelfNotAllowed",
79        "TrustLineMissing",
80        "CannotDelete",
81        "NotAuthMaintainLiabilities",
82    ];
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::Success => "Success",
97            Self::Malformed => "Malformed",
98            Self::NoIssuer => "NoIssuer",
99            Self::InvalidLimit => "InvalidLimit",
100            Self::LowReserve => "LowReserve",
101            Self::SelfNotAllowed => "SelfNotAllowed",
102            Self::TrustLineMissing => "TrustLineMissing",
103            Self::CannotDelete => "CannotDelete",
104            Self::NotAuthMaintainLiabilities => "NotAuthMaintainLiabilities",
105        }
106    }
107
108    #[must_use]
109    pub const fn variants() -> [ChangeTrustResultCode; Self::_VARIANTS.len()] {
110        Self::VARIANTS
111    }
112}
113
114impl Name for ChangeTrustResultCode {
115    #[must_use]
116    fn name(&self) -> &'static str {
117        Self::name(self)
118    }
119}
120
121impl Variants<ChangeTrustResultCode> for ChangeTrustResultCode {
122    fn variants() -> slice::Iter<'static, ChangeTrustResultCode> {
123        Self::VARIANTS.iter()
124    }
125}
126
127impl Enum for ChangeTrustResultCode {}
128
129impl fmt::Display for ChangeTrustResultCode {
130    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131        f.write_str(self.name())
132    }
133}
134
135impl TryFrom<i32> for ChangeTrustResultCode {
136    type Error = Error;
137
138    fn try_from(i: i32) -> Result<Self, Error> {
139        let e = match i {
140            0 => ChangeTrustResultCode::Success,
141            -1 => ChangeTrustResultCode::Malformed,
142            -2 => ChangeTrustResultCode::NoIssuer,
143            -3 => ChangeTrustResultCode::InvalidLimit,
144            -4 => ChangeTrustResultCode::LowReserve,
145            -5 => ChangeTrustResultCode::SelfNotAllowed,
146            -6 => ChangeTrustResultCode::TrustLineMissing,
147            -7 => ChangeTrustResultCode::CannotDelete,
148            -8 => ChangeTrustResultCode::NotAuthMaintainLiabilities,
149            #[allow(unreachable_patterns)]
150            _ => return Err(Error::Invalid),
151        };
152        Ok(e)
153    }
154}
155
156impl From<ChangeTrustResultCode> for i32 {
157    #[must_use]
158    fn from(e: ChangeTrustResultCode) -> Self {
159        e as Self
160    }
161}
162
163impl ReadXdr for ChangeTrustResultCode {
164    #[cfg(feature = "std")]
165    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
166        r.with_limited_depth(|r| {
167            let e = i32::read_xdr(r)?;
168            let v: Self = e.try_into()?;
169            Ok(v)
170        })
171    }
172}
173
174impl WriteXdr for ChangeTrustResultCode {
175    #[cfg(feature = "std")]
176    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
177        w.with_limited_depth(|w| {
178            let i: i32 = (*self).into();
179            i.write_xdr(w)
180        })
181    }
182}