Skip to content

Commit

Permalink
Add cli example
Browse files Browse the repository at this point in the history
  • Loading branch information
syrtcevvi committed Aug 4, 2024
1 parent b707240 commit 38dd797
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 55 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
## unreleased
### Added
- `quick::bundle` parser which combines the capabilities of the `forward_from_now` and `backward_from_now` parsers
- `cli` example provides the interactive way to try some parsers out
- New parsers:
- `short_named_weekday_dot`

Expand Down
101 changes: 48 additions & 53 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ en = []

[dependencies]
chrono = "0.4.38"
criterion = { version = "0.5.1", features = ["html_reports"] }
nom = "7.1.3"

[dev-dependencies]
criterion = "0.5.1"
anyhow = "1.0.86"
criterion = { version = "0.5.1", features = ["html_reports"] }
pretty_assertions = "1.4.0"
rstest = "0.21.0"

Expand Down
68 changes: 68 additions & 0 deletions examples/cli.rs
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(())
}

0 comments on commit 38dd797

Please sign in to comment.