Skip to main content

vuquest_3320/command/
mobile_phone.rs

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