multiversx_sc_scenario/scenario/model/value/
value_set_u64.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 multiversx_sc::chain_core::types::ReturnCode;
8use num_bigint::BigUint;
9use num_traits::ToPrimitive;
10use std::fmt;
11
12#[derive(Debug, Clone)]
13pub struct U64Value {
14    pub value: u64,
15    pub original: ValueSubTree,
16}
17
18impl U64Value {
19    pub fn empty() -> Self {
20        U64Value {
21            value: 0,
22            original: ValueSubTree::Str(String::default()),
23        }
24    }
25
26    pub fn zero() -> Self {
27        U64Value {
28            value: 0,
29            original: ValueSubTree::Str("0".to_string()),
30        }
31    }
32}
33
34impl Default for U64Value {
35    fn default() -> Self {
36        U64Value::empty()
37    }
38}
39
40impl InterpretableFrom<u64> for U64Value {
41    fn interpret_from(from: u64, _context: &InterpreterContext) -> Self {
42        U64Value {
43            value: from,
44            original: ValueSubTree::Str(from.to_string()),
45        }
46    }
47}
48
49impl InterpretableFrom<ValueSubTree> for U64Value {
50    fn interpret_from(from: ValueSubTree, context: &InterpreterContext) -> Self {
51        let bytes = interpret_subtree(&from, context);
52        let bu = BigUint::from_bytes_be(&bytes);
53        U64Value {
54            value: bu.to_u64().unwrap(),
55            original: from,
56        }
57    }
58}
59
60impl InterpretableFrom<&str> for U64Value {
61    fn interpret_from(from: &str, context: &InterpreterContext) -> Self {
62        let bytes = interpret_string(from, context);
63        let bu = BigUint::from_bytes_be(&bytes);
64        U64Value {
65            value: bu.to_u64().unwrap(),
66            original: ValueSubTree::Str(from.to_string()),
67        }
68    }
69}
70
71impl IntoRaw<ValueSubTree> for U64Value {
72    fn into_raw(self) -> ValueSubTree {
73        self.original
74    }
75}
76
77impl U64Value {
78    pub fn into_raw_opt(self) -> Option<ValueSubTree> {
79        if self.value > 0 {
80            Some(self.into_raw())
81        } else {
82            None
83        }
84    }
85}
86
87impl From<u64> for U64Value {
88    fn from(from: u64) -> Self {
89        U64Value {
90            value: from,
91            original: ValueSubTree::Str(from.to_string()),
92        }
93    }
94}
95
96impl From<u32> for U64Value {
97    fn from(from: u32) -> Self {
98        U64Value {
99            value: from as u64,
100            original: ValueSubTree::Str(from.to_string()),
101        }
102    }
103}
104
105impl From<i32> for U64Value {
106    fn from(from: i32) -> Self {
107        assert!(from >= 0, "U64Value cannot be negative");
108        Self::from(from as u32)
109    }
110}
111
112impl From<ReturnCode> for U64Value {
113    fn from(from: ReturnCode) -> Self {
114        U64Value::from(from.as_u64())
115    }
116}
117
118impl From<&str> for U64Value {
119    fn from(from: &str) -> Self {
120        U64Value::interpret_from(from, &InterpreterContext::default())
121    }
122}
123
124impl fmt::Display for U64Value {
125    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126        self.original.fmt(f)
127    }
128}