Skip to main content

vuquest_3320/command/
manual_trigger.rs

1use crate::result::{Error, Result};
2
3const NORMAL: &str = "PAPHHF";
4const ENHANCED: &str = "PAPHHS";
5
6/// Represents the `Mobile Phone Read Mode` serial command.
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum ManualTriggerMode {
9    Normal,
10    Enhanced,
11}
12
13impl ManualTriggerMode {
14    /// Creates a new [ManualTriggerMode].
15    pub const fn new() -> Self {
16        Self::Normal
17    }
18
19    /// Gets the ASCII serial command code for [ManualTriggerMode].
20    pub const fn command(&self) -> &str {
21        match self {
22            Self::Normal => NORMAL,
23            Self::Enhanced => ENHANCED,
24        }
25    }
26}
27
28impl Default for ManualTriggerMode {
29    fn default() -> Self {
30        Self::new()
31    }
32}
33
34impl TryFrom<&str> for ManualTriggerMode {
35    type Error = Error;
36
37    fn try_from(val: &str) -> Result<Self> {
38        match val {
39            v if v.contains(NORMAL) => Ok(Self::Normal),
40            v if v.contains(ENHANCED) => Ok(Self::Enhanced),
41            _ => Err(Error::InvalidVariant),
42        }
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_valid() {
52        [ManualTriggerMode::Normal, ManualTriggerMode::Enhanced]
53            .into_iter()
54            .zip([NORMAL, ENHANCED])
55            .for_each(|(cmd, exp_ascii_cmd)| {
56                assert_eq!(cmd.command(), exp_ascii_cmd);
57                assert_eq!(ManualTriggerMode::try_from(exp_ascii_cmd), Ok(cmd));
58            });
59    }
60}