Convertation: IResult -> Result
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?
}
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 ?
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)
}
I can do my own utility, but IMO Finish trait should be rewritten or just complemented with new function I described early.
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.
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.
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.