1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4#[cfg_attr(feature = "serde", cfg_eval::cfg_eval)]
71#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
72#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
73#[cfg_attr(
74 all(feature = "serde", feature = "alloc"),
75 serde_with::serde_as,
76 derive(serde::Serialize, serde::Deserialize),
77 serde(rename_all = "snake_case")
78)]
79#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
80#[allow(clippy::large_enum_variant)]
81pub enum ScVal {
82 Bool(bool),
83 Void,
84 Error(ScError),
85 U32(u32),
86 I32(i32),
87 U64(
88 #[cfg_attr(
89 all(feature = "serde", feature = "alloc"),
90 serde_as(as = "NumberOrString")
91 )]
92 u64,
93 ),
94 I64(
95 #[cfg_attr(
96 all(feature = "serde", feature = "alloc"),
97 serde_as(as = "NumberOrString")
98 )]
99 i64,
100 ),
101 Timepoint(TimePoint),
102 Duration(Duration),
103 U128(UInt128Parts),
104 I128(Int128Parts),
105 U256(UInt256Parts),
106 I256(Int256Parts),
107 Bytes(ScBytes),
108 String(ScString),
109 Symbol(ScSymbol),
110 Vec(Option<ScVec>),
111 Map(Option<ScMap>),
112 Address(ScAddress),
113 ContractInstance(ScContractInstance),
114 LedgerKeyContractInstance,
115 LedgerKeyNonce(ScNonceKey),
116}
117
118#[cfg(feature = "alloc")]
119impl Default for ScVal {
120 fn default() -> Self {
121 Self::Bool(bool::default())
122 }
123}
124
125impl ScVal {
126 const _VARIANTS: &[ScValType] = &[
127 ScValType::Bool,
128 ScValType::Void,
129 ScValType::Error,
130 ScValType::U32,
131 ScValType::I32,
132 ScValType::U64,
133 ScValType::I64,
134 ScValType::Timepoint,
135 ScValType::Duration,
136 ScValType::U128,
137 ScValType::I128,
138 ScValType::U256,
139 ScValType::I256,
140 ScValType::Bytes,
141 ScValType::String,
142 ScValType::Symbol,
143 ScValType::Vec,
144 ScValType::Map,
145 ScValType::Address,
146 ScValType::ContractInstance,
147 ScValType::LedgerKeyContractInstance,
148 ScValType::LedgerKeyNonce,
149 ];
150 pub const VARIANTS: [ScValType; Self::_VARIANTS.len()] = {
151 let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
152 let mut i = 1;
153 while i < Self::_VARIANTS.len() {
154 arr[i] = Self::_VARIANTS[i];
155 i += 1;
156 }
157 arr
158 };
159 const _VARIANTS_STR: &[&str] = &[
160 "Bool",
161 "Void",
162 "Error",
163 "U32",
164 "I32",
165 "U64",
166 "I64",
167 "Timepoint",
168 "Duration",
169 "U128",
170 "I128",
171 "U256",
172 "I256",
173 "Bytes",
174 "String",
175 "Symbol",
176 "Vec",
177 "Map",
178 "Address",
179 "ContractInstance",
180 "LedgerKeyContractInstance",
181 "LedgerKeyNonce",
182 ];
183 pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
184 let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
185 let mut i = 1;
186 while i < Self::_VARIANTS_STR.len() {
187 arr[i] = Self::_VARIANTS_STR[i];
188 i += 1;
189 }
190 arr
191 };
192
193 #[must_use]
194 pub const fn name(&self) -> &'static str {
195 match self {
196 Self::Bool(_) => "Bool",
197 Self::Void => "Void",
198 Self::Error(_) => "Error",
199 Self::U32(_) => "U32",
200 Self::I32(_) => "I32",
201 Self::U64(_) => "U64",
202 Self::I64(_) => "I64",
203 Self::Timepoint(_) => "Timepoint",
204 Self::Duration(_) => "Duration",
205 Self::U128(_) => "U128",
206 Self::I128(_) => "I128",
207 Self::U256(_) => "U256",
208 Self::I256(_) => "I256",
209 Self::Bytes(_) => "Bytes",
210 Self::String(_) => "String",
211 Self::Symbol(_) => "Symbol",
212 Self::Vec(_) => "Vec",
213 Self::Map(_) => "Map",
214 Self::Address(_) => "Address",
215 Self::ContractInstance(_) => "ContractInstance",
216 Self::LedgerKeyContractInstance => "LedgerKeyContractInstance",
217 Self::LedgerKeyNonce(_) => "LedgerKeyNonce",
218 }
219 }
220
221 #[must_use]
222 pub const fn discriminant(&self) -> ScValType {
223 #[allow(clippy::match_same_arms)]
224 match self {
225 Self::Bool(_) => ScValType::Bool,
226 Self::Void => ScValType::Void,
227 Self::Error(_) => ScValType::Error,
228 Self::U32(_) => ScValType::U32,
229 Self::I32(_) => ScValType::I32,
230 Self::U64(_) => ScValType::U64,
231 Self::I64(_) => ScValType::I64,
232 Self::Timepoint(_) => ScValType::Timepoint,
233 Self::Duration(_) => ScValType::Duration,
234 Self::U128(_) => ScValType::U128,
235 Self::I128(_) => ScValType::I128,
236 Self::U256(_) => ScValType::U256,
237 Self::I256(_) => ScValType::I256,
238 Self::Bytes(_) => ScValType::Bytes,
239 Self::String(_) => ScValType::String,
240 Self::Symbol(_) => ScValType::Symbol,
241 Self::Vec(_) => ScValType::Vec,
242 Self::Map(_) => ScValType::Map,
243 Self::Address(_) => ScValType::Address,
244 Self::ContractInstance(_) => ScValType::ContractInstance,
245 Self::LedgerKeyContractInstance => ScValType::LedgerKeyContractInstance,
246 Self::LedgerKeyNonce(_) => ScValType::LedgerKeyNonce,
247 }
248 }
249
250 #[must_use]
251 pub const fn variants() -> [ScValType; Self::_VARIANTS.len()] {
252 Self::VARIANTS
253 }
254}
255
256impl Name for ScVal {
257 #[must_use]
258 fn name(&self) -> &'static str {
259 Self::name(self)
260 }
261}
262
263impl Discriminant<ScValType> for ScVal {
264 #[must_use]
265 fn discriminant(&self) -> ScValType {
266 Self::discriminant(self)
267 }
268}
269
270impl Variants<ScValType> for ScVal {
271 fn variants() -> slice::Iter<'static, ScValType> {
272 Self::VARIANTS.iter()
273 }
274}
275
276impl Union<ScValType> for ScVal {}
277
278impl ReadXdr for ScVal {
279 #[cfg(feature = "std")]
280 fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
281 r.with_limited_depth(|r| {
282 let dv: ScValType = <ScValType as ReadXdr>::read_xdr(r)?;
283 #[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
284 let v = match dv {
285 ScValType::Bool => Self::Bool(bool::read_xdr(r)?),
286 ScValType::Void => Self::Void,
287 ScValType::Error => Self::Error(ScError::read_xdr(r)?),
288 ScValType::U32 => Self::U32(u32::read_xdr(r)?),
289 ScValType::I32 => Self::I32(i32::read_xdr(r)?),
290 ScValType::U64 => Self::U64(u64::read_xdr(r)?),
291 ScValType::I64 => Self::I64(i64::read_xdr(r)?),
292 ScValType::Timepoint => Self::Timepoint(TimePoint::read_xdr(r)?),
293 ScValType::Duration => Self::Duration(Duration::read_xdr(r)?),
294 ScValType::U128 => Self::U128(UInt128Parts::read_xdr(r)?),
295 ScValType::I128 => Self::I128(Int128Parts::read_xdr(r)?),
296 ScValType::U256 => Self::U256(UInt256Parts::read_xdr(r)?),
297 ScValType::I256 => Self::I256(Int256Parts::read_xdr(r)?),
298 ScValType::Bytes => Self::Bytes(ScBytes::read_xdr(r)?),
299 ScValType::String => Self::String(ScString::read_xdr(r)?),
300 ScValType::Symbol => Self::Symbol(ScSymbol::read_xdr(r)?),
301 ScValType::Vec => Self::Vec(Option::<ScVec>::read_xdr(r)?),
302 ScValType::Map => Self::Map(Option::<ScMap>::read_xdr(r)?),
303 ScValType::Address => Self::Address(ScAddress::read_xdr(r)?),
304 ScValType::ContractInstance => {
305 Self::ContractInstance(ScContractInstance::read_xdr(r)?)
306 }
307 ScValType::LedgerKeyContractInstance => Self::LedgerKeyContractInstance,
308 ScValType::LedgerKeyNonce => Self::LedgerKeyNonce(ScNonceKey::read_xdr(r)?),
309 #[allow(unreachable_patterns)]
310 _ => return Err(Error::Invalid),
311 };
312 Ok(v)
313 })
314 }
315}
316
317impl WriteXdr for ScVal {
318 #[cfg(feature = "std")]
319 fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
320 w.with_limited_depth(|w| {
321 self.discriminant().write_xdr(w)?;
322 #[allow(clippy::match_same_arms)]
323 match self {
324 Self::Bool(v) => v.write_xdr(w)?,
325 Self::Void => ().write_xdr(w)?,
326 Self::Error(v) => v.write_xdr(w)?,
327 Self::U32(v) => v.write_xdr(w)?,
328 Self::I32(v) => v.write_xdr(w)?,
329 Self::U64(v) => v.write_xdr(w)?,
330 Self::I64(v) => v.write_xdr(w)?,
331 Self::Timepoint(v) => v.write_xdr(w)?,
332 Self::Duration(v) => v.write_xdr(w)?,
333 Self::U128(v) => v.write_xdr(w)?,
334 Self::I128(v) => v.write_xdr(w)?,
335 Self::U256(v) => v.write_xdr(w)?,
336 Self::I256(v) => v.write_xdr(w)?,
337 Self::Bytes(v) => v.write_xdr(w)?,
338 Self::String(v) => v.write_xdr(w)?,
339 Self::Symbol(v) => v.write_xdr(w)?,
340 Self::Vec(v) => v.write_xdr(w)?,
341 Self::Map(v) => v.write_xdr(w)?,
342 Self::Address(v) => v.write_xdr(w)?,
343 Self::ContractInstance(v) => v.write_xdr(w)?,
344 Self::LedgerKeyContractInstance => ().write_xdr(w)?,
345 Self::LedgerKeyNonce(v) => v.write_xdr(w)?,
346 };
347 Ok(())
348 })
349 }
350}