diff options
Diffstat (limited to 'src/entities.rs')
-rw-r--r-- | src/entities.rs | 216 |
1 files changed, 0 insertions, 216 deletions
diff --git a/src/entities.rs b/src/entities.rs deleted file mode 100644 index 89905c6..0000000 --- a/src/entities.rs +++ /dev/null | |||
@@ -1,216 +0,0 @@ | |||
1 | use std::collections::HashMap; | ||
2 | |||
3 | pub(crate) type DynEntity = dyn Entity + Sync + Send; | ||
4 | |||
5 | #[derive(Debug, Clone, Hash, PartialEq, Eq)] | ||
6 | pub(crate) struct Usr(pub(crate) String); | ||
7 | |||
8 | pub(crate) struct EntitiesManager { | ||
9 | toplevel_entities: HashMap<Usr, Box<DynEntity>>, | ||
10 | descriptions: HashMap<Usr, Description>, | ||
11 | } | ||
12 | |||
13 | pub(crate) struct EntitiesManagerComponents { | ||
14 | pub(crate) toplevel_entities: HashMap<Usr, Box<DynEntity>>, | ||
15 | pub(crate) descriptions: HashMap<Usr, Description>, | ||
16 | } | ||
17 | |||
18 | impl EntitiesManager { | ||
19 | pub fn new() -> Self { | ||
20 | EntitiesManager { | ||
21 | toplevel_entities: HashMap::new(), | ||
22 | descriptions: HashMap::new(), | ||
23 | } | ||
24 | } | ||
25 | |||
26 | pub fn insert(&mut self, usr: Usr, description: Description) { | ||
27 | info!("Found entity {:?}", description.name); | ||
28 | self.descriptions.insert(usr, description); | ||
29 | } | ||
30 | |||
31 | pub fn insert_toplevel(&mut self, usr: Usr, entity: Box<DynEntity>) { | ||
32 | self.toplevel_entities.insert(usr, entity); | ||
33 | } | ||
34 | |||
35 | pub fn decompose(self) -> EntitiesManagerComponents { | ||
36 | EntitiesManagerComponents { | ||
37 | toplevel_entities: self.toplevel_entities, | ||
38 | descriptions: self.descriptions, | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | |||
43 | #[derive(Debug, Clone, Default)] | ||
44 | pub(crate) struct Description { | ||
45 | pub(crate) name: String, | ||
46 | pub(crate) brief: String, | ||
47 | pub(crate) detailed: String, | ||
48 | } | ||
49 | |||
50 | impl Description { | ||
51 | pub(crate) fn with_name(name: String) -> Self { | ||
52 | Description { | ||
53 | name, | ||
54 | ..Default::default() | ||
55 | } | ||
56 | } | ||
57 | } | ||
58 | |||
59 | #[derive(Debug, Clone)] | ||
60 | pub(crate) struct Described<T> { | ||
61 | pub(crate) description: Description, | ||
62 | pub(crate) entity: T, | ||
63 | } | ||
64 | |||
65 | pub(crate) struct ChildGroup<'a> { | ||
66 | pub(crate) name: &'static str, | ||
67 | pub(crate) children: Vec<(&'a Usr, &'a DynEntity)>, | ||
68 | } | ||
69 | |||
70 | pub(crate) trait Entity { | ||
71 | fn kind_name(&self) -> &'static str; | ||
72 | fn embeddable_children(&self) -> Vec<ChildGroup> { | ||
73 | Vec::new() | ||
74 | } | ||
75 | fn separate_children(&self) -> Vec<ChildGroup> { | ||
76 | Vec::new() | ||
77 | } | ||
78 | } | ||
79 | |||
80 | #[derive(Debug, Clone)] | ||
81 | pub(crate) struct NameSpace { | ||
82 | // TODO: replace with Vec to keep order | ||
83 | pub(crate) members: HashMap<Usr, NameSpaceChild>, | ||
84 | } | ||
85 | |||
86 | #[derive(Debug, Clone)] | ||
87 | pub(crate) enum NameSpaceChild { | ||
88 | NameSpace(NameSpace), | ||
89 | Class(Class), | ||
90 | Variable(Variable), | ||
91 | Function(Function), | ||
92 | } | ||
93 | |||
94 | impl Entity for NameSpace { | ||
95 | fn kind_name(&self) -> &'static str { | ||
96 | "namespace" | ||
97 | } | ||
98 | |||
99 | fn embeddable_children(&self) -> Vec<ChildGroup> { | ||
100 | let mut variables = Vec::new(); | ||
101 | let mut functions = Vec::new(); | ||
102 | for (usr, member) in &self.members { | ||
103 | match member { | ||
104 | NameSpaceChild::NameSpace(_) => {} | ||
105 | NameSpaceChild::Class(_) => {} | ||
106 | NameSpaceChild::Variable(variable) => variables.push((usr, variable as &DynEntity)), | ||
107 | NameSpaceChild::Function(function) => functions.push((usr, function as &DynEntity)), | ||
108 | } | ||
109 | } | ||
110 | |||
111 | vec![ | ||
112 | ChildGroup { | ||
113 | name: "Variables", | ||
114 | children: variables, | ||
115 | }, | ||
116 | ChildGroup { | ||
117 | name: "Functions", | ||
118 | children: functions, | ||
119 | }, | ||
120 | ] | ||
121 | } | ||
122 | |||
123 | fn separate_children(&self) -> Vec<ChildGroup> { | ||
124 | let mut namespaces = Vec::new(); | ||
125 | let mut classes = Vec::new(); | ||
126 | for (usr, member) in &self.members { | ||
127 | match member { | ||
128 | NameSpaceChild::NameSpace(namespace) => { | ||
129 | namespaces.push((usr, namespace as &DynEntity)) | ||
130 | } | ||
131 | NameSpaceChild::Class(class) => classes.push((usr, class as &DynEntity)), | ||
132 | NameSpaceChild::Variable(_) => {} | ||
133 | NameSpaceChild::Function(_) => {} | ||
134 | } | ||
135 | } | ||
136 | |||
137 | vec![ | ||
138 | ChildGroup { | ||
139 | name: "Namespaces", | ||
140 | children: namespaces, | ||
141 | }, | ||
142 | ChildGroup { | ||
143 | name: "Classes", | ||
144 | children: classes, | ||
145 | }, | ||
146 | ] | ||
147 | } | ||
148 | } | ||
149 | |||
150 | #[derive(Debug, Clone)] | ||
151 | pub(crate) struct Variable { | ||
152 | pub(crate) r#type: String, | ||
153 | } | ||
154 | |||
155 | impl Entity for Variable { | ||
156 | fn kind_name(&self) -> &'static str { | ||
157 | "variable" | ||
158 | } | ||
159 | } | ||
160 | |||
161 | #[derive(Debug, Clone)] | ||
162 | pub(crate) struct Function { | ||
163 | pub(crate) arguments: Vec<FunctionArgument>, | ||
164 | pub(crate) return_type: String, | ||
165 | } | ||
166 | |||
167 | impl Entity for Function { | ||
168 | fn kind_name(&self) -> &'static str { | ||
169 | "function" | ||
170 | } | ||
171 | } | ||
172 | |||
173 | #[derive(Debug, Clone)] | ||
174 | pub(crate) struct FunctionArgument { | ||
175 | pub(crate) name: String, | ||
176 | pub(crate) r#type: String, | ||
177 | } | ||
178 | |||
179 | #[derive(Debug, Clone)] | ||
180 | pub(crate) struct Class { | ||
181 | //pub(crate) member_types: Vec<Usr>, | ||
182 | // TODO: replace with Vec to keep order | ||
183 | pub(crate) member_functions: HashMap<Usr, Function>, | ||
184 | pub(crate) member_variables: HashMap<Usr, Variable>, | ||
185 | } | ||
186 | |||
187 | impl Entity for Class { | ||
188 | fn kind_name(&self) -> &'static str { | ||
189 | "class" | ||
190 | } | ||
191 | |||
192 | fn embeddable_children(&self) -> Vec<ChildGroup> { | ||
193 | vec![ | ||
194 | ChildGroup { | ||
195 | name: "Functions", | ||
196 | children: self | ||
197 | .member_functions | ||
198 | .iter() | ||
199 | .map(|(usr, func)| (usr, func as &DynEntity)) | ||
200 | .collect(), | ||
201 | }, | ||
202 | ChildGroup { | ||
203 | name: "Variables", | ||
204 | children: self | ||
205 | .member_variables | ||
206 | .iter() | ||
207 | .map(|(usr, var)| (usr, var as &DynEntity)) | ||
208 | .collect(), | ||
209 | }, | ||
210 | ] | ||
211 | } | ||
212 | |||
213 | fn separate_children(&self) -> Vec<ChildGroup> { | ||
214 | Vec::new() | ||
215 | } | ||
216 | } | ||