falco_event/types/primitive/
bool.rs

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
use crate::event_derive::{FromBytes, FromBytesResult, ToBytes};
use crate::types::format::Format;
use crate::types::primitive::bool;
use std::fmt::Formatter;
use std::io::Write;

impl FromBytes<'_> for bool {
    fn from_bytes(buf: &mut &[u8]) -> FromBytesResult<Self> {
        let val = u32::from_bytes(buf)?;
        Ok(val != 0)
    }
}

impl ToBytes for bool {
    fn binary_size(&self) -> usize {
        std::mem::size_of::<u32>()
    }

    fn write<W: Write>(&self, writer: W) -> std::io::Result<()> {
        let val: u32 = if *self { 1 } else { 0 };
        val.write(writer)
    }

    fn default_repr() -> impl ToBytes {
        0u32
    }
}

impl<F> Format<F> for bool {
    fn format(&self, fmt: &mut Formatter) -> std::fmt::Result {
        if *self {
            fmt.write_str("true")
        } else {
            fmt.write_str("false")
        }
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_serde_bool_true() {
        let json = serde_json::to_string(&true).unwrap();
        assert_eq!(json, r#"true"#);

        let true2: bool = serde_json::from_str(&json).unwrap();
        assert!(true2);
    }

    #[test]
    fn test_serde_bool_false() {
        let json = serde_json::to_string(&false).unwrap();
        assert_eq!(json, r#"false"#);

        let false2: bool = serde_json::from_str(&json).unwrap();
        assert!(!false2);
    }
}