1pub mod import;
4
5#[cfg(any(test, feature = "mocks"))]
6pub mod mock;
7
8pub use import::*;
9use ::is_tree::*;
10
11use crate::prelude::*;
12use crate::{Visibility, Attributes, Function, Object, Identifier, TypeDefinition};
13use crate::interface::Interface;
14
15#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, IsTree)]
17pub struct Module {
18    pub attributes: Attributes,
20    pub visibility: Visibility,
22    #[tree(path_segment)]
24    pub identifier: Identifier,
25    pub imports: Vec<Import>,
27    pub objects: Vec<Object>,
29    pub functions: Vec<Function>,
31    pub types: Vec<TypeDefinition>,
33    pub interfaces: Vec<Interface>,
35    #[tree(branch)]
37    pub modules: Vec<Module>,
38}
39
40impl CountSymbols for Module {
41    fn count_symbols(&self) -> usize {
42        self.objects.len()
43            + self.functions.count_symbols()
44            + self.types.count_symbols()
45            + self.interfaces.count_symbols()
46            + self.modules.count_symbols()
47    }
48}
49
50impl CountSymbols for &mut Module {
51    fn count_symbols(&self) -> usize {
52        self.objects.len()
53            + self.functions.count_symbols()
54            + self.types.count_symbols()
55            + self.interfaces.count_symbols()
56            + self.modules.count_symbols()
57    }    
58}
59
60impl CountSymbols for &Module {
61    fn count_symbols(&self) -> usize {
62        self.objects.len()
63            + self.functions.count_symbols()
64            + self.types.count_symbols()
65            + self.interfaces.count_symbols()
66            + self.modules.count_symbols()
67    }    
68}
69
70impl CountSymbols for Vec<Module> {
71    fn count_symbols(&self) -> usize {
72        self.iter().fold(0, |acc, module| acc + module.count_symbols())
73    }
74}
75
76impl CountSymbols for &Vec<Module> {
77    fn count_symbols(&self) -> usize {
78        self.iter().fold(0, |acc, module| acc + module.count_symbols())
79    }
80}
81
82impl Module {
83    pub fn join(&mut self, other: Self) {
84        self.interfaces.extend(other.interfaces);
85        self.functions.extend(other.functions);
86        self.types.extend(other.types);
87        self.objects.extend(other.objects);
88        self.modules.extend(other.modules);
89        self.imports.extend(other.imports);
90    }
91
92    pub fn is_empty(&self) -> bool {
93        self.objects.is_empty()
94            && self.functions.is_empty()
95            && self.interfaces.is_empty()
96            && self.types.is_empty()
97            && self.modules.is_empty()
98    }
99}