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

Help with reporting non-parsing errors. #319

Closed
rneswold opened this issue Jul 6, 2021 · 3 comments
Closed

Help with reporting non-parsing errors. #319

rneswold opened this issue Jul 6, 2021 · 3 comments

Comments

@rneswold
Copy link

rneswold commented Jul 6, 2021

I've been having good, initial success using combine but I've reached a point where I need to report an error if an integer field is out of range. The function is

fn parse_u32<Input>() -> impl Parser<Input, Output = u32>
where
    Input: Stream<Token = char>,
    Input::Error: ParseError<Input::Token, Input::Range, Input::Position>
{
    repeat::many1(char::digit()).and_then(|v: String| v.parse::<u32>())
}

This generates errors since ParseIntError can't be converted to a StreamError. I try some of the recommendations offered by the compiler but those also return errors. The project's Wiki page mentions ways of reporting errors, but it seems to be out-of-date since it mentions StreamErrorFor::<> which I couldn't find in the docs.

Am I trying too hard? Is there a straightforward way of reporting errors that I'm missing?

Can the Wiki be brought up to date?

@rneswold rneswold changed the title Error reporting help Help with reporting non-parsing errors. Jul 7, 2021
@rneswold
Copy link
Author

rneswold commented Jul 7, 2021

I found StreamErrorFor. Doing some investigating ...

@rneswold
Copy link
Author

rneswold commented Jul 7, 2021

Sorry for the noise. The Wiki example makes the code compile.

@rneswold rneswold closed this as completed Jul 7, 2021
@gcoakes
Copy link

gcoakes commented Feb 5, 2022

For anyone who comes across this and doesn't quite understand StreamErrorFor (like I didn't)... It's essentially a type alias to help name an error which should satisfy some traits. It looks like you use it like this:

fn cc<I>() -> impl Parser<I, Output = PacketType>
where
    I: Stream<Token = char>,
    I::Error: ParseError<I::Token, I::Range, I::Position>,
    StreamErrorFor<I>: From<UnexpectedParse>,
{
    digit().and_then(|cc| PacketType::try_from(cc).map_err(|_| UnexpectedParse::Unexpected))
}

As opposed to what the compiler tells you to do:

fn cc<I>() -> impl Parser<I, Output = PacketType>
where
    I: Stream<Token = char>,
    I::Error: ParseError<I::Token, I::Range, I::Position>,
-   StreamErrorFor<I>: From<UnexpectedParse>,
+   <<I as StreamOnce>::Error as ParseError<
+       char,
+       <I as StreamOnce>::Range,
+       <I as StreamOnce>::Position,
+   >>::StreamError: From<UnexpectedParse>,
{
    digit().and_then(|cc| PacketType::try_from(cc).map_err(|_| UnexpectedParse::Unexpected))
}

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

2 participants