smt_lang/problem/
entry.rs

1use super::*;
2
3//------------------------- Entry Type -------------------------
4
5#[derive(Clone, PartialEq, Eq, Hash, Debug)]
6pub enum EntryType {
7    Instance(InstanceId),
8    Variable(VariableId),
9    //
10    Parameter(Parameter),
11    //
12    StrucSelf(StructureId),
13    //
14    ClassSelf(ClassId),
15}
16
17//------------------------- Entry -------------------------
18
19#[derive(Clone, Debug)]
20pub struct Entry {
21    name: String,
22    typ: EntryType,
23}
24
25impl Entry {
26    pub fn new(name: String, typ: EntryType) -> Self {
27        Self { name, typ }
28    }
29
30    pub fn new_parameter(parameter: &Parameter) -> Self {
31        let name = parameter.name().to_string();
32        let typ = EntryType::Parameter(parameter.clone());
33        Self { name, typ }
34    }
35
36    pub fn name(&self) -> &str {
37        &self.name
38    }
39
40    pub fn typ(&self) -> &EntryType {
41        &self.typ
42    }
43}
44
45impl FromId<InstanceId> for Entry {
46    fn from_id(problem: &Problem, id: InstanceId) -> Self {
47        let name = problem.get(id).unwrap().name().into();
48        let typ = EntryType::Instance(id);
49        Self { name, typ }
50    }
51}
52impl FromId<VariableId> for Entry {
53    fn from_id(problem: &Problem, id: VariableId) -> Self {
54        let name = problem.get(id).unwrap().name().into();
55        let typ = EntryType::Variable(id);
56        Self { name, typ }
57    }
58}
59
60//------------------------- Entries -------------------------
61
62#[derive(Clone, Debug)]
63pub struct Entries(Vec<Entry>);
64
65impl Entries {
66    pub fn new(entries: Vec<Entry>) -> Self {
67        Entries(entries)
68    }
69
70    fn entries(&self) -> &Vec<Entry> {
71        let Entries(entries) = self;
72        entries
73    }
74
75    pub fn add(&self, entry: Entry) -> Entries {
76        let mut v = self.entries().clone();
77        v.push(entry);
78        Entries(v)
79    }
80
81    pub fn add_all(&self, entries: Entries) -> Entries {
82        let Entries(others) = entries;
83        let mut v = self.entries().clone();
84        for entry in others {
85            v.push(entry);
86        }
87        Entries(v)
88    }
89
90    pub fn get(&self, name: &str) -> Option<Entry> {
91        for e in self.entries().iter().rev() {
92            if e.name() == name {
93                return Some(e.clone());
94            }
95        }
96        None
97    }
98}