summaryrefslogtreecommitdiffstats
path: root/src/entities.rs
blob: b7368df079fede561c355ea95bb592493176e127 (plain)
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
134
use std::collections::HashMap;

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub(crate) struct Usr(pub(crate) String);

#[derive(Debug, Clone)]
pub(crate) struct EntitiesManager {
    toplevel_entities: HashMap<Usr, Entity>,
    descriptions: HashMap<Usr, Description>,
}

pub(crate) struct EntitiesManagerComponents {
    pub(crate) toplevel_entities: HashMap<Usr, Entity>,
    pub(crate) descriptions: HashMap<Usr, Description>,
}

impl EntitiesManager {
    pub fn new() -> Self {
        EntitiesManager {
            toplevel_entities: HashMap::new(),
            descriptions: HashMap::new(),
        }
    }

    pub fn insert(&mut self, usr: Usr, description: Description) {
        info!("Found entity {:?}", description.name);
        self.descriptions.insert(usr, description);
    }

    pub fn insert_toplevel(&mut self, usr: Usr, entity: Entity) {
        self.toplevel_entities.insert(usr, entity);
    }

    pub fn decompose(self) -> EntitiesManagerComponents {
        EntitiesManagerComponents {
            toplevel_entities: self.toplevel_entities,
            descriptions: self.descriptions,
        }
    }
}

#[derive(Debug, Clone, Default)]
pub(crate) struct Description {
    pub(crate) name: String,
    pub(crate) brief: String,
    pub(crate) detailed: String,
}

impl Description {
    pub(crate) fn with_name(name: String) -> Self {
        Description {
            name,
            ..Default::default()
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct Described<T> {
    pub(crate) description: Description,
    pub(crate) entity: T,
}

#[derive(Debug, Clone)]
pub(crate) enum Entity {
    NameSpace(NameSpace),
    Variable(Variable),
    Function(Function),
    Class(Class),
}

impl Entity {
    fn kind_name(&self) -> &'static str {
        match self {
            Entity::NameSpace(_) => "namespace",
            Entity::Variable(_) => "variable",
            Entity::Function(_) => "function",
            Entity::Class(_) => "class",
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct NameSpace {
    pub(crate) members: HashMap<Usr, Entity>,
}

impl From<NameSpace> for Entity {
    fn from(ns: NameSpace) -> Self {
        Entity::NameSpace(ns)
    }
}

#[derive(Debug, Clone)]
pub(crate) struct Variable {
    pub(crate) r#type: String,
}

impl From<Variable> for Entity {
    fn from(var: Variable) -> Self {
        Entity::Variable(var)
    }
}

#[derive(Debug, Clone)]
pub(crate) struct Function {
    pub(crate) arguments: Vec<FunctionArgument>,
    pub(crate) return_type: String,
}

impl From<Function> for Entity {
    fn from(func: Function) -> Self {
        Entity::Function(func)
    }
}

#[derive(Debug, Clone)]
pub(crate) struct FunctionArgument {
    pub(crate) name: String,
    pub(crate) r#type: String,
}

#[derive(Debug, Clone)]
pub(crate) struct Class {
    pub(crate) member_types: Vec<Usr>,
    pub(crate) member_functions: HashMap<Usr, Function>,
    pub(crate) member_variables: HashMap<Usr, Variable>,
}

impl From<Class> for Entity {
    fn from(class: Class) -> Self {
        Entity::Class(class)
    }
}