sparkfun_openlog/
status.rs

1use core::fmt;
2
3#[derive(Debug)]
4pub struct Status(u8);
5
6impl fmt::Display for Status {
7    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8        write!(f, "Status: (")?;
9        write!(f, "Init OK = {}, ", self.init_ok() as u8)?;
10        write!(
11            f,
12            "Last Cmd Success = {}, ",
13            self.last_command_succeeded() as u8
14        )?;
15        write!(f, "Last Cmd Known = {}, ", self.last_command_known() as u8)?;
16        write!(f, "File Open = {}, ", self.file_open() as u8)?;
17        write!(f, "In Root = {})", self.in_root_directory() as u8)
18    }
19}
20
21impl Status {
22    pub fn init_ok(&self) -> bool {
23        self.0 & (1 << 0) != 0
24    }
25
26    pub fn last_command_succeeded(&self) -> bool {
27        self.0 & (1 << 1) != 0
28    }
29
30    pub fn last_command_known(&self) -> bool {
31        self.0 & (1 << 2) != 0
32    }
33
34    pub fn file_open(&self) -> bool {
35        self.0 & (1 << 3) != 0
36    }
37
38    pub fn in_root_directory(&self) -> bool {
39        self.0 & (1 << 4) != 0
40    }
41}
42
43impl From<u8> for Status {
44    fn from(value: u8) -> Self {
45        Self(value)
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_status_reading() {
55        let status = Status::from(0b00010101);
56        assert!(status.init_ok());
57        assert!(!status.last_command_succeeded());
58        assert!(status.last_command_known());
59        assert!(!status.file_open());
60        assert!(status.in_root_directory());
61    }
62}