Skip to content

Commit

Permalink
bbn: Add DateLike
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Jul 5, 2023
1 parent 086b9ef commit db9be9a
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 1 deletion.
30 changes: 29 additions & 1 deletion bbn/Cargo.lock

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

1 change: 1 addition & 0 deletions bbn/crates/bbn/Cargo.toml
Expand Up @@ -25,6 +25,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
sitemap-xml-writer = "0.1.0"
thiserror = { workspace = true }
time = { version = "0.3.22", features = ["formatting", "parsing"] }
tokio = { workspace = true }
xdg = "2.2.0"

Expand Down
135 changes: 135 additions & 0 deletions bbn/crates/bbn/src/date_like.rs
@@ -0,0 +1,135 @@
use std::str::FromStr;

use time::{format_description, Date};

#[derive(Debug, Eq, PartialEq)]
pub enum DateLike {
CalendarDate(date_range::date::Date),
WeekDate(date_range::date::Date),
}

impl FromStr for DateLike {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let calendar_date_format = format_description::parse(
"[year base:calendar repr:full]-[month padding:zero repr:numerical]-[day padding:zero]",
)?;
let week_date_format = format_description::parse("[year base:iso_week repr:full]-W[week_number padding:zero repr:iso]-[weekday repr:monday one_indexed:true]")?;
Date::parse(s, &week_date_format)
.map_err(|e| anyhow::anyhow!(e))
.and_then(|d| {
d.format(&calendar_date_format)
.map_err(|e| anyhow::anyhow!(e))
.and_then(|s| {
date_range::date::Date::from_str(s.as_str()).map_err(|e| anyhow::anyhow!(e))
})
.map(Self::WeekDate)
})
.or_else(|_| {
Date::parse(s, &calendar_date_format)
.map_err(|e| anyhow::anyhow!(e))
.and_then(|d| {
d.format(&calendar_date_format)
.map_err(|e| anyhow::anyhow!(e))
})
.and_then(|s| {
date_range::date::Date::from_str(s.as_str()).map_err(|e| anyhow::anyhow!(e))
})
.map(Self::CalendarDate)
})
}
}

#[cfg(test)]
mod tests {
use time::{
format_description::{self},
Date, Month, Weekday,
};

use super::*;

#[test]
fn test_calendar_date_format() -> anyhow::Result<()> {
let calendar_date_format = format_description::parse(
"[year base:calendar repr:full]-[month padding:zero repr:numerical]-[day padding:zero]",
)?;
let test_cases = vec![
(
Date::from_calendar_date(2023, Month::January, 1)?,
"2023-01-01",
),
(
Date::from_calendar_date(2023, Month::December, 31)?,
"2023-12-31",
),
(
Date::from_calendar_date(1, Month::January, 1)?,
"0001-01-01",
),
];
for (date, s) in test_cases {
assert_eq!(Date::parse(s, &calendar_date_format)?, date);
assert_eq!(date.format(&calendar_date_format)?, s);
}
Ok(())
}

#[test]
fn test_from_str() -> anyhow::Result<()> {
assert_eq!(
DateLike::from_str("2023-07-05")?,
DateLike::CalendarDate(date_range::date::Date::from_str("2023-07-05")?)
);
assert_eq!(
DateLike::from_str("2023-W27-3")?,
DateLike::WeekDate(date_range::date::Date::from_str("2023-07-05")?)
);
Ok(())
}

#[test]
fn test_week_date_format() -> anyhow::Result<()> {
let week_date_format = format_description::parse("[year base:iso_week repr:full]-W[week_number padding:zero repr:iso]-[weekday repr:monday one_indexed:true]")?;
let test_cases = vec![
(
Date::from_iso_week_date(2023, 27, Weekday::Wednesday)?,
"2023-W27-3",
),
(
Date::from_iso_week_date(2023, 1, Weekday::Monday)?,
"2023-W01-1",
),
(
Date::from_iso_week_date(2023, 52, Weekday::Sunday)?,
"2023-W52-7",
),
(
Date::from_iso_week_date(2026, 53, Weekday::Thursday)?,
"2026-W53-4",
),
(
Date::from_calendar_date(2026, Month::December, 31)?,
"2026-W53-4",
),
(
Date::from_iso_week_date(2026, 53, Weekday::Friday)?,
"2026-W53-5",
),
(
Date::from_calendar_date(2027, Month::January, 1)?,
"2026-W53-5",
),
(
Date::from_iso_week_date(1, 1, Weekday::Monday)?,
"0001-W01-1",
),
];
for (date, s) in test_cases {
assert_eq!(Date::parse(s, &week_date_format)?, date);
assert_eq!(date.format(&week_date_format)?, s);
}
Ok(())
}
}
1 change: 1 addition & 0 deletions bbn/crates/bbn/src/main.rs
Expand Up @@ -3,6 +3,7 @@ mod command;
mod config;
mod config_repository;
mod credentials;
mod date_like;

pub use bbn_date_range::bbn_date_range;
use clap_complete::{generate, Shell};
Expand Down

0 comments on commit db9be9a

Please sign in to comment.