multiversx_sc_scenario/facade/expr/
mxsc_path.rs1use multiversx_chain_scenario_format::{
2 interpret_trait::InterpreterContext, value_interpreter::interpret_string,
3};
4use multiversx_sc::types::{AnnotatedValue, ManagedBuffer, TxCodeValue};
5
6use crate::{ScenarioTxEnv, ScenarioTxEnvData};
7
8use super::RegisterCodeSource;
9
10const MXSC_PREFIX: &str = "mxsc:";
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13pub struct MxscPath<'a> {
14 path: &'a str,
15}
16
17impl<'a> MxscPath<'a> {
18 pub const fn new(path: &'a str) -> Self {
19 MxscPath { path }
20 }
21}
22
23impl MxscPath<'_> {
24 pub fn eval_to_expr(&self) -> String {
25 format!("{MXSC_PREFIX}{}", self.path)
26 }
27
28 pub fn resolve_contents(&self, context: &InterpreterContext) -> Vec<u8> {
29 interpret_string(&format!("{MXSC_PREFIX}{}", self.path), context)
30 }
31}
32
33impl<Env> AnnotatedValue<Env, ManagedBuffer<Env::Api>> for MxscPath<'_>
34where
35 Env: ScenarioTxEnv,
36{
37 fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
38 self.eval_to_expr().into()
39 }
40
41 fn to_value(&self, env: &Env) -> ManagedBuffer<Env::Api> {
42 self.resolve_contents(&env.env_data().interpreter_context())
43 .into()
44 }
45}
46
47impl<Env> TxCodeValue<Env> for MxscPath<'_> where Env: ScenarioTxEnv {}
48
49impl RegisterCodeSource for MxscPath<'_> {
50 fn into_code(self, env_data: ScenarioTxEnvData) -> Vec<u8> {
51 self.resolve_contents(&env_data.interpreter_context())
52 }
53}
54
55#[cfg(test)]
56pub mod tests {
57 use crate::imports::MxscPath;
58
59 fn assert_eq_eval(expr: &'static str, expected: &str) {
60 assert_eq!(&MxscPath::new(expr).eval_to_expr(), expected);
61 }
62
63 #[test]
64 fn test_address_value() {
65 assert_eq_eval("output/adder.mxsc.json", "mxsc:output/adder.mxsc.json");
66 }
67}