Skip to main content

vuquest_3320/command/
query.rs

1const DEFAULT_VALUE: &str = "^";
2const CURRENT_VALUE: &str = "?";
3const RANGE_VALUE: &str = "*";
4
5/// Represents special characters used to modify other serial commands to query command values.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum QueryCommand {
8    /// Gets the default value for the command.
9    DefaultValue,
10    /// Gets the current value for the command.
11    CurrentValue,
12    /// Gets the range of acceptable values for the command.
13    RangeValue,
14}
15
16impl QueryCommand {
17    /// Creates a new [QueryCommand].
18    pub const fn new() -> Self {
19        Self::DefaultValue
20    }
21
22    /// Gets the ASCII-encoded serial command for the [QueryCommand].
23    pub const fn command(&self) -> &str {
24        match self {
25            Self::DefaultValue => DEFAULT_VALUE,
26            Self::CurrentValue => CURRENT_VALUE,
27            Self::RangeValue => RANGE_VALUE,
28        }
29    }
30}
31
32impl Default for QueryCommand {
33    fn default() -> Self {
34        Self::new()
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_valid() {
44        [
45            QueryCommand::DefaultValue,
46            QueryCommand::CurrentValue,
47            QueryCommand::RangeValue,
48        ]
49        .into_iter()
50        .zip([DEFAULT_VALUE, CURRENT_VALUE, RANGE_VALUE])
51        .for_each(|(cmd, exp_ascii)| {
52            assert_eq!(cmd.command(), exp_ascii);
53        });
54    }
55}