messagepack_core/decode/
bool.rs

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