gesha_rust_types/
definitions.rs

1use crate::Definition;
2
3#[derive(Clone, Debug, PartialEq, Default)]
4pub struct Definitions(Vec<Definition>);
5
6impl Definitions {
7    pub fn new() -> Self {
8        Self(vec![])
9    }
10
11    pub fn is_empty(&self) -> bool {
12        self.0.is_empty()
13    }
14
15    pub fn set<A: Into<Definition>>(&mut self, def: A) {
16        // TODO: return error if definition already pushed
17        self.0.push(def.into());
18    }
19
20    pub fn iter(&self) -> impl Iterator<Item = &Definition> {
21        self.0.iter()
22    }
23
24    pub fn from<A, E>(xs: Vec<A>) -> Result<Self, E>
25    where
26        A: TryInto<Definition, Error = E>,
27    {
28        xs.into_iter()
29            .map(|x| x.try_into())
30            .collect::<Result<Vec<_>, E>>()
31            .map(Self)
32    }
33}
34
35impl FromIterator<Definition> for Definitions {
36    fn from_iter<T: IntoIterator<Item = Definition>>(iter: T) -> Self {
37        let set = iter.into_iter().collect();
38        Self(set)
39    }
40}
41
42impl IntoIterator for Definitions {
43    type Item = <Vec<Definition> as IntoIterator>::Item;
44    type IntoIter = <Vec<Definition> as IntoIterator>::IntoIter;
45
46    fn into_iter(self) -> Self::IntoIter {
47        IntoIterator::into_iter(self.0)
48    }
49}