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

ErrorConvert is only implemented for InputError #600

Open
praveenperera opened this issue Oct 3, 2024 Discussed in #498 · 0 comments
Open

ErrorConvert is only implemented for InputError #600

praveenperera opened this issue Oct 3, 2024 Discussed in #498 · 0 comments
Labels
A-error Area: error reporting C-enhancement Category: Raise on the bar on expectations

Comments

@praveenperera
Copy link

praveenperera commented Oct 3, 2024

Discussed in #498

Originally posted by TheNeikos March 25, 2024
Heya!

I have a function that returns a PResult, but I can't easily use the bits::bits parser because the error traits it requires are not implemented/compatible with PResult's definition:

bits requires that:

  • BitError: ParserError<(Input, usize)> + ErrorConvert<ByteError>
  • ByteError: ParserError<Input>

But ErrorConvert is only implemented for InputError. Since PResult is the 'main' way of using winnow, this means I have to awkwardly specify the Error type using a turbofish. bits has 5 generics (even if I can infer 4 of them).

So for example an invocation looks like this:

winnow::binary::bits::bits::<_, _, InputError<(_, usize)>, _, _>((
                winnow::binary::bits::pattern(0x0, 1usize),
                winnow::binary::bits::bool,
                winnow::binary::bits::bool,
                winnow::binary::bits::take(2usize)
                    .try_map(<QualityOfService as TryFrom<u8>>::try_from),
                winnow::binary::bits::bool,
                winnow::binary::bits::bool,
                winnow::binary::bits::bool,
            ))
            .parse_next(input)
            .map_err(|_: ErrMode<InputError<_>>| {
                ErrMode::from_error_kind(input, winnow::error::ErrorKind::Slice)
            })?

Now, am I doing something wrong? Or is this just a current limitation?

Thanks for any pointers!


Parse Peek

fn parse_header_byte(input: Stream<'_>) -> IResult<Stream<'_>, (bool, bool, bool, bool, bool, u8)> {
    bits::<_, _, InputError<(_, usize)>, _, _>((
        take_bool,
        take_bool,
        take_bool,
        take_bool,
        take_bool,
        take_bits(3_u8),
    ))
    .parse_peek(input)
}

Parse next

fn parse_header_byte(input: &mut Stream<'_>) -> PResult<(bool, bool, bool, bool, bool, u8)> {
    bits::<_, _, InputError<(_, usize)>, _, _>((
        take_bool,
        take_bool,
        take_bool,
        take_bool,
        take_bool,
        take_bits(3_u8),
    ))
    .parse_next(input)
    .map_err(|_: ErrMode<InputError<_>>| {
        ErrMode::from_error_kind(input, winnow::error::ErrorKind::Slice)
    })
}
@epage epage changed the title Improve ergonomics of using winnow::binary::bits::bits with parse_next ErrorConvert is only implemented for InputError Oct 3, 2024
@epage epage added A-error Area: error reporting C-enhancement Category: Raise on the bar on expectations labels Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-error Area: error reporting C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

2 participants