dharitri_sc/abi/
type_description_container.rs

1use super::*;
2use dharitri_sc_codec::Vec;
3
4pub trait TypeDescriptionContainer {
5    fn new() -> Self;
6
7    fn contains_type(&self, type_name: &str) -> bool;
8
9    // A placeholder gets inserted while computing field descriptions for a type,
10    // to avoid an infinite loop for recursive types (if the same type appears again lower in the tree).
11    fn reserve_type_name(&mut self, type_names: TypeNames) {
12        self.insert(type_names, TypeDescription::PLACEHOLDER);
13    }
14
15    fn insert(&mut self, type_names: TypeNames, type_description: TypeDescription);
16
17    fn insert_all(&mut self, other: &Self);
18}
19
20#[derive(Clone, Default, Debug)]
21pub struct TypeDescriptionContainerImpl(pub Vec<(TypeNames, TypeDescription)>);
22
23impl TypeDescriptionContainer for TypeDescriptionContainerImpl {
24    fn new() -> Self {
25        TypeDescriptionContainerImpl(Vec::new())
26    }
27
28    fn contains_type(&self, type_name: &str) -> bool {
29        self.0
30            .iter()
31            .any(|(existing_type_name, _)| existing_type_name.abi == type_name)
32    }
33
34    fn insert(&mut self, type_names: TypeNames, type_description: TypeDescription) {
35        if let Some((_existing_type_name, existing_type_description)) = self
36            .0
37            .iter_mut()
38            .find(|(name, _)| name.abi == type_names.abi)
39        {
40            *existing_type_description = type_description;
41        } else {
42            self.0.push((type_names, type_description));
43        }
44    }
45
46    fn insert_all(&mut self, other: &Self) {
47        for (key, value) in other.0.iter() {
48            self.insert(key.clone(), value.clone());
49        }
50    }
51}