1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use analysis::{Description, Enum, Function};
use synthesis::{TestClass, TestFunction};

////////////////////////////////////////////////////////////////////////////////

macro_rules! implement_conversion {
    ($t:ident) => {
        impl EntityConversion for $t {
            fn convert(entity_type: &mut EntityType) -> Option<&mut $t> {
                match entity_type {
                    EntityType::$t(entity) => Some(entity),
                    _ => None,
                }
            }
        }
    };
}

/// The different types an Entitiy can have.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum EntityType {
    /// The index of a new entity hierarchy.
    Entity(Entity),

    /// An enumeration.
    Enum(Enum),

    /// A function.
    Function(Function),

    /// A test class.
    TestClass(TestClass),

    /// A test function.
    TestFunction(TestFunction),
}

pub trait EntityConversion {
    fn convert(entity_type: &mut EntityType) -> Option<&mut Self>;
}

implement_conversion!(Entity);
implement_conversion!(Enum);
implement_conversion!(Function);
implement_conversion!(TestClass);
implement_conversion!(TestFunction);

fn convert<T>(entity_type: &mut EntityType) -> Option<&mut T>
where
    T: EntityConversion,
{
    T::convert(entity_type)
}

////////////////////////////////////////////////////////////////////////////////

/// The representation of an Entity as a possbile generic node on the
/// abstract syntax tree. An Entity has to be kind of a EntityType.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Entity {
    pub name: String,
    pub entities: Vec<EntityType>,
    pub description: Option<Description>,
}

impl Entity {
    /// Creates a new Entity instance.
    ///
    /// # Example
    ///
    /// ```
    /// use thinlinelib::entity::Entity;
    ///
    /// let class = Entity::new("testClass");
    ///
    /// assert_eq!(class.name, "testClass");
    /// assert!(class.entities.is_empty());
    /// ```
    pub fn new<S: Into<String>>(name: S) -> Self {
        Self {
            name: name.into(),
            entities: Vec::new(),
            description: None,
        }
    }

    /// Adds an Entity to the Entity instance.
    ///
    /// # Example
    ///
    /// ```
    /// use thinlinelib::entity::{Entity, EntityType};
    ///
    /// let mut entity = Entity::new("outer_entity");
    /// let entity_type = EntityType::Entity(Entity::new("inner_entity"));
    /// entity.add_entity::<Entity>(entity_type);
    ///
    /// assert_eq!(entity.entities.len(), 1);
    /// ```
    pub fn add_entity<T>(&mut self, entity: EntityType) -> Option<&mut T>
    where
        T: EntityConversion,
    {
        self.entities.push(entity);
        if let Some(entity) = self.entities.last_mut() {
            return convert(entity);
        }

        None
    }

    /// Returns the functions of an entity.
    pub fn functions(&self) -> Vec<&Function> {
        let mut entity_vec: Vec<&Function> = Vec::new();
        for entity in &self.entities {
            if let EntityType::Function(fct) = entity {
                entity_vec.push(&fct);
            }
        }
        return entity_vec;
    }

    /// Sets the description for the Entity.
    pub fn set_description(&mut self, description: &str) {
        if self.description.is_none() {
            self.description = Some(Description::new());
        }

        if let Some(desc) = &mut self.description {
            desc.set(description);
        }
    }
}