multiversx_sc_scenario/scenario/model/value/
value_set_big_uint.rs

1use crate::scenario_format::{
2    interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
3    serde_raw::ValueSubTree,
4    value_interpreter::{interpret_string, interpret_subtree},
5};
6
7use crate::multiversx_sc::api::ManagedTypeApi;
8use num_bigint::BigUint;
9use num_traits::Zero;
10use std::fmt;
11
12#[derive(Debug, Clone)]
13pub struct BigUintValue {
14    pub value: BigUint,
15    pub original: ValueSubTree,
16}
17
18impl BigUintValue {
19    pub fn is_zero(&self) -> bool {
20        self.value.is_zero()
21    }
22}
23
24impl InterpretableFrom<ValueSubTree> for BigUintValue {
25    fn interpret_from(from: ValueSubTree, context: &InterpreterContext) -> Self {
26        let bytes = interpret_subtree(&from, context);
27        BigUintValue {
28            value: BigUint::from_bytes_be(&bytes),
29            original: from,
30        }
31    }
32}
33
34impl InterpretableFrom<&str> for BigUintValue {
35    fn interpret_from(from: &str, context: &InterpreterContext) -> Self {
36        let bytes = interpret_string(from, context);
37        BigUintValue {
38            value: BigUint::from_bytes_be(&bytes),
39            original: ValueSubTree::Str(from.to_string()),
40        }
41    }
42}
43
44impl IntoRaw<ValueSubTree> for BigUintValue {
45    fn into_raw(self) -> ValueSubTree {
46        self.original
47    }
48}
49
50impl BigUintValue {
51    pub fn into_raw_opt(self) -> Option<ValueSubTree> {
52        if self.value == 0u32.into() {
53            None
54        } else {
55            Some(self.into_raw())
56        }
57    }
58}
59
60impl From<u32> for BigUintValue {
61    fn from(from: u32) -> Self {
62        BigUintValue {
63            value: from.into(),
64            original: ValueSubTree::Str(from.to_string()),
65        }
66    }
67}
68
69impl From<u64> for BigUintValue {
70    fn from(from: u64) -> Self {
71        BigUintValue {
72            value: from.into(),
73            original: ValueSubTree::Str(from.to_string()),
74        }
75    }
76}
77
78impl From<u128> for BigUintValue {
79    fn from(from: u128) -> Self {
80        BigUintValue {
81            value: from.into(),
82            original: ValueSubTree::Str(from.to_string()),
83        }
84    }
85}
86
87impl From<BigUint> for BigUintValue {
88    fn from(from: BigUint) -> Self {
89        let s = from.to_string();
90        BigUintValue {
91            value: from,
92            original: ValueSubTree::Str(s),
93        }
94    }
95}
96
97impl From<&BigUint> for BigUintValue {
98    fn from(from: &BigUint) -> Self {
99        Self::from(from.clone())
100    }
101}
102
103impl<M: ManagedTypeApi> From<crate::multiversx_sc::types::BigUint<M>> for BigUintValue {
104    fn from(from: crate::multiversx_sc::types::BigUint<M>) -> Self {
105        let value = from.to_alloc();
106        BigUintValue {
107            original: ValueSubTree::Str(value.to_string()),
108            value,
109        }
110    }
111}
112
113impl<M: ManagedTypeApi> From<crate::multiversx_sc::types::AnnotatedEgldPayment<M>>
114    for BigUintValue
115{
116    fn from(from: crate::multiversx_sc::types::AnnotatedEgldPayment<M>) -> Self {
117        BigUintValue {
118            value: from.value.to_alloc(),
119            original: ValueSubTree::Str(from.annotation.to_string()),
120        }
121    }
122}
123
124impl From<&str> for BigUintValue {
125    fn from(from: &str) -> Self {
126        BigUintValue::interpret_from(from, &InterpreterContext::default())
127    }
128}
129
130impl fmt::Display for BigUintValue {
131    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132        self.original.fmt(f)
133    }
134}
135
136impl Default for BigUintValue {
137    fn default() -> Self {
138        BigUintValue {
139            original: ValueSubTree::default(),
140            value: BigUint::from(0u32),
141        }
142    }
143}