If I want to write a nom parser that could fail internally, how do I propagate the error?
As an example, something to parse a NaiveDate
might look like:
fn parse_date(i: &str) -> IResult<&str, NaiveDate> {
map(take(10), |s| NaiveDate::parse_from_str(s, "%m/%d/%Y")?)(i)
}
The parse_from_str
may fail and returns its own ParseResult
type.
I actually rely on it's success/failure to determine if this parser works.
How can I convert an inner Result (in this case chrono::format::ParseResult
) to something that works with nom?
You can use Nom's
map_res
method. MapRes emits only theErrorKind::MapRes
(no custom error), but if only ok/err result is needed that should suffice.