nom icon indicating copy to clipboard operation
nom copied to clipboard

Convertation: IResult -> Result

Open r4v3n6101 opened this issue 4 years ago • 6 comments

First, thanks for reading. Long time I think about function allows user to convert IResult into Result (assuming we no more need rest of unread data). I guess it should look smth like this :

fn parser<O, E> (input: &[u8]) -> Result<O, E>{
   let parser = ...; // IResult<&[u8], O, E>
   return parser.done(); // or just into() may be? or rewrite existing finish fn?
}

r4v3n6101 avatar Aug 13 '21 18:08 r4v3n6101

I believe what you want is already implemented with finish()

Since, we talk about error management in nom, do you think you could tell why you was unable to find this method ? I believe this missing the keyword result somewhere. Maybe a into_result() name would make it easier to find ?

Stargateur avatar Aug 13 '21 18:08 Stargateur

Ah I didn't read the comment, don't put important information in comment my brain doesn't bother read them.

I often use this pattern:

pub fn bearer(input: &str) -> Result<&str, Err<Error<&str>>> {
    let (_, bearer) = combinator::all_consuming(sequence::preceded(
        bytes::tag("Bearer"),
        sequence::preceded(character::space1, b64token),
    ))(input)?;

    Ok(bearer)
}

I have a utils for that but for V5:

pub fn all_consuming<I, O, F, E>(f: F) -> impl Fn(I) -> Result<O, Err<E>>
where
    F: Fn(I) -> IResult<I, O, E> + Copy,
    E: error::ParseError<I>,
    I: nom::InputLength,
{
    move |input| combinator::all_consuming(f)(input).map(|(_, output)| output)
}

Stargateur avatar Aug 13 '21 18:08 Stargateur

I can do my own utility, but IMO Finish trait should be rewritten or just complemented with new function I described early.

r4v3n6101 avatar Aug 14 '21 15:08 r4v3n6101

I can do my own utility

When did I imply you can't ? why so aggressive to someone who offer a solution ?

Finish trait should be rewritten or just complemented with new function I described early.

I do not agree cause what you want would not check that all have indeed be consumed, nom is build on the principle of consuming input, and return the rest. If there is some input that remain the parser should not discard it as you proposed.

Make all_consuming not return the input is better for me but I believe this would make all_consuming not chainable because it would not meet the signature of a nom parser. But I don't see why someone would want to chain all_consuming with another parser. But I prefer what nom choice it's a general solution and everyone can choice to just discard the input.

Stargateur avatar Aug 14 '21 15:08 Stargateur

You misunderstood me, sorry if my words were offensive to you. So, sometimes there's a trash data (some formats of files store previous or unused data or pad data or something like this) and we don't need to read rest of data, so we ignore this. My idea is to stop parser with data we already parsed and rest isn't meaningful.

r4v3n6101 avatar Aug 14 '21 17:08 r4v3n6101

Don't worry, one can use rest to express "discard" in nom and so code would be (using my function) all_consuming(sequence::preceded(important_parser, rest)) I believe it's more "nomsty". In the end it's a taste I guess, I have no string opinion to not add utils to the finish trait.

Stargateur avatar Aug 14 '21 18:08 Stargateur