multiversx_sc_scenario/scenario/model/value/
value_set_bytes.rs

1use multiversx_sc::types::{AnnotatedValue, ManagedBuffer, TxCodeValue, TxEnv};
2
3use crate::scenario_format::{
4    interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
5    serde_raw::ValueSubTree,
6    value_interpreter::{interpret_string, interpret_subtree},
7};
8
9use std::fmt;
10
11use super::BytesKey;
12
13#[derive(Clone, Debug)]
14pub struct BytesValue {
15    pub value: Vec<u8>,
16    pub original: ValueSubTree,
17}
18
19impl BytesValue {
20    pub fn empty() -> Self {
21        BytesValue {
22            value: Vec::new(),
23            original: ValueSubTree::Str(String::default()),
24        }
25    }
26
27    /// Creates a `0x...` expression directly.
28    pub fn from_hex(hex_value: &str) -> Self {
29        Self {
30            value: hex::decode(hex_value).expect("could not decode hex value"),
31            original: ValueSubTree::Str(format!("0x{hex_value}")),
32        }
33    }
34
35    /// Creates a `str:...` expression directly.
36    pub fn from_str_expr(value: &str) -> Self {
37        Self {
38            value: value.as_bytes().to_owned(),
39            original: ValueSubTree::Str(format!("str:{value}")),
40        }
41    }
42}
43
44impl InterpretableFrom<ValueSubTree> for BytesValue {
45    fn interpret_from(from: ValueSubTree, context: &InterpreterContext) -> Self {
46        BytesValue {
47            value: interpret_subtree(&from, context),
48            original: from,
49        }
50    }
51}
52
53impl IntoRaw<ValueSubTree> for BytesValue {
54    fn into_raw(self) -> ValueSubTree {
55        self.original
56    }
57}
58
59impl InterpretableFrom<&str> for BytesValue {
60    fn interpret_from(from: &str, context: &InterpreterContext) -> Self {
61        BytesValue {
62            value: interpret_string(from, context),
63            original: ValueSubTree::Str(from.to_string()),
64        }
65    }
66}
67
68impl InterpretableFrom<String> for BytesValue {
69    fn interpret_from(from: String, context: &InterpreterContext) -> Self {
70        BytesValue {
71            value: interpret_string(from.as_str(), context),
72            original: ValueSubTree::Str(from),
73        }
74    }
75}
76
77impl From<&str> for BytesValue {
78    fn from(from: &str) -> Self {
79        BytesValue::interpret_from(from, &InterpreterContext::default())
80    }
81}
82
83impl From<String> for BytesValue {
84    fn from(from: String) -> Self {
85        BytesValue::interpret_from(from, &InterpreterContext::default())
86    }
87}
88
89impl From<&[u8]> for BytesValue {
90    fn from(bytes: &[u8]) -> Self {
91        let expr = format!("0x{}", hex::encode(bytes));
92        BytesValue {
93            value: bytes.to_vec(),
94            original: ValueSubTree::Str(expr),
95        }
96    }
97}
98
99impl From<Vec<u8>> for BytesValue {
100    fn from(v: Vec<u8>) -> Self {
101        Self::from(v.as_slice())
102    }
103}
104
105impl From<BytesKey> for BytesValue {
106    fn from(from: BytesKey) -> Self {
107        BytesValue {
108            value: from.value,
109            original: ValueSubTree::Str(from.original),
110        }
111    }
112}
113
114impl From<&BytesKey> for BytesValue {
115    fn from(from: &BytesKey) -> Self {
116        BytesValue {
117            value: from.value.clone(),
118            original: ValueSubTree::Str(from.original.clone()),
119        }
120    }
121}
122
123impl From<&BytesValue> for BytesValue {
124    fn from(from: &BytesValue) -> Self {
125        from.clone()
126    }
127}
128
129impl From<(&str, &InterpreterContext)> for BytesValue {
130    fn from(from: (&str, &InterpreterContext)) -> Self {
131        BytesValue::interpret_from(from.0, from.1)
132    }
133}
134
135impl From<()> for BytesValue {
136    fn from(_: ()) -> Self {
137        BytesValue::from("")
138    }
139}
140
141impl Default for BytesValue {
142    fn default() -> Self {
143        Self {
144            value: Vec::new(),
145            original: ValueSubTree::Str(String::new()),
146        }
147    }
148}
149
150impl fmt::Display for BytesValue {
151    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152        self.original.fmt(f)
153    }
154}
155
156impl<Env> AnnotatedValue<Env, ManagedBuffer<Env::Api>> for BytesValue
157where
158    Env: TxEnv,
159{
160    fn annotation(&self, _env: &Env) -> ManagedBuffer<<Env as TxEnv>::Api> {
161        self.original.to_concatenated_string().into()
162    }
163
164    fn to_value(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
165        self.value.clone().into()
166    }
167}
168
169impl<Env> TxCodeValue<Env> for BytesValue where Env: TxEnv {}
170
171impl<Env> AnnotatedValue<Env, ManagedBuffer<Env::Api>> for &BytesValue
172where
173    Env: TxEnv,
174{
175    fn annotation(&self, _env: &Env) -> ManagedBuffer<<Env as TxEnv>::Api> {
176        self.original.to_concatenated_string().into()
177    }
178
179    fn to_value(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
180        self.value.clone().into()
181    }
182}
183
184impl<Env> TxCodeValue<Env> for &BytesValue where Env: TxEnv {}