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
71
72
73
74
75
use crate::errors::*;
use bytes::*;
use std::cell::RefCell;
use std::convert::{TryFrom, TryInto};
use std::rc::Rc;

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

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

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

    pub fn can_parse(input: Rc<RefCell<Bytes>>) -> bool {
        let input = input.borrow()[0];
        input == TRUE || input == FALSE
    }
}

impl TryInto<Bytes> for BoltBoolean {
    type Error = Error;
    fn try_into(self) -> Result<Bytes> {
        if self.value {
            Ok(Bytes::copy_from_slice(&[TRUE]))
        } else {
            Ok(Bytes::copy_from_slice(&[FALSE]))
        }
    }
}

impl TryFrom<Rc<RefCell<Bytes>>> for BoltBoolean {
    type Error = Error;

    fn try_from(input: Rc<RefCell<Bytes>>) -> Result<BoltBoolean> {
        let value = input.borrow_mut().get_u8();
        match value {
            TRUE => Ok(BoltBoolean::new(true)),
            FALSE => Ok(BoltBoolean::new(false)),
            _ => return Err(Error::InvalidTypeMarker("invalid boolean marker".into())),
        }
    }
}

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

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

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

    #[test]
    fn should_deserialize_boolean() {
        let b = Rc::new(RefCell::new(Bytes::copy_from_slice(&[TRUE])));
        let bolt_boolean: BoltBoolean = b.try_into().unwrap();
        assert_eq!(bolt_boolean.value, true);

        let b = Rc::new(RefCell::new(Bytes::copy_from_slice(&[FALSE])));
        let bolt_boolean: BoltBoolean = b.try_into().unwrap();
        assert_eq!(bolt_boolean.value, false);
    }
}