summaryrefslogtreecommitdiffstats
path: root/src/entities.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/entities.rs')
-rw-r--r--src/entities.rs142
1 files changed, 112 insertions, 30 deletions
diff --git a/src/entities.rs b/src/entities.rs
index b7368df..89905c6 100644
--- a/src/entities.rs
+++ b/src/entities.rs
@@ -1,16 +1,17 @@
1use std::collections::HashMap; 1use std::collections::HashMap;
2 2
3pub(crate) type DynEntity = dyn Entity + Sync + Send;
4
3#[derive(Debug, Clone, Hash, PartialEq, Eq)] 5#[derive(Debug, Clone, Hash, PartialEq, Eq)]
4pub(crate) struct Usr(pub(crate) String); 6pub(crate) struct Usr(pub(crate) String);
5 7
6#[derive(Debug, Clone)]
7pub(crate) struct EntitiesManager { 8pub(crate) struct EntitiesManager {
8 toplevel_entities: HashMap<Usr, Entity>, 9 toplevel_entities: HashMap<Usr, Box<DynEntity>>,
9 descriptions: HashMap<Usr, Description>, 10 descriptions: HashMap<Usr, Description>,
10} 11}
11 12
12pub(crate) struct EntitiesManagerComponents { 13pub(crate) struct EntitiesManagerComponents {
13 pub(crate) toplevel_entities: HashMap<Usr, Entity>, 14 pub(crate) toplevel_entities: HashMap<Usr, Box<DynEntity>>,
14 pub(crate) descriptions: HashMap<Usr, Description>, 15 pub(crate) descriptions: HashMap<Usr, Description>,
15} 16}
16 17
@@ -27,7 +28,7 @@ impl EntitiesManager {
27 self.descriptions.insert(usr, description); 28 self.descriptions.insert(usr, description);
28 } 29 }
29 30
30 pub fn insert_toplevel(&mut self, usr: Usr, entity: Entity) { 31 pub fn insert_toplevel(&mut self, usr: Usr, entity: Box<DynEntity>) {
31 self.toplevel_entities.insert(usr, entity); 32 self.toplevel_entities.insert(usr, entity);
32 } 33 }
33 34
@@ -61,33 +62,88 @@ pub(crate) struct Described<T> {
61 pub(crate) entity: T, 62 pub(crate) entity: T,
62} 63}
63 64
65pub(crate) struct ChildGroup<'a> {
66 pub(crate) name: &'static str,
67 pub(crate) children: Vec<(&'a Usr, &'a DynEntity)>,
68}
69
70pub(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
64#[derive(Debug, Clone)] 80#[derive(Debug, Clone)]
65pub(crate) enum Entity { 81pub(crate) struct NameSpace {
82 // TODO: replace with Vec to keep order
83 pub(crate) members: HashMap<Usr, NameSpaceChild>,
84}
85
86#[derive(Debug, Clone)]
87pub(crate) enum NameSpaceChild {
66 NameSpace(NameSpace), 88 NameSpace(NameSpace),
89 Class(Class),
67 Variable(Variable), 90 Variable(Variable),
68 Function(Function), 91 Function(Function),
69 Class(Class),
70} 92}
71 93
72impl Entity { 94impl Entity for NameSpace {
73 fn kind_name(&self) -> &'static str { 95 fn kind_name(&self) -> &'static str {
74 match self { 96 "namespace"
75 Entity::NameSpace(_) => "namespace", 97 }
76 Entity::Variable(_) => "variable", 98
77 Entity::Function(_) => "function", 99 fn embeddable_children(&self) -> Vec<ChildGroup> {
78 Entity::Class(_) => "class", 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 }
79 } 109 }
110
111 vec![
112 ChildGroup {
113 name: "Variables",
114 children: variables,
115 },
116 ChildGroup {
117 name: "Functions",
118 children: functions,
119 },
120 ]
80 } 121 }
81}
82 122
83#[derive(Debug, Clone)] 123 fn separate_children(&self) -> Vec<ChildGroup> {
84pub(crate) struct NameSpace { 124 let mut namespaces = Vec::new();
85 pub(crate) members: HashMap<Usr, Entity>, 125 let mut classes = Vec::new();
86} 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 }
87 136
88impl From<NameSpace> for Entity { 137 vec![
89 fn from(ns: NameSpace) -> Self { 138 ChildGroup {
90 Entity::NameSpace(ns) 139 name: "Namespaces",
140 children: namespaces,
141 },
142 ChildGroup {
143 name: "Classes",
144 children: classes,
145 },
146 ]
91 } 147 }
92} 148}
93 149
@@ -96,9 +152,9 @@ pub(crate) struct Variable {
96 pub(crate) r#type: String, 152 pub(crate) r#type: String,
97} 153}
98 154
99impl From<Variable> for Entity { 155impl Entity for Variable {
100 fn from(var: Variable) -> Self { 156 fn kind_name(&self) -> &'static str {
101 Entity::Variable(var) 157 "variable"
102 } 158 }
103} 159}
104 160
@@ -108,9 +164,9 @@ pub(crate) struct Function {
108 pub(crate) return_type: String, 164 pub(crate) return_type: String,
109} 165}
110 166
111impl From<Function> for Entity { 167impl Entity for Function {
112 fn from(func: Function) -> Self { 168 fn kind_name(&self) -> &'static str {
113 Entity::Function(func) 169 "function"
114 } 170 }
115} 171}
116 172
@@ -122,13 +178,39 @@ pub(crate) struct FunctionArgument {
122 178
123#[derive(Debug, Clone)] 179#[derive(Debug, Clone)]
124pub(crate) struct Class { 180pub(crate) struct Class {
125 pub(crate) member_types: Vec<Usr>, 181 //pub(crate) member_types: Vec<Usr>,
182 // TODO: replace with Vec to keep order
126 pub(crate) member_functions: HashMap<Usr, Function>, 183 pub(crate) member_functions: HashMap<Usr, Function>,
127 pub(crate) member_variables: HashMap<Usr, Variable>, 184 pub(crate) member_variables: HashMap<Usr, Variable>,
128} 185}
129 186
130impl From<Class> for Entity { 187impl Entity for Class {
131 fn from(class: Class) -> Self { 188 fn kind_name(&self) -> &'static str {
132 Entity::Class(class) 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()
133 } 215 }
134} 216}