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