-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
This example provides the interactive way to test some existing date parsers. | ||
Sample of one run: | ||
$ cargo run --release --example cli | ||
> Today is: 2024-08-04 | ||
$ + 10 | ||
> recognized: 2024-08-14 | ||
$ 10 | ||
> recognized: 2024-08-10 | ||
$ 22-04 | ||
> recognized: 2024-04-22 | ||
$ yesterday | ||
> recognized: 2024-08-03 | ||
Try other variations, supported by the `quick::bundle` and `en::bundle_dmy` parsers out! | ||
*/ | ||
|
||
use std::io; | ||
|
||
use chrono::{Local, NaiveDate}; | ||
use nom::branch::alt; | ||
|
||
use nom_date_parsers::{i18n::en, quick, types::IResult}; | ||
|
||
fn versatile_parser(input: &str) -> IResult<&str, NaiveDate> { | ||
// Its essential to provide parsers in the correct order due to the fact that | ||
// `+10` pattern can be recognized by the `numeric::dd_only` parser instead of | ||
// `quick::forward_from_now` | ||
alt((quick::bundle, en::bundle_dmy))(input) | ||
/* | ||
Uncomment these lines, comment previous one, run example and try to type `42` as input. You will see smth like: | ||
"unable to recognize the input as a date: Parsing Error: DayOutOfRange" | ||
*/ | ||
// use nom_date_parsers::prelude::dd_only; | ||
// dd_only(input) | ||
} | ||
|
||
fn main() -> anyhow::Result<()> { | ||
println!("Today is: {}", Local::now().date_naive()); | ||
|
||
for line in io::stdin().lines() { | ||
match line { | ||
Ok(line) => { | ||
match versatile_parser(&line).map(|r| r.1) { | ||
Ok(date) => { | ||
/* | ||
N.B. due to the nature of the nom, the non-existent date `31-02-2024` will be parsed by the | ||
`dd_only` parser as `31` without throwing an error if the `31-<current month>-<current year>` date exists | ||
*/ | ||
println!("recognized: {}", date); | ||
} | ||
Err(err) => { | ||
/* | ||
Due to the fact that `bundle` parsers use `alt` combinator all related-errors are | ||
shadowed by the `Parsing Error: Nom("42", Tag)` error. So, if you want to see that `42` input is out of day-part range, | ||
it's impossible with `bundle` parsers, use `dd_only` instead | ||
*/ | ||
println!("unable to recognize the input as a date: {err}"); | ||
} | ||
} | ||
} | ||
Err(_) => break, | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |