multiversx_sc_scenario/scenario/model/esdt_data/
esdt_instances_check.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
use crate::scenario_format::{
    interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
    serde_raw::CheckEsdtInstancesRaw,
};

use super::CheckEsdtInstance;

#[derive(Debug, Clone)]
pub enum CheckEsdtInstances {
    Star,
    Equal(Vec<CheckEsdtInstance>),
}

impl CheckEsdtInstances {
    pub fn is_star(&self) -> bool {
        matches!(self, CheckEsdtInstances::Star)
    }

    pub fn contains_nonce(&self, nonce: u64) -> bool {
        match &self {
            CheckEsdtInstances::Equal(eq) => {
                for expected_value in eq.iter() {
                    if expected_value.nonce.value == nonce {
                        return true;
                    }
                }
            },
            CheckEsdtInstances::Star => {},
        }
        false
    }
}

impl Default for CheckEsdtInstances {
    fn default() -> Self {
        CheckEsdtInstances::Equal(Vec::new())
    }
}

impl InterpretableFrom<CheckEsdtInstancesRaw> for CheckEsdtInstances {
    fn interpret_from(from: CheckEsdtInstancesRaw, context: &InterpreterContext) -> Self {
        match from {
            CheckEsdtInstancesRaw::Unspecified => CheckEsdtInstances::Star,
            CheckEsdtInstancesRaw::Star => CheckEsdtInstances::Star,
            CheckEsdtInstancesRaw::Equal(m) => CheckEsdtInstances::Equal(
                m.into_iter()
                    .map(|v| CheckEsdtInstance::interpret_from(v, context))
                    .collect(),
            ),
        }
    }
}

impl IntoRaw<CheckEsdtInstancesRaw> for CheckEsdtInstances {
    fn into_raw(self) -> CheckEsdtInstancesRaw {
        match self {
            CheckEsdtInstances::Equal(eq) => {
                CheckEsdtInstancesRaw::Equal(eq.into_iter().map(|cei| cei.into_raw()).collect())
            },
            CheckEsdtInstances::Star => CheckEsdtInstancesRaw::Star,
        }
    }
}