1use analysis::{Description, Enum, Function};
2use synthesis::{TestClass, TestFunction};
3
4macro_rules! implement_conversion {
7 ($t:ident) => {
8 impl EntityConversion for $t {
9 fn convert(entity_type: &mut EntityType) -> Option<&mut $t> {
10 match entity_type {
11 EntityType::$t(entity) => Some(entity),
12 _ => None,
13 }
14 }
15 }
16 };
17}
18
19#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
21pub enum EntityType {
22 Entity(Entity),
24
25 Enum(Enum),
27
28 Function(Function),
30
31 TestClass(TestClass),
33
34 TestFunction(TestFunction),
36}
37
38pub trait EntityConversion {
39 fn convert(entity_type: &mut EntityType) -> Option<&mut Self>;
40}
41
42implement_conversion!(Entity);
43implement_conversion!(Enum);
44implement_conversion!(Function);
45implement_conversion!(TestClass);
46implement_conversion!(TestFunction);
47
48fn convert<T>(entity_type: &mut EntityType) -> Option<&mut T>
49where
50 T: EntityConversion,
51{
52 T::convert(entity_type)
53}
54
55#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
60pub struct Entity {
61 pub name: String,
62 pub entities: Vec<EntityType>,
63 pub description: Option<Description>,
64}
65
66impl Entity {
67 pub fn new<S: Into<String>>(name: S) -> Self {
80 Self {
81 name: name.into(),
82 entities: Vec::new(),
83 description: None,
84 }
85 }
86
87 pub fn add_entity<T>(&mut self, entity: EntityType) -> Option<&mut T>
101 where
102 T: EntityConversion,
103 {
104 self.entities.push(entity);
105 if let Some(entity) = self.entities.last_mut() {
106 return convert(entity);
107 }
108
109 None
110 }
111
112 pub fn functions(&self) -> Vec<&Function> {
114 let mut entity_vec: Vec<&Function> = Vec::new();
115 for entity in &self.entities {
116 if let EntityType::Function(fct) = entity {
117 entity_vec.push(&fct);
118 }
119 }
120 return entity_vec;
121 }
122
123 pub fn set_description(&mut self, description: &str) {
125 if self.description.is_none() {
126 self.description = Some(Description::new());
127 }
128
129 if let Some(desc) = &mut self.description {
130 desc.set(description);
131 }
132 }
133}