rust-bakery / nom

Rust parser combinator framework
MIT License
9.18k stars 792 forks source link

Why does map_res closure not simply return Option #1635

Closed gdennie closed 1 year ago

gdennie commented 1 year ago

In nom (currently v7.1.3 with rust nightly)...

The map_res function requires a closure that returns core::result::Result<T,E>. However, map_res does actually use the Err discriminant's value. However, requiring a Result<T,E> result type complicates type conformance as well as the code. As such, I am curious as to why Option<T> was not chosen for the closure result type.

gdennie commented 1 year ago

map_res requires its error value to implement nom::err::FromExternalError which requires the result error value. However, current error type implementations by nom namely (), (I, ErrorKind), and VerboseError do not even hold this value.

Geal commented 1 year ago

It is meant for use with a custom error type that can hold the external error yes. If you don't use it, why not try map_opt?

gdennie commented 1 year ago

Did not know about map_opt, as such. Recall seeing the name once or twice in the docs. Will now be using it.

Thanks.