Skip to main content

stellar_xdr/generated/
payment_result_code.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// PaymentResultCode is an XDR Enum defined as:
5///
6/// ```text
7/// enum PaymentResultCode
8/// {
9///     // codes considered as "success" for the operation
10///     PAYMENT_SUCCESS = 0, // payment successfully completed
11///
12///     // codes considered as "failure" for the operation
13///     PAYMENT_MALFORMED = -1,          // bad input
14///     PAYMENT_UNDERFUNDED = -2,        // not enough funds in source account
15///     PAYMENT_SRC_NO_TRUST = -3,       // no trust line on source account
16///     PAYMENT_SRC_NOT_AUTHORIZED = -4, // source not authorized to transfer
17///     PAYMENT_NO_DESTINATION = -5,     // destination account does not exist
18///     PAYMENT_NO_TRUST = -6,       // destination missing a trust line for asset
19///     PAYMENT_NOT_AUTHORIZED = -7, // destination not authorized to hold asset
20///     PAYMENT_LINE_FULL = -8,      // destination would go above their limit
21///     PAYMENT_NO_ISSUER = -9       // missing issuer on asset
22/// };
23/// ```
24///
25// enum
26#[cfg_attr(feature = "alloc", derive(Default))]
27#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
28#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
29#[cfg_attr(
30    all(feature = "serde", feature = "alloc"),
31    derive(serde::Serialize, serde::Deserialize),
32    serde(rename_all = "snake_case")
33)]
34#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
35#[repr(i32)]
36pub enum PaymentResultCode {
37    #[cfg_attr(feature = "alloc", default)]
38    Success = 0,
39    Malformed = -1,
40    Underfunded = -2,
41    SrcNoTrust = -3,
42    SrcNotAuthorized = -4,
43    NoDestination = -5,
44    NoTrust = -6,
45    NotAuthorized = -7,
46    LineFull = -8,
47    NoIssuer = -9,
48}
49
50impl PaymentResultCode {
51    const _VARIANTS: &[PaymentResultCode] = &[
52        PaymentResultCode::Success,
53        PaymentResultCode::Malformed,
54        PaymentResultCode::Underfunded,
55        PaymentResultCode::SrcNoTrust,
56        PaymentResultCode::SrcNotAuthorized,
57        PaymentResultCode::NoDestination,
58        PaymentResultCode::NoTrust,
59        PaymentResultCode::NotAuthorized,
60        PaymentResultCode::LineFull,
61        PaymentResultCode::NoIssuer,
62    ];
63    pub const VARIANTS: [PaymentResultCode; 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        "Underfunded",
76        "SrcNoTrust",
77        "SrcNotAuthorized",
78        "NoDestination",
79        "NoTrust",
80        "NotAuthorized",
81        "LineFull",
82        "NoIssuer",
83    ];
84    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
85        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
86        let mut i = 1;
87        while i < Self::_VARIANTS_STR.len() {
88            arr[i] = Self::_VARIANTS_STR[i];
89            i += 1;
90        }
91        arr
92    };
93
94    #[must_use]
95    pub const fn name(&self) -> &'static str {
96        match self {
97            Self::Success => "Success",
98            Self::Malformed => "Malformed",
99            Self::Underfunded => "Underfunded",
100            Self::SrcNoTrust => "SrcNoTrust",
101            Self::SrcNotAuthorized => "SrcNotAuthorized",
102            Self::NoDestination => "NoDestination",
103            Self::NoTrust => "NoTrust",
104            Self::NotAuthorized => "NotAuthorized",
105            Self::LineFull => "LineFull",
106            Self::NoIssuer => "NoIssuer",
107        }
108    }
109
110    #[must_use]
111    pub const fn variants() -> [PaymentResultCode; Self::_VARIANTS.len()] {
112        Self::VARIANTS
113    }
114}
115
116impl Name for PaymentResultCode {
117    #[must_use]
118    fn name(&self) -> &'static str {
119        Self::name(self)
120    }
121}
122
123impl Variants<PaymentResultCode> for PaymentResultCode {
124    fn variants() -> slice::Iter<'static, PaymentResultCode> {
125        Self::VARIANTS.iter()
126    }
127}
128
129impl Enum for PaymentResultCode {}
130
131impl fmt::Display for PaymentResultCode {
132    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133        f.write_str(self.name())
134    }
135}
136
137impl TryFrom<i32> for PaymentResultCode {
138    type Error = Error;
139
140    fn try_from(i: i32) -> Result<Self, Error> {
141        let e = match i {
142            0 => PaymentResultCode::Success,
143            -1 => PaymentResultCode::Malformed,
144            -2 => PaymentResultCode::Underfunded,
145            -3 => PaymentResultCode::SrcNoTrust,
146            -4 => PaymentResultCode::SrcNotAuthorized,
147            -5 => PaymentResultCode::NoDestination,
148            -6 => PaymentResultCode::NoTrust,
149            -7 => PaymentResultCode::NotAuthorized,
150            -8 => PaymentResultCode::LineFull,
151            -9 => PaymentResultCode::NoIssuer,
152            #[allow(unreachable_patterns)]
153            _ => return Err(Error::Invalid),
154        };
155        Ok(e)
156    }
157}
158
159impl From<PaymentResultCode> for i32 {
160    #[must_use]
161    fn from(e: PaymentResultCode) -> Self {
162        e as Self
163    }
164}
165
166impl ReadXdr for PaymentResultCode {
167    #[cfg(feature = "std")]
168    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
169        r.with_limited_depth(|r| {
170            let e = i32::read_xdr(r)?;
171            let v: Self = e.try_into()?;
172            Ok(v)
173        })
174    }
175}
176
177impl WriteXdr for PaymentResultCode {
178    #[cfg(feature = "std")]
179    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
180        w.with_limited_depth(|w| {
181            let i: i32 = (*self).into();
182            i.write_xdr(w)
183        })
184    }
185}