Skip to main content

stellar_xdr/generated/
create_account_result_code.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// CreateAccountResultCode is an XDR Enum defined as:
5///
6/// ```text
7/// enum CreateAccountResultCode
8/// {
9///     // codes considered as "success" for the operation
10///     CREATE_ACCOUNT_SUCCESS = 0, // account was created
11///
12///     // codes considered as "failure" for the operation
13///     CREATE_ACCOUNT_MALFORMED = -1,   // invalid destination
14///     CREATE_ACCOUNT_UNDERFUNDED = -2, // not enough funds in source account
15///     CREATE_ACCOUNT_LOW_RESERVE =
16///         -3, // would create an account below the min reserve
17///     CREATE_ACCOUNT_ALREADY_EXIST = -4 // account already exists
18/// };
19/// ```
20///
21// enum
22#[cfg_attr(feature = "alloc", derive(Default))]
23#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
24#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
25#[cfg_attr(
26    all(feature = "serde", feature = "alloc"),
27    derive(serde::Serialize, serde::Deserialize),
28    serde(rename_all = "snake_case")
29)]
30#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
31#[repr(i32)]
32pub enum CreateAccountResultCode {
33    #[cfg_attr(feature = "alloc", default)]
34    Success = 0,
35    Malformed = -1,
36    Underfunded = -2,
37    LowReserve = -3,
38    AlreadyExist = -4,
39}
40
41impl CreateAccountResultCode {
42    const _VARIANTS: &[CreateAccountResultCode] = &[
43        CreateAccountResultCode::Success,
44        CreateAccountResultCode::Malformed,
45        CreateAccountResultCode::Underfunded,
46        CreateAccountResultCode::LowReserve,
47        CreateAccountResultCode::AlreadyExist,
48    ];
49    pub const VARIANTS: [CreateAccountResultCode; Self::_VARIANTS.len()] = {
50        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
51        let mut i = 1;
52        while i < Self::_VARIANTS.len() {
53            arr[i] = Self::_VARIANTS[i];
54            i += 1;
55        }
56        arr
57    };
58    const _VARIANTS_STR: &[&str] = &[
59        "Success",
60        "Malformed",
61        "Underfunded",
62        "LowReserve",
63        "AlreadyExist",
64    ];
65    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
66        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
67        let mut i = 1;
68        while i < Self::_VARIANTS_STR.len() {
69            arr[i] = Self::_VARIANTS_STR[i];
70            i += 1;
71        }
72        arr
73    };
74
75    #[must_use]
76    pub const fn name(&self) -> &'static str {
77        match self {
78            Self::Success => "Success",
79            Self::Malformed => "Malformed",
80            Self::Underfunded => "Underfunded",
81            Self::LowReserve => "LowReserve",
82            Self::AlreadyExist => "AlreadyExist",
83        }
84    }
85
86    #[must_use]
87    pub const fn variants() -> [CreateAccountResultCode; Self::_VARIANTS.len()] {
88        Self::VARIANTS
89    }
90}
91
92impl Name for CreateAccountResultCode {
93    #[must_use]
94    fn name(&self) -> &'static str {
95        Self::name(self)
96    }
97}
98
99impl Variants<CreateAccountResultCode> for CreateAccountResultCode {
100    fn variants() -> slice::Iter<'static, CreateAccountResultCode> {
101        Self::VARIANTS.iter()
102    }
103}
104
105impl Enum for CreateAccountResultCode {}
106
107impl fmt::Display for CreateAccountResultCode {
108    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109        f.write_str(self.name())
110    }
111}
112
113impl TryFrom<i32> for CreateAccountResultCode {
114    type Error = Error;
115
116    fn try_from(i: i32) -> Result<Self, Error> {
117        let e = match i {
118            0 => CreateAccountResultCode::Success,
119            -1 => CreateAccountResultCode::Malformed,
120            -2 => CreateAccountResultCode::Underfunded,
121            -3 => CreateAccountResultCode::LowReserve,
122            -4 => CreateAccountResultCode::AlreadyExist,
123            #[allow(unreachable_patterns)]
124            _ => return Err(Error::Invalid),
125        };
126        Ok(e)
127    }
128}
129
130impl From<CreateAccountResultCode> for i32 {
131    #[must_use]
132    fn from(e: CreateAccountResultCode) -> Self {
133        e as Self
134    }
135}
136
137impl ReadXdr for CreateAccountResultCode {
138    #[cfg(feature = "std")]
139    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
140        r.with_limited_depth(|r| {
141            let e = i32::read_xdr(r)?;
142            let v: Self = e.try_into()?;
143            Ok(v)
144        })
145    }
146}
147
148impl WriteXdr for CreateAccountResultCode {
149    #[cfg(feature = "std")]
150    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
151        w.with_limited_depth(|w| {
152            let i: i32 = (*self).into();
153            i.write_xdr(w)
154        })
155    }
156}