messagepack_core/decode/
bool.rs

1use super::{DecodeBorrowed, Error};
2use crate::{formats::Format, io::IoRead};
3
4impl<'de> DecodeBorrowed<'de> for bool {
5    type Value = Self;
6
7    fn decode_borrowed_with_format<R>(
8        format: Format,
9        _reader: &mut R,
10    ) -> core::result::Result<Self::Value, Error<R::Error>>
11    where
12        R: IoRead<'de>,
13    {
14        let val = match format {
15            Format::True => Ok(true),
16            Format::False => Ok(false),
17            _ => Err(Error::UnexpectedFormat),
18        }?;
19
20        Ok(val)
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use crate::decode::Decode;
27
28    #[test]
29    fn decode_true() {
30        let buf: &[u8] = &[0xc3];
31        let mut r = crate::io::SliceReader::new(buf);
32        let decoded = bool::decode(&mut r).unwrap();
33        let expect = true;
34        assert_eq!(decoded, expect);
35        assert_eq!(r.rest().len(), 0)
36    }
37
38    #[test]
39    fn decode_false() {
40        let buf: &[u8] = &[0xc2];
41        let mut r = crate::io::SliceReader::new(buf);
42        let decoded = bool::decode(&mut r).unwrap();
43        let expect: bool = false;
44        assert_eq!(decoded, expect);
45        assert_eq!(r.rest().len(), 0)
46    }
47}