use chrono::{Local, NaiveDate};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FirstDay {
#[default]
Monday,
Sunday,
}
fn default_date() -> NaiveDate {
Local::now().date_naive()
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ParserConfig {
pub first_day: FirstDay,
pub next_weekday_means_week: bool,
pub next_day_of_month_means_month: bool,
pub next_partial_date_means_year: bool,
pub last_weekday_means_week: bool,
pub last_day_of_month_means_month: bool,
pub last_partial_date_means_year: bool,
#[cfg_attr(feature = "serde", serde(skip, default = "default_date"))]
pub today: NaiveDate,
}
impl Default for ParserConfig {
fn default() -> Self {
Self {
first_day: FirstDay::Monday,
next_weekday_means_week: false,
next_day_of_month_means_month: false,
next_partial_date_means_year: false,
last_weekday_means_week: false,
last_day_of_month_means_month: false,
last_partial_date_means_year: false,
today: default_date(),
}
}
}
impl ParserConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_today(&mut self, today: NaiveDate) -> &mut Self {
self.today = today;
self
}
pub fn week_starts_monday(&mut self) -> &mut Self {
self.first_day = FirstDay::Monday;
self
}
pub fn week_starts_sunday(&mut self) -> &mut Self {
self.first_day = FirstDay::Sunday;
self
}
pub fn next_weekday_means_week(&mut self) -> &mut Self {
self.next_weekday_means_week = true;
self
}
pub fn next_weekday_means_closest(&mut self) -> &mut Self {
self.next_weekday_means_week = false;
self
}
pub fn next_day_of_month_means_month(&mut self) -> &mut Self {
self.next_day_of_month_means_month = true;
self
}
pub fn next_day_of_month_means_closest(&mut self) -> &mut Self {
self.next_day_of_month_means_month = false;
self
}
pub fn next_partial_date_means_year(&mut self) -> &mut Self {
self.next_partial_date_means_year = true;
self
}
pub fn next_partial_date_means_closest(&mut self) -> &mut Self {
self.next_partial_date_means_year = false;
self
}
pub fn last_weekday_means_week(&mut self) -> &mut Self {
self.last_weekday_means_week = true;
self
}
pub fn last_weekday_means_closest(&mut self) -> &mut Self {
self.last_weekday_means_week = false;
self
}
pub fn last_day_of_month_means_month(&mut self) -> &mut Self {
self.last_day_of_month_means_month = true;
self
}
pub fn last_day_of_month_means_closest(&mut self) -> &mut Self {
self.last_day_of_month_means_month = false;
self
}
pub fn last_partial_date_means_year(&mut self) -> &mut Self {
self.last_partial_date_means_year = true;
self
}
pub fn last_partial_date_means_closest(&mut self) -> &mut Self {
self.last_partial_date_means_year = false;
self
}
}