Skip to main content

vuquest_3320/command/
software_rev.rs

1use crate::result::{Error, Result};
2
3const SOFTWARE_REVISION: &str = "REVINF";
4
5/// Represents the `Software Revision` serial command.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub struct SoftwareRevision;
8
9impl SoftwareRevision {
10    /// Creates a new [SoftwareRevision].
11    pub const fn new() -> Self {
12        Self {}
13    }
14
15    /// Gets the ASCII serial command code for [SoftwareRevision].
16    pub const fn command(&self) -> &str {
17        SOFTWARE_REVISION
18    }
19}
20
21impl Default for SoftwareRevision {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl TryFrom<&str> for SoftwareRevision {
28    type Error = Error;
29
30    fn try_from(val: &str) -> Result<Self> {
31        match val {
32            v if v.contains(SOFTWARE_REVISION) => Ok(Self::new()),
33            _ => Err(Error::InvalidVariant),
34        }
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_valid() {
44        let cmd = SoftwareRevision::new();
45        let exp_ascii_cmd = SOFTWARE_REVISION;
46
47        assert_eq!(cmd.command(), exp_ascii_cmd);
48        assert_eq!(SoftwareRevision::try_from(exp_ascii_cmd), Ok(cmd));
49    }
50}