From 30ffed484cac4970c6264e409bd8d88cbcccf205 Mon Sep 17 00:00:00 2001 From: William Calliari Date: Tue, 28 Jun 2022 21:45:51 +0200 Subject: [PATCH 1/2] Implement function `bool` for `nom::bits::complete` --- src/bits/complete.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/bits/complete.rs b/src/bits/complete.rs index b46bc7a27..2fd41e1ca 100644 --- a/src/bits/complete.rs +++ b/src/bits/complete.rs @@ -105,6 +105,29 @@ where } } +/// Parses one specific bit as a bool. +/// +/// # Example +/// ```rust +/// # use nom::bits::complete::bool; +/// # use nom::IResult; +/// # use nom::error::{Error, ErrorKind}; +/// +/// fn parse(input: (&[u8], usize)) -> IResult<(&[u8], usize), bool> { +/// bool(input) +/// } +/// +/// assert_eq!(parse(([0b10000000].as_ref(), 0)), Ok((([0b10000000].as_ref(), 1), true))); +/// assert_eq!(parse(([0b10000000].as_ref(), 1)), Ok((([0b10000000].as_ref(), 2), false))); +/// ``` +pub fn bool>(input: (I, usize)) -> IResult<(I, usize), bool, E> + where + I: Slice> + InputIter + InputLength, +{ + let (res, bit): (_, u32) = take(1usize)(input)?; + Ok((res, bit != 0)) +} + #[cfg(test)] mod test { use super::*; @@ -147,4 +170,28 @@ mod test { Ok((([0b11111111].as_ref(), 4), 0b1000110100111111111111)) ); } + + #[test] + fn test_bool_0() { + let input = [0b10000000].as_ref(); + + let result: crate::IResult<(&[u8], usize), bool> = bool((input, 0)); + + assert_eq!(result, Ok(((input, 1), true))); + } + + #[test] + fn test_bool_eof() { + let input = [0b10000000].as_ref(); + + let result: crate::IResult<(&[u8], usize), bool> = bool((input, 8)); + + assert_eq!( + result, + Err(crate::Err::Error(crate::error::Error { + input: (input, 8), + code: ErrorKind::Eof + })) + ); + } } From 8809b12423e749d356b812aeafbb0ea401b05384 Mon Sep 17 00:00:00 2001 From: William Calliari Date: Tue, 28 Jun 2022 21:48:33 +0200 Subject: [PATCH 2/2] Implement function `bool` for `nom::bits::streaming` --- src/bits/streaming.rs | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/bits/streaming.rs b/src/bits/streaming.rs index e8adc1c0d..892fd328d 100644 --- a/src/bits/streaming.rs +++ b/src/bits/streaming.rs @@ -79,6 +79,29 @@ where } } +/// Parses one specific bit as a bool. +/// +/// # Example +/// ```rust +/// # use nom::bits::complete::bool; +/// # use nom::IResult; +/// # use nom::error::{Error, ErrorKind}; +/// +/// fn parse(input: (&[u8], usize)) -> IResult<(&[u8], usize), bool> { +/// bool(input) +/// } +/// +/// assert_eq!(parse(([0b10000000].as_ref(), 0)), Ok((([0b10000000].as_ref(), 1), true))); +/// assert_eq!(parse(([0b10000000].as_ref(), 1)), Ok((([0b10000000].as_ref(), 2), false))); +/// ``` +pub fn bool>(input: (I, usize)) -> IResult<(I, usize), bool, E> + where + I: Slice> + InputIter + InputLength, +{ + let (res, bit): (_, u32) = take(1usize)(input)?; + Ok((res, bit != 0)) +} + #[cfg(test)] mod test { use super::*; @@ -126,4 +149,25 @@ mod test { })) ); } + + #[test] + fn test_bool_0() { + let input = [0b10000000].as_ref(); + + let result: crate::IResult<(&[u8], usize), bool> = bool((input, 0)); + + assert_eq!(result, Ok(((input, 1), true))); + } + + #[test] + fn test_bool_eof() { + let input = [0b10000000].as_ref(); + + let result: crate::IResult<(&[u8], usize), bool> = bool((input, 8)); + + assert_eq!( + result, + Err(crate::Err::Incomplete(Needed::new(1))) + ); + } }