teo_runtime/enum/
builder.rs1use std::collections::BTreeMap;
2use std::sync::{Arc, Mutex};
3use maplit::btreemap;
4use crate::app::data::AppData;
5use crate::comment::Comment;
6use crate::r#enum::{Enum, r#enum};
7use crate::r#enum::member::Member;
8use crate::Value;
9
10#[derive(Debug, Clone)]
11pub struct Builder {
12 inner: Arc<Inner>,
13}
14
15#[derive(Debug)]
16struct Inner {
17 pub path: Vec<String>,
18 pub comment: Option<Comment>,
19 pub option: bool,
20 pub interface: bool,
21 pub members: Vec<Member>,
22 pub data: Arc<Mutex<BTreeMap<String, Value>>>,
23 pub app_data: AppData,
24}
25
26impl Builder {
27 pub fn new(path: Vec<String>, comment: Option<Comment>, option: bool, interface: bool, members: Vec<Member>, app_data: AppData) -> Self {
28 Self {
29 inner: Arc::new(Inner {
30 path,
31 comment,
32 option,
33 interface,
34 members,
35 data: Arc::new(Mutex::new(btreemap! {})),
36 app_data,
37 })
38 }
39 }
40
41 pub fn path(&self) -> &Vec<String> {
42 &self.inner.path
43 }
44
45 pub fn comment(&self) -> Option<&Comment> {
46 self.inner.comment.as_ref()
47 }
48
49 pub fn option(&self) -> bool {
50 self.inner.option
51 }
52
53 pub fn interface(&self) -> bool {
54 self.inner.interface
55 }
56
57 pub fn members(&self) -> &Vec<Member> {
58 &self.inner.members
59 }
60
61 pub fn data(&self) -> BTreeMap<String, Value> {
62 self.inner.data.lock().unwrap().clone()
63 }
64
65 pub fn set_data(&self, data: BTreeMap<String, Value>) {
66 *self.inner.data.lock().unwrap() = data;
67 }
68
69 pub fn insert_data_entry(&self, key: String, value: Value) {
70 self.inner.data.lock().unwrap().insert(key, value);
71 }
72
73 pub fn remove_data_entry(&self, key: &str) {
74 self.inner.data.lock().unwrap().remove(key);
75 }
76
77 pub fn data_entry(&self, key: &str) -> Option<Value> {
78 self.inner.data.lock().unwrap().get(key).cloned()
79 }
80
81 pub fn build(self) -> Enum {
82 Enum {
83 inner: Arc::new(r#enum::Inner {
84 path: self.inner.path.clone(),
85 comment: self.inner.comment.clone(),
86 option: self.inner.option,
87 interface: self.inner.interface,
88 members: self.inner.members.clone(),
89 data: self.inner.data.lock().unwrap().clone(),
90 member_names: self.members().iter().map(|m| m.name.clone()).collect(),
91 })
92 }
93 }
94
95 pub fn app_data(&self) -> &AppData {
96 &self.inner.app_data
97 }
98}