forked from tkeksa/extcap-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.rs
215 lines (188 loc) · 5.38 KB
/
control.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use crate::print_opt_value;
/// Button roles
pub enum ButtonRole {
/// INTERFACE_ROLE_CONTROL
Control,
/// INTERFACE_ROLE_LOGGER
Logger,
/// INTERFACE_ROLE_HELP
Help,
/// INTERFACE_ROLE_RESTORE
Restore,
}
impl ButtonRole {
fn role_str(&self) -> &'static str {
match self {
ButtonRole::Control => "control",
ButtonRole::Logger => "logger",
ButtonRole::Help => "help",
ButtonRole::Restore => "restore",
}
}
}
/// Interface toolbar Control types
pub enum ControlType {
/// None or unknown
None,
/// INTERFACE_TYPE_BOOLEAN
Boolean,
/// INTERFACE_TYPE_BUTTON
Button(ButtonRole),
/// INTERFACE_TYPE_SELECTOR
Selector,
/// INTERFACE_TYPE_STRING
String,
}
impl Default for ControlType {
fn default() -> Self {
ControlType::None
}
}
impl ControlType {
fn type_str(&self) -> &'static str {
match self {
ControlType::None => "none",
ControlType::Boolean => "boolean",
ControlType::Button(_) => "button",
ControlType::Selector => "selector",
ControlType::String => "string",
}
}
}
/// Control representation
#[derive(Default)]
pub struct Control {
number: usize,
ctype: ControlType,
display: Option<String>,
default: Option<String>,
range: Option<String>,
validation: Option<String>,
tooltip: Option<String>,
placeholder: Option<String>,
vals: Vec<ControlVal>,
}
impl Control {
fn new(ctype: ControlType) -> Self {
Self {
number: usize::MAX,
ctype,
..Default::default()
}
}
pub(crate) fn set_number(&mut self, number: usize) {
self.number = number;
for val in self.vals.iter_mut() {
val.set_control(number);
}
}
/// Creates a new instance of `Control` with 'ControlType::Boolean' type
pub fn new_boolean() -> Self {
Control::new(ControlType::Boolean)
}
/// Creates a new instance of `Control` with 'ControlType::Button' type
pub fn new_button(role: ButtonRole) -> Self {
Control::new(ControlType::Button(role))
}
/// Creates a new instance of `Control` with 'ControlType::Selector' type
pub fn new_selector() -> Self {
Control::new(ControlType::Selector)
}
/// Creates a new instance of `Control` with 'ControlType::String' type
pub fn new_string() -> Self {
Control::new(ControlType::String)
}
/// Sets the display string
pub fn display(mut self, display: &str) -> Self {
self.display = Some(display.to_owned());
self
}
/// Sets the default value
pub fn default<T: ToString>(mut self, default: &T) -> Self {
self.default = Some(default.to_string());
self
}
/// Sets the range string
pub fn range<T: ToString>(mut self, range: &T) -> Self {
self.range = Some(range.to_string());
self
}
/// Sets the validation regular expression string
pub fn validation<T: ToString>(mut self, validation: &T) -> Self {
self.validation = Some(validation.to_string());
self
}
/// Sets the tooltip string
pub fn tooltip(mut self, tooltip: &str) -> Self {
self.tooltip = Some(tooltip.to_owned());
self
}
/// Sets the placeholder string
pub fn placeholder(mut self, placeholder: &str) -> Self {
self.placeholder = Some(placeholder.to_owned());
self
}
/// Adds a value
pub fn add_val(&mut self, val: ControlVal) {
self.vals.push(val);
}
pub(crate) fn print_control(&self) {
print!(
"control {{number={}}}{{type={}}}",
self.number,
self.ctype.type_str()
);
if let ControlType::Button(role) = &self.ctype {
print!("{{role={}}}", role.role_str());
};
print_opt_value("display", &self.display);
print_opt_value("default", &self.default);
print_opt_value("range", &self.range);
print_opt_value("validation", &self.validation);
print_opt_value("tooltip", &self.tooltip);
print_opt_value("placeholder", &self.placeholder);
println!();
self.vals.iter().for_each(ControlVal::print_value);
}
}
/// Control value representation
#[derive(Default)]
pub struct ControlVal {
control: usize,
value: String,
display: Option<String>,
default: Option<bool>,
}
impl ControlVal {
/// Creates a new instance of `ControlVal` using a string value
pub fn new<T: ToString>(value: T) -> Self {
ControlVal {
control: usize::MAX,
value: value.to_string(),
..Default::default()
}
}
fn set_control(&mut self, control: usize) {
self.control = control;
}
/// Sets the display string
pub fn display(mut self, display: &str) -> Self {
self.display = Some(display.to_owned());
self
}
/// Sets the default flag
pub fn default(mut self, default: bool) -> Self {
self.default = Some(default);
self
}
fn print_value(&self) {
print!(
"value {{control={}}}{{value={}}}{{display={}}}",
self.control,
self.value,
self.display.as_ref().unwrap_or(&self.value)
);
print_opt_value("default", &self.default);
println!();
}
}