Skip to content

Commit

Permalink
Add today language-specific parser
Browse files Browse the repository at this point in the history
  • Loading branch information
syrtcevvi committed Aug 8, 2024
1 parent 0bc7667 commit a0a84e9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## 1.1.0 - 2024-08-08
### Added
- New parser `today` for `ru` and `en` modules.
- `today` parser was added to the `ru::bundle`, `en::bundle_dmy` and `en::bundle_mdy` bundle-parsers.

## 1.0.0 - 2024-08-04
### Added
- `quick::bundle` parser which combines the capabilities of the `forward_from_now` and `backward_from_now` parsers
Expand Down
6 changes: 6 additions & 0 deletions src/i18n/en.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub use self::{relative::*, weekday::*};
/// - [`dd_only`]
/// - Language-specific
/// - [`yesterday`]
/// - [`today`]
/// - [`tomorrow`]
/// - [`current_named_weekday_only`]
///
Expand All @@ -29,6 +30,7 @@ pub fn bundle_dmy(input: &str) -> IResult<&str, NaiveDate> {
dd_mm_only,
dd_only,
yesterday,
today,
tomorrow,
current_named_weekday_only,
))(input)
Expand All @@ -42,6 +44,7 @@ pub fn bundle_dmy(input: &str) -> IResult<&str, NaiveDate> {
/// - [`dd_only`]
/// - Language-specific
/// - [`yesterday`]
/// - [`today`]
/// - [`tomorrow`]
/// - [`current_named_weekday_only`]
///
Expand All @@ -52,6 +55,7 @@ pub fn bundle_mdy(input: &str) -> IResult<&str, NaiveDate> {
mm_dd_only,
dd_only,
yesterday,
today,
tomorrow,
current_named_weekday_only,
))(input)
Expand All @@ -72,6 +76,7 @@ mod tests {
#[case("03/12", Ok(("", Local::now().date_naive().with_day(3).unwrap().with_month(12).unwrap())))]
#[case("13 06\t2024", Ok(("", NaiveDate::from_ymd_opt(2024, 6, 13).unwrap())))]
#[case("Yesterday", Ok(("", Local::now().sub(Days::new(1)).date_naive())))]
#[case("Today", Ok(("", Local::now().date_naive())))]
#[case("Tomorrow", Ok(("", Local::now().add(Days::new(1)).date_naive())))]
fn test_bundle_dmy(#[case] input: &str, #[case] expected: IResult<&str, NaiveDate>) {
assert_eq!(bundle_dmy(input), expected)
Expand All @@ -82,6 +87,7 @@ mod tests {
#[case("12/03", Ok(("", Local::now().date_naive().with_day(3).unwrap().with_month(12).unwrap())))]
#[case("06 13\t2024", Ok(("", NaiveDate::from_ymd_opt(2024, 6, 13).unwrap())))]
#[case("Yesterday", Ok(("", Local::now().sub(Days::new(1)).date_naive())))]
#[case("Today", Ok(("", Local::now().date_naive())))]
#[case("Tomorrow", Ok(("", Local::now().add(Days::new(1)).date_naive())))]
fn test_bundle_mdy(#[case] input: &str, #[case] expected: IResult<&str, NaiveDate>) {
assert_eq!(bundle_mdy(input), expected)
Expand Down
22 changes: 22 additions & 0 deletions src/i18n/en/relative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ pub fn yesterday(input: &str) -> IResult<&str, NaiveDate> {
)(input)
}

/// Recognizes the `case insensitive` word `today` in `English` and returns
/// the corresponding [`NaiveDate`] for it.
///
/// # Examples
///
/// ```
/// use chrono::Local;
/// use nom_date_parsers::i18n::en::today;
///
/// assert_eq!(today("Today")?.1, Local::now().date_naive());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn today(input: &str) -> IResult<&str, NaiveDate> {
value(Local::now().date_naive(), tag_no_case("today"))(input)
}

/// Recognizes the `case insensitive` word `tomorrow` in `English` and returns
/// the corresponding [`NaiveDate`] for it.
///
Expand Down Expand Up @@ -67,6 +83,12 @@ mod tests {
assert_eq!(yesterday(input), expected);
}

#[rstest]
#[case("Today", Ok(("", Local::now().date_naive())))]
fn test_today(#[case] input: &str, #[case] expected: IResult<&str, NaiveDate>) {
assert_eq!(today(input), expected);
}

#[rstest]
#[case("Tomorrow", Ok(("", Local::now().add(Days::new(1)).date_naive())))]
fn test_tomorrow(#[case] input: &str, #[case] expected: IResult<&str, NaiveDate>) {
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/ru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use self::{relative::*, weekday::*};
/// - Language-specific
/// - [`day_before_yesterday`]
/// - [`yesterday`]
/// - [`today`]
/// - [`tomorrow`]
/// - [`day_after_tomorrow`]
/// - [`current_named_weekday_only`]
Expand All @@ -32,6 +33,7 @@ pub fn bundle(input: &str) -> IResult<&str, NaiveDate> {
dd_only,
day_before_yesterday,
yesterday,
today,
tomorrow,
day_after_tomorrow,
current_named_weekday_only,
Expand All @@ -55,6 +57,7 @@ mod tests {
#[case("13 06\t2024", Ok(("", NaiveDate::from_ymd_opt(2024, 6, 13).unwrap())))]
#[case("позавчера", Ok(("", Local::now().sub(Days::new(2)).date_naive())))]
#[case("Вчера", Ok(("", Local::now().sub(Days::new(1)).date_naive())))]
#[case("Сегодня", Ok(("", Local::now().date_naive())))]
#[case("Завтра", Ok(("", Local::now().add(Days::new(1)).date_naive())))]
#[case("послезавтра", Ok(("", Local::now().add(Days::new(2)).date_naive())))]
fn test_bundle(#[case] input: &str, #[case] expected: IResult<&str, NaiveDate>) {
Expand Down
22 changes: 22 additions & 0 deletions src/i18n/ru/relative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ pub fn yesterday(input: &str) -> IResult<&str, NaiveDate> {
)(input)
}

/// Recognizes the `case insensitive` word `today` in `Russian` and returns
/// the corresponding [`NaiveDate`] for it.
///
/// # Examples
///
/// ```
/// use chrono::Local;
/// use nom_date_parsers::i18n::ru::today;
///
/// assert_eq!(today("сегодня")?.1, Local::now().date_naive());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn today(input: &str) -> IResult<&str, NaiveDate> {
value(Local::now().date_naive(), tag_no_case("сегодня"))(input)
}

/// Recognizes the `case insensitive` word `завтра` in `Russian` and returns the
/// corresponding [`NaiveDate`].
///
Expand Down Expand Up @@ -121,6 +137,12 @@ mod tests {
assert_eq!(yesterday(input), expected);
}

#[rstest]
#[case("Сегодня", Ok(("", Local::now().date_naive())))]
fn test_today(#[case] input: &str, #[case] expected: IResult<&str, NaiveDate>) {
assert_eq!(today(input), expected);
}

#[rstest]
#[case("Завтра", Ok(("", Local::now().add(Days::new(1)).date_naive())))]
fn test_tomorrow(#[case] input: &str, #[case] expected: IResult<&str, NaiveDate>) {
Expand Down

0 comments on commit a0a84e9

Please sign in to comment.