extcap/
control.rs

1use crate::print_opt_value;
2
3/// Button roles
4pub enum ButtonRole {
5    /// INTERFACE_ROLE_CONTROL
6    Control,
7    /// INTERFACE_ROLE_LOGGER
8    Logger,
9    /// INTERFACE_ROLE_HELP
10    Help,
11    /// INTERFACE_ROLE_RESTORE
12    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
26/// Interface toolbar Control types
27pub enum ControlType {
28    /// None or unknown
29    None,
30    /// INTERFACE_TYPE_BOOLEAN
31    Boolean,
32    /// INTERFACE_TYPE_BUTTON
33    Button(ButtonRole),
34    /// INTERFACE_TYPE_SELECTOR
35    Selector,
36    /// INTERFACE_TYPE_STRING
37    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/// Control representation
59#[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    /// Creates a new instance of `Control` with 'ControlType::Boolean' type
89    pub fn new_boolean() -> Self {
90        Control::new(ControlType::Boolean)
91    }
92
93    /// Creates a new instance of `Control` with 'ControlType::Button' type
94    pub fn new_button(role: ButtonRole) -> Self {
95        Control::new(ControlType::Button(role))
96    }
97
98    /// Creates a new instance of `Control` with 'ControlType::Selector' type
99    pub fn new_selector() -> Self {
100        Control::new(ControlType::Selector)
101    }
102
103    /// Creates a new instance of `Control` with 'ControlType::String' type
104    pub fn new_string() -> Self {
105        Control::new(ControlType::String)
106    }
107
108    /// Sets the display string
109    pub fn display(mut self, display: &str) -> Self {
110        self.display = Some(display.to_owned());
111        self
112    }
113
114    /// Sets the default value
115    pub fn default<T: ToString>(mut self, default: &T) -> Self {
116        self.default = Some(default.to_string());
117        self
118    }
119
120    /// Sets the range string
121    pub fn range<T: ToString>(mut self, range: &T) -> Self {
122        self.range = Some(range.to_string());
123        self
124    }
125
126    /// Sets the validation regular expression string
127    pub fn validation<T: ToString>(mut self, validation: &T) -> Self {
128        self.validation = Some(validation.to_string());
129        self
130    }
131
132    /// Sets the tooltip string
133    pub fn tooltip(mut self, tooltip: &str) -> Self {
134        self.tooltip = Some(tooltip.to_owned());
135        self
136    }
137
138    /// Sets the placeholder string
139    pub fn placeholder(mut self, placeholder: &str) -> Self {
140        self.placeholder = Some(placeholder.to_owned());
141        self
142    }
143
144    /// Adds a value
145    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/// Control value representation
171#[derive(Default)]
172pub struct ControlVal {
173    control: usize,
174    value: String,
175    display: Option<String>,
176    default: Option<bool>,
177}
178
179impl ControlVal {
180    /// Creates a new instance of `ControlVal` using a string value
181    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    /// Sets the display string
194    pub fn display(mut self, display: &str) -> Self {
195        self.display = Some(display.to_owned());
196        self
197    }
198
199    /// Sets the default flag
200    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}