Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing flags from bitstream #1533

Closed
w1ll-i-code opened this issue Jun 28, 2022 · 4 comments
Closed

Parsing flags from bitstream #1533

w1ll-i-code opened this issue Jun 28, 2022 · 4 comments

Comments

@w1ll-i-code
Copy link
Contributor

Feature request:

Hello, I want to parse some bit flags from a bit stream. I know I could do that with bit masks, but with nom it seems clearer and the error handling comes for free. It would also improve the ergonomics for parsing binary formats in general in my opinion.

I propose to add a new function nom::bits::*::bool that takes exactly one bit from the bitstream and transforms it into a bool. If I can have an opinion if this would be useful, I will implement it myself, document it and open a PR for it, so it is no hustle for you.

System Information

  • Rust version : 1.61.0
  • nom version : 7.11
  • nom compilation features used: default

Test case

use nom::bits::{ bits, streaming::{tag, bool} };
use nom::error::Error;
use nom::sequence::tuple;
use nom::IResult;

fn parse(input: &[u8]) -> IResult<&[u8], (u8, bool)> {
    bits::<_, _, Error<(&[u8], usize)>, _, _>(tuple((tag(0, 2usize), bool)))(input)
}

#[test]
fn parse_flag_from_bitstream() {
    let input = 0b00100000u32.to_le_bytes();
    let (remaining, parsed) = parse(&input).expect("We only take three bits, so this is save.");

    assert_eq!(remaining, &[0x00, 0x00, 0x00]);
    assert_eq!(parsed, (0x00, true));
}
@epage
Copy link
Contributor

epage commented Dec 24, 2022

Since some of us are talking about a nom v8, something I've been wondering is if we should make a dedicated type for (&[u8], usize) so we can implement traits for it and have it better work with the rest of the API. For example, instead of needing to make a specialized bits::bool, you could use (the misnamed) proposed bytes::any (which is also why I'm suggesting renaming the bytes module)

@epage
Copy link
Contributor

epage commented Dec 24, 2022

I wonder what the line is between (&[u8], usize) / built-in bit support, a custom type for it, and nom_bitvec.

@Geal
Copy link
Collaborator

Geal commented Dec 30, 2022

(&[u8], usize) was useful to get started in parsing bit streams, but that could benefit from a more specialized type, yes. nom-bitvec was an attempt at that, and was move to a separate crate due to dependency ncompatibilities

@w1ll-i-code
Copy link
Contributor Author

That's cool. Where's the discussion for the nom v8 going on? I'd love to keep up and maybe contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants