smt_lang/problem/
type_entry.rs

1use super::*;
2
3//------------------------- TypeEntry Type -------------------------
4
5#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
6pub enum TypeEntryType {
7    Structure(StructureId),
8    Class(ClassId),
9}
10
11//------------------------- TypeEntry -------------------------
12
13#[derive(Clone, Debug)]
14pub struct TypeEntry {
15    name: String,
16    typ: TypeEntryType,
17}
18
19impl TypeEntry {
20    pub fn new(name: String, typ: TypeEntryType) -> Self {
21        Self { name, typ }
22    }
23
24    pub fn name(&self) -> &str {
25        &self.name
26    }
27
28    pub fn typ(&self) -> TypeEntryType {
29        self.typ
30    }
31}
32
33impl FromId<StructureId> for TypeEntry {
34    fn from_id(problem: &Problem, id: StructureId) -> Self {
35        let name = problem.get(id).unwrap().name().into();
36        let typ = TypeEntryType::Structure(id);
37        Self { name, typ }
38    }
39}
40
41impl FromId<ClassId> for TypeEntry {
42    fn from_id(problem: &Problem, id: ClassId) -> Self {
43        let name = problem.get(id).unwrap().name().into();
44        let typ = TypeEntryType::Class(id);
45        Self { name, typ }
46    }
47}
48
49//------------------------- TypeEntries -------------------------
50
51#[derive(Clone, Debug)]
52pub struct TypeEntries(Vec<TypeEntry>);
53
54impl TypeEntries {
55    pub fn new(entries: Vec<TypeEntry>) -> Self {
56        TypeEntries(entries)
57    }
58
59    fn entries(&self) -> &Vec<TypeEntry> {
60        let TypeEntries(entries) = self;
61        entries
62    }
63
64    pub fn add(&self, entry: TypeEntry) -> TypeEntries {
65        let mut v = self.entries().clone();
66        v.push(entry);
67        TypeEntries(v)
68    }
69
70    pub fn get(&self, name: &str) -> Option<TypeEntry> {
71        for e in self.entries().iter().rev() {
72            if e.name() == name {
73                return Some(e.clone());
74            }
75        }
76        None
77    }
78}