multiversx_sc_scenario/facade/expr/
mxsc_path.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use multiversx_chain_scenario_format::{
    interpret_trait::InterpreterContext, value_interpreter::interpret_string,
};
use multiversx_sc::types::{AnnotatedValue, ManagedBuffer, TxCodeValue};

use crate::{ScenarioTxEnv, ScenarioTxEnvData};

use super::RegisterCodeSource;

const MXSC_PREFIX: &str = "mxsc:";

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MxscPath<'a> {
    path: &'a str,
}

impl<'a> MxscPath<'a> {
    pub const fn new(path: &'a str) -> Self {
        MxscPath { path }
    }
}

impl MxscPath<'_> {
    pub fn eval_to_expr(&self) -> String {
        format!("{MXSC_PREFIX}{}", self.path)
    }

    pub fn resolve_contents(&self, context: &InterpreterContext) -> Vec<u8> {
        interpret_string(&format!("{MXSC_PREFIX}{}", self.path), context)
    }
}

impl<Env> AnnotatedValue<Env, ManagedBuffer<Env::Api>> for MxscPath<'_>
where
    Env: ScenarioTxEnv,
{
    fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
        self.eval_to_expr().into()
    }

    fn to_value(&self, env: &Env) -> ManagedBuffer<Env::Api> {
        self.resolve_contents(&env.env_data().interpreter_context())
            .into()
    }
}

impl<Env> TxCodeValue<Env> for MxscPath<'_> where Env: ScenarioTxEnv {}

impl RegisterCodeSource for MxscPath<'_> {
    fn into_code(self, env_data: ScenarioTxEnvData) -> Vec<u8> {
        self.resolve_contents(&env_data.interpreter_context())
    }
}

#[cfg(test)]
pub mod tests {
    use crate::imports::MxscPath;

    fn assert_eq_eval(expr: &'static str, expected: &str) {
        assert_eq!(&MxscPath::new(expr).eval_to_expr(), expected);
    }

    #[test]
    fn test_address_value() {
        assert_eq_eval("output/adder.mxsc.json", "mxsc:output/adder.mxsc.json");
    }
}