Skip to main content

stellar_xdr/generated/
soroban_authorized_function.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// SorobanAuthorizedFunction is an XDR Union defined as:
5///
6/// ```text
7/// union SorobanAuthorizedFunction switch (SorobanAuthorizedFunctionType type)
8/// {
9/// case SOROBAN_AUTHORIZED_FUNCTION_TYPE_CONTRACT_FN:
10///     InvokeContractArgs contractFn;
11/// // This variant of auth payload for creating new contract instances
12/// // doesn't allow specifying the constructor arguments, creating contracts
13/// // with constructors that take arguments is only possible by authorizing
14/// // `SOROBAN_AUTHORIZED_FUNCTION_TYPE_CREATE_CONTRACT_V2_HOST_FN`
15/// // (protocol 22+).
16/// case SOROBAN_AUTHORIZED_FUNCTION_TYPE_CREATE_CONTRACT_HOST_FN:
17///     CreateContractArgs createContractHostFn;
18/// // This variant of auth payload for creating new contract instances
19/// // is only accepted in and after protocol 22. It allows authorizing the
20/// // contract constructor arguments.
21/// case SOROBAN_AUTHORIZED_FUNCTION_TYPE_CREATE_CONTRACT_V2_HOST_FN:
22///     CreateContractArgsV2 createContractV2HostFn;
23/// };
24/// ```
25///
26// union with discriminant SorobanAuthorizedFunctionType
27#[cfg_attr(feature = "serde", cfg_eval::cfg_eval)]
28#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
29#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
30#[cfg_attr(
31    all(feature = "serde", feature = "alloc"),
32    serde_with::serde_as,
33    derive(serde::Serialize, serde::Deserialize),
34    serde(rename_all = "snake_case")
35)]
36#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
37#[allow(clippy::large_enum_variant)]
38pub enum SorobanAuthorizedFunction {
39    ContractFn(InvokeContractArgs),
40    CreateContractHostFn(CreateContractArgs),
41    CreateContractV2HostFn(CreateContractArgsV2),
42}
43
44#[cfg(feature = "alloc")]
45impl Default for SorobanAuthorizedFunction {
46    fn default() -> Self {
47        Self::ContractFn(InvokeContractArgs::default())
48    }
49}
50
51impl SorobanAuthorizedFunction {
52    const _VARIANTS: &[SorobanAuthorizedFunctionType] = &[
53        SorobanAuthorizedFunctionType::ContractFn,
54        SorobanAuthorizedFunctionType::CreateContractHostFn,
55        SorobanAuthorizedFunctionType::CreateContractV2HostFn,
56    ];
57    pub const VARIANTS: [SorobanAuthorizedFunctionType; 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        "ContractFn",
68        "CreateContractHostFn",
69        "CreateContractV2HostFn",
70    ];
71    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
72        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
73        let mut i = 1;
74        while i < Self::_VARIANTS_STR.len() {
75            arr[i] = Self::_VARIANTS_STR[i];
76            i += 1;
77        }
78        arr
79    };
80
81    #[must_use]
82    pub const fn name(&self) -> &'static str {
83        match self {
84            Self::ContractFn(_) => "ContractFn",
85            Self::CreateContractHostFn(_) => "CreateContractHostFn",
86            Self::CreateContractV2HostFn(_) => "CreateContractV2HostFn",
87        }
88    }
89
90    #[must_use]
91    pub const fn discriminant(&self) -> SorobanAuthorizedFunctionType {
92        #[allow(clippy::match_same_arms)]
93        match self {
94            Self::ContractFn(_) => SorobanAuthorizedFunctionType::ContractFn,
95            Self::CreateContractHostFn(_) => SorobanAuthorizedFunctionType::CreateContractHostFn,
96            Self::CreateContractV2HostFn(_) => {
97                SorobanAuthorizedFunctionType::CreateContractV2HostFn
98            }
99        }
100    }
101
102    #[must_use]
103    pub const fn variants() -> [SorobanAuthorizedFunctionType; Self::_VARIANTS.len()] {
104        Self::VARIANTS
105    }
106}
107
108impl Name for SorobanAuthorizedFunction {
109    #[must_use]
110    fn name(&self) -> &'static str {
111        Self::name(self)
112    }
113}
114
115impl Discriminant<SorobanAuthorizedFunctionType> for SorobanAuthorizedFunction {
116    #[must_use]
117    fn discriminant(&self) -> SorobanAuthorizedFunctionType {
118        Self::discriminant(self)
119    }
120}
121
122impl Variants<SorobanAuthorizedFunctionType> for SorobanAuthorizedFunction {
123    fn variants() -> slice::Iter<'static, SorobanAuthorizedFunctionType> {
124        Self::VARIANTS.iter()
125    }
126}
127
128impl Union<SorobanAuthorizedFunctionType> for SorobanAuthorizedFunction {}
129
130impl ReadXdr for SorobanAuthorizedFunction {
131    #[cfg(feature = "std")]
132    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
133        r.with_limited_depth(|r| {
134            let dv: SorobanAuthorizedFunctionType =
135                <SorobanAuthorizedFunctionType as ReadXdr>::read_xdr(r)?;
136            #[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
137            let v = match dv {
138                SorobanAuthorizedFunctionType::ContractFn => {
139                    Self::ContractFn(InvokeContractArgs::read_xdr(r)?)
140                }
141                SorobanAuthorizedFunctionType::CreateContractHostFn => {
142                    Self::CreateContractHostFn(CreateContractArgs::read_xdr(r)?)
143                }
144                SorobanAuthorizedFunctionType::CreateContractV2HostFn => {
145                    Self::CreateContractV2HostFn(CreateContractArgsV2::read_xdr(r)?)
146                }
147                #[allow(unreachable_patterns)]
148                _ => return Err(Error::Invalid),
149            };
150            Ok(v)
151        })
152    }
153}
154
155impl WriteXdr for SorobanAuthorizedFunction {
156    #[cfg(feature = "std")]
157    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
158        w.with_limited_depth(|w| {
159            self.discriminant().write_xdr(w)?;
160            #[allow(clippy::match_same_arms)]
161            match self {
162                Self::ContractFn(v) => v.write_xdr(w)?,
163                Self::CreateContractHostFn(v) => v.write_xdr(w)?,
164                Self::CreateContractV2HostFn(v) => v.write_xdr(w)?,
165            };
166            Ok(())
167        })
168    }
169}