1use crate::print_opt_value;
2
3pub enum ButtonRole {
5 Control,
7 Logger,
9 Help,
11 Restore,
13}
14
15impl ButtonRole {
16 fn role_str(&self) -> &'static str {
17 match self {
18 ButtonRole::Control => "control",
19 ButtonRole::Logger => "logger",
20 ButtonRole::Help => "help",
21 ButtonRole::Restore => "restore",
22 }
23 }
24}
25
26pub enum ControlType {
28 None,
30 Boolean,
32 Button(ButtonRole),
34 Selector,
36 String,
38}
39
40impl Default for ControlType {
41 fn default() -> Self {
42 ControlType::None
43 }
44}
45
46impl ControlType {
47 fn type_str(&self) -> &'static str {
48 match self {
49 ControlType::None => "none",
50 ControlType::Boolean => "boolean",
51 ControlType::Button(_) => "button",
52 ControlType::Selector => "selector",
53 ControlType::String => "string",
54 }
55 }
56}
57
58#[derive(Default)]
60pub struct Control {
61 number: usize,
62 ctype: ControlType,
63 display: Option<String>,
64 default: Option<String>,
65 range: Option<String>,
66 validation: Option<String>,
67 tooltip: Option<String>,
68 placeholder: Option<String>,
69 vals: Vec<ControlVal>,
70}
71
72impl Control {
73 fn new(ctype: ControlType) -> Self {
74 Self {
75 number: usize::MAX,
76 ctype,
77 ..Default::default()
78 }
79 }
80
81 pub(crate) fn set_number(&mut self, number: usize) {
82 self.number = number;
83 for val in self.vals.iter_mut() {
84 val.set_control(number);
85 }
86 }
87
88 pub fn new_boolean() -> Self {
90 Control::new(ControlType::Boolean)
91 }
92
93 pub fn new_button(role: ButtonRole) -> Self {
95 Control::new(ControlType::Button(role))
96 }
97
98 pub fn new_selector() -> Self {
100 Control::new(ControlType::Selector)
101 }
102
103 pub fn new_string() -> Self {
105 Control::new(ControlType::String)
106 }
107
108 pub fn display(mut self, display: &str) -> Self {
110 self.display = Some(display.to_owned());
111 self
112 }
113
114 pub fn default<T: ToString>(mut self, default: &T) -> Self {
116 self.default = Some(default.to_string());
117 self
118 }
119
120 pub fn range<T: ToString>(mut self, range: &T) -> Self {
122 self.range = Some(range.to_string());
123 self
124 }
125
126 pub fn validation<T: ToString>(mut self, validation: &T) -> Self {
128 self.validation = Some(validation.to_string());
129 self
130 }
131
132 pub fn tooltip(mut self, tooltip: &str) -> Self {
134 self.tooltip = Some(tooltip.to_owned());
135 self
136 }
137
138 pub fn placeholder(mut self, placeholder: &str) -> Self {
140 self.placeholder = Some(placeholder.to_owned());
141 self
142 }
143
144 pub fn add_val(&mut self, val: ControlVal) {
146 self.vals.push(val);
147 }
148
149 pub(crate) fn print_control(&self) {
150 print!(
151 "control {{number={}}}{{type={}}}",
152 self.number,
153 self.ctype.type_str()
154 );
155 if let ControlType::Button(role) = &self.ctype {
156 print!("{{role={}}}", role.role_str());
157 };
158 print_opt_value("display", &self.display);
159 print_opt_value("default", &self.default);
160 print_opt_value("range", &self.range);
161 print_opt_value("validation", &self.validation);
162 print_opt_value("tooltip", &self.tooltip);
163 print_opt_value("placeholder", &self.placeholder);
164 println!();
165
166 self.vals.iter().for_each(ControlVal::print_value);
167 }
168}
169
170#[derive(Default)]
172pub struct ControlVal {
173 control: usize,
174 value: String,
175 display: Option<String>,
176 default: Option<bool>,
177}
178
179impl ControlVal {
180 pub fn new<T: ToString>(value: T) -> Self {
182 ControlVal {
183 control: usize::MAX,
184 value: value.to_string(),
185 ..Default::default()
186 }
187 }
188
189 fn set_control(&mut self, control: usize) {
190 self.control = control;
191 }
192
193 pub fn display(mut self, display: &str) -> Self {
195 self.display = Some(display.to_owned());
196 self
197 }
198
199 pub fn default(mut self, default: bool) -> Self {
201 self.default = Some(default);
202 self
203 }
204
205 fn print_value(&self) {
206 print!(
207 "value {{control={}}}{{value={}}}{{display={}}}",
208 self.control,
209 self.value,
210 self.display.as_ref().unwrap_or(&self.value)
211 );
212 print_opt_value("default", &self.default);
213 println!();
214 }
215}