I want to parse some string constants with nom5 the constants
would be like
today,
yesterday,
thisweek,
thismonth,
thisyear
I can obviously match these with something like
alt((tag("today"), tag("yesterday"), tag("thisweek"), tag("thismonth"), tag("thisyear"))
but I want to map these to a a date string.
Ie
today -> "13-08-2020"
thismonth -> "01-08-2020"
so something like
fn date_const_s<'a>(i: &'a str) -> IResult<&'a str, &'a str, (&'a str, ErrorKind)> {
map(alt((tag("today"), tag("yesterday"), tag("thisweek"), tag("thismonth"), tag("thisyear"))),
|s: &str|
{
match s.as_ref() {
"today" => Utc::today().to_string().as_ref(),
}
} )(i)
}
I have two issues I don't know how to solve though.
The first is how I can return a string ref without creating a temporary with to_string()
and how can I return an Err if the match does not succeed as I would have different types in different arms on the match ?
Thanks