Skip to content

Commit

Permalink
1533 implement bool function for bits (#1534)
Browse files Browse the repository at this point in the history
* Implement function `bool` for `nom::bits::complete`

* Implement function `bool` for `nom::bits::streaming`

Co-authored-by: William Calliari <william.calliari@gmail.com>
  • Loading branch information
w1ll-i-code and William Calliari committed Dec 30, 2022
1 parent 6e45c5d commit 6860641
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/bits/complete.rs
Expand Up @@ -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<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
{
let (res, bit): (_, u32) = take(1usize)(input)?;
Ok((res, bit != 0))
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -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
}))
);
}
}
44 changes: 44 additions & 0 deletions src/bits/streaming.rs
Expand Up @@ -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<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
{
let (res, bit): (_, u32) = take(1usize)(input)?;
Ok((res, bit != 0))
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -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)))
);
}
}

0 comments on commit 6860641

Please sign in to comment.