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
69
70
use crate::{
    errors::{Error, Result},
    types::BoltWireFormat,
    version::Version,
};
use bytes::{Buf, BufMut, Bytes, BytesMut};

pub const FALSE: u8 = 0xC2;
pub const TRUE: u8 = 0xC3;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BoltBoolean {
    pub value: bool,
}

impl BoltBoolean {
    pub fn new(value: bool) -> BoltBoolean {
        BoltBoolean { value }
    }
}

impl BoltWireFormat for BoltBoolean {
    fn can_parse(_version: Version, input: &[u8]) -> bool {
        let input = input[0];
        input == TRUE || input == FALSE
    }

    fn parse(_version: Version, input: &mut Bytes) -> Result<Self> {
        let value = input.get_u8();
        match value {
            TRUE => Ok(BoltBoolean::new(true)),
            FALSE => Ok(BoltBoolean::new(false)),
            _ => Err(Error::InvalidTypeMarker("invalid boolean marker".into())),
        }
    }

    fn write_into(&self, _version: Version, bytes: &mut BytesMut) -> Result<()> {
        let value = if self.value { TRUE } else { FALSE };
        bytes.reserve(1);
        bytes.put_u8(value);
        Ok(())
    }
}

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

    #[test]
    fn should_serialize_boolean() {
        let bolt_boolean = BoltBoolean::new(true);
        let b: Bytes = bolt_boolean.into_bytes(Version::V4_1).unwrap();
        assert_eq!(&b[..], &[0xC3]);

        let bolt_boolean = BoltBoolean::new(false);
        let b: Bytes = bolt_boolean.into_bytes(Version::V4_1).unwrap();
        assert_eq!(&b[..], &[0xC2]);
    }

    #[test]
    fn should_deserialize_boolean() {
        let mut b = Bytes::from_static(&[TRUE]);
        let bolt_boolean: BoltBoolean = BoltBoolean::parse(Version::V4_1, &mut b).unwrap();
        assert!(bolt_boolean.value);

        let mut b = Bytes::from_static(&[FALSE]);
        let bolt_boolean: BoltBoolean = BoltBoolean::parse(Version::V4_1, &mut b).unwrap();
        assert!(!bolt_boolean.value);
    }
}