1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::fmt;

#[derive(Clone, Copy)]
#[repr(C)]
pub struct GainSTMControlFlags(u8);

bitflags::bitflags! {
    impl GainSTMControlFlags : u8 {
        const NONE       = 0;
        const BEGIN      = 1 << 2;
        const END        = 1 << 3;
        const UPDATE     = 1 << 4;
        const SEND_BIT0  = 1 << 6;
        const SEND_BIT1  = 1 << 7;
    }
}

impl fmt::Display for GainSTMControlFlags {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut flags = Vec::new();
        if self.contains(GainSTMControlFlags::BEGIN) {
            flags.push("BEGIN")
        }
        if self.contains(GainSTMControlFlags::END) {
            flags.push("END")
        }
        if self.contains(GainSTMControlFlags::UPDATE) {
            flags.push("UPDATE")
        }
        if self.is_empty() {
            flags.push("NONE")
        }
        write!(
            f,
            "{}",
            flags
                .iter()
                .map(|s| s.to_string())
                .collect::<Vec<_>>()
                .join(" | ")
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_size() {
        assert_eq!(std::mem::size_of::<GainSTMControlFlags>(), 1);
    }

    #[test]
    fn test_fmt() {
        assert_eq!(format!("{}", GainSTMControlFlags::NONE), "NONE");
        assert_eq!(format!("{}", GainSTMControlFlags::BEGIN), "BEGIN");
        assert_eq!(format!("{}", GainSTMControlFlags::END), "END");
        assert_eq!(format!("{}", GainSTMControlFlags::UPDATE), "UPDATE");
        assert_eq!(
            format!(
                "{}",
                GainSTMControlFlags::BEGIN | GainSTMControlFlags::END | GainSTMControlFlags::UPDATE
            ),
            "BEGIN | END | UPDATE"
        );
    }
}