Skip to main content

stellar_xdr/generated/
invoke_host_function_result.rs

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