multiversx_sc_scenario/facade/expr/
num_expr.rs

1use crate::ScenarioTxEnv;
2
3use multiversx_chain_scenario_format::{
4    interpret_trait::InterpreterContext, value_interpreter::interpret_string,
5};
6use multiversx_sc::{
7    api::ManagedTypeApi,
8    types::{AnnotatedValue, BigUint, ManagedBuffer, TxEgldValue, TxGasValue},
9};
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub struct NumExpr<'a>(pub &'a str);
13
14fn interpret_big_uint<Api>(s: &str) -> BigUint<Api>
15where
16    Api: ManagedTypeApi,
17{
18    let bytes = interpret_string(s, &InterpreterContext::new());
19    BigUint::from_bytes_be(&bytes)
20}
21
22impl<Env> AnnotatedValue<Env, BigUint<Env::Api>> for NumExpr<'_>
23where
24    Env: ScenarioTxEnv,
25{
26    fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
27        self.0.into()
28    }
29
30    fn to_value(&self, _env: &Env) -> BigUint<Env::Api> {
31        interpret_big_uint(self.0)
32    }
33}
34
35impl<Env> AnnotatedValue<Env, u64> for NumExpr<'_>
36where
37    Env: ScenarioTxEnv,
38{
39    fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
40        self.0.into()
41    }
42
43    fn to_value(&self, _env: &Env) -> u64 {
44        interpret_big_uint::<Env::Api>(self.0).to_u64().unwrap()
45    }
46}
47
48impl<Env> TxEgldValue<Env> for NumExpr<'_> where Env: ScenarioTxEnv {}
49impl<Env> TxGasValue<Env> for NumExpr<'_> where Env: ScenarioTxEnv {}