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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use crate::*;

#[wasm_bindgen]
#[derive(
    Clone, Debug, Eq, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize, JsonSchema,
)]
pub struct NativeScripts(pub(crate) Vec<NativeScript>);

#[wasm_bindgen]
impl NativeScripts {
    pub fn new() -> Self {
        Self(Vec::new())
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn get(&self, index: usize) -> NativeScript {
        self.0[index].clone()
    }

    pub fn add(&mut self, elem: &NativeScript) {
        self.0.push(elem.clone());
    }

    pub(crate) fn deduplicated_view(&self) -> Vec<&NativeScript> {
        let mut dedup = BTreeSet::new();
        let mut scripts = Vec::new();
        for elem in &self.0 {
            if dedup.insert(elem) {
                scripts.push(elem);
            }
        }
        scripts
    }

    pub(crate) fn deduplicated_clone(&self) -> NativeScripts {
        let mut dedup = BTreeSet::new();
        let mut scripts = Vec::new();
        for script in &self.0 {
            if dedup.insert(script.clone()) {
                scripts.push(script.clone());
            }
        }
        NativeScripts(scripts)
    }

    #[allow(dead_code)]
    pub(crate) fn contains(&self, script: &NativeScript) -> bool {
        self.0.contains(script)
    }
}

impl_to_from!(NativeScripts);

impl From<Vec<NativeScript>> for NativeScripts {
    fn from(scripts: Vec<NativeScript>) -> Self {
        scripts.iter().fold(
            NativeScripts::new(),
            |mut scripts, s| {
                scripts.add(s);
                scripts
            },
        )
    }
}

impl NoneOrEmpty for NativeScripts {
    fn is_none_or_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl From<&NativeScripts> for Ed25519KeyHashes {
    fn from(scripts: &NativeScripts) -> Self {
        scripts
            .0
            .iter()
            .fold(Ed25519KeyHashes::new(), |mut set, s| {
                set.extend_move(Ed25519KeyHashes::from(s));
                set
            })
    }
}