teo_runtime/enum/
builder.rs

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
use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};
use maplit::btreemap;
use crate::app::data::AppData;
use crate::comment::Comment;
use crate::r#enum::{Enum, r#enum};
use crate::r#enum::member::Member;
use crate::Value;

#[derive(Debug, Clone)]
pub struct Builder {
    inner: Arc<Inner>,
}

#[derive(Debug)]
struct Inner {
    pub path: Vec<String>,
    pub comment: Option<Comment>,
    pub option: bool,
    pub interface: bool,
    pub members: Vec<Member>,
    pub data: Arc<Mutex<BTreeMap<String, Value>>>,
    pub app_data: AppData,
}

impl Builder {
    pub fn new(path: Vec<String>, comment: Option<Comment>, option: bool, interface: bool, members: Vec<Member>, app_data: AppData) -> Self {
        Self {
            inner: Arc::new(Inner {
                path,
                comment,
                option,
                interface,
                members,
                data: Arc::new(Mutex::new(btreemap! {})),
                app_data,
            })
        }
    }

    pub fn path(&self) -> &Vec<String> {
        &self.inner.path
    }

    pub fn comment(&self) -> Option<&Comment> {
        self.inner.comment.as_ref()
    }

    pub fn option(&self) -> bool {
        self.inner.option
    }

    pub fn interface(&self) -> bool {
        self.inner.interface
    }

    pub fn members(&self) -> &Vec<Member> {
        &self.inner.members
    }

    pub fn data(&self) -> BTreeMap<String, Value> {
        self.inner.data.lock().unwrap().clone()
    }

    pub fn set_data(&self, data: BTreeMap<String, Value>) {
        *self.inner.data.lock().unwrap() = data;
    }

    pub fn insert_data_entry(&self, key: String, value: Value) {
        self.inner.data.lock().unwrap().insert(key, value);
    }

    pub fn remove_data_entry(&self, key: &str) {
        self.inner.data.lock().unwrap().remove(key);
    }

    pub fn data_entry(&self, key: &str) -> Option<Value> {
        self.inner.data.lock().unwrap().get(key).cloned()
    }

    pub fn build(self) -> Enum {
        Enum {
            inner: Arc::new(r#enum::Inner {
                path: self.inner.path.clone(),
                comment: self.inner.comment.clone(),
                option: self.inner.option,
                interface: self.inner.interface,
                members: self.inner.members.clone(),
                data: self.inner.data.lock().unwrap().clone(),
                member_names: self.members().iter().map(|m| m.name.clone()).collect(),
            })
        }
    }

    pub fn app_data(&self) -> &AppData {
        &self.inner.app_data
    }
}