improvement & updated documentation

This commit is contained in:
Hlars 2025-08-06 21:14:20 +02:00
parent 8a78e8f901
commit a5509152dd
7 changed files with 88 additions and 145 deletions

30
Cargo.lock generated
View File

@ -11,17 +11,10 @@ dependencies = [
"powerfmt", "powerfmt",
] ]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]] [[package]]
name = "holidays" name = "holidays"
version = "0.1.1" version = "0.2.0"
dependencies = [ dependencies = [
"strum",
"time", "time",
] ]
@ -75,27 +68,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "strum"
version = "0.27.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af23d6f6c1a224baef9d3f61e287d2761385a5b88fdab4eb4c6f11aeb54c4bcf"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.27.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7695ce3845ea4b33927c055a39dc438a45b059f7c1b3d91d38d10355fb8cbca7"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.104" version = "2.0.104"

View File

@ -1,21 +1,11 @@
[package] [package]
name = "holidays" name = "holidays"
version = "0.1.1" version = "0.2.0"
edition = "2024" edition = "2024"
publish = ["kellnr"] publish = ["kellnr"]
# [features]
# default = ["de"]
# # Includes Germany
# de = []
# # Includes France
# fr = []
# # Includes The United States
# us = []
[dependencies] [dependencies]
time = "0.3.41" time = "0.3.41"
strum = { version = "0.27.2", features = ["derive"] }
[dev-dependencies] [dev-dependencies]
time = { version = "0.3.41", features = ["macros"] } time = { version = "0.3.41", features = ["macros"] }

View File

@ -6,9 +6,14 @@ use crate::{
utils::{self}, utils::{self},
}; };
#[derive(Debug)]
pub(crate) struct GermanHolidays(pub Vec<Holiday<GermanState>>); pub(crate) struct GermanHolidays(pub Vec<Holiday<GermanState>>);
#[derive(Debug, strum::Display, Clone, Copy, Hash, PartialEq, Eq)] /// Enum representing the states in Germany for holiday checking.
///
/// This enum is used to specify the specific state within Germany for which holidays are to be checked.
/// Each variant of the enum corresponds to a different state in Germany.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum GermanState { pub enum GermanState {
/// Baden-Württemberg /// Baden-Württemberg
BW, BW,
@ -46,11 +51,7 @@ pub enum GermanState {
ANY, ANY,
} }
impl super::StateList for GermanState { impl super::StateList for GermanState {}
fn all_states_identifier() -> String {
Self::ANY.to_string()
}
}
impl GermanHolidays { impl GermanHolidays {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {

View File

@ -6,9 +6,14 @@ use crate::{
utils, utils,
}; };
#[derive(Debug)]
pub(crate) struct FrenchHolidays(pub Vec<Holiday<FrenchState>>); pub(crate) struct FrenchHolidays(pub Vec<Holiday<FrenchState>>);
#[derive(Debug, Clone, Copy, strum::Display, Hash, PartialEq, Eq)] /// Enum representing the states in France for holiday checking.
///
/// This enum is used to specify the specific state within France for which holidays are to be checked.
/// Each variant of the enum corresponds to a different state in France.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum FrenchState { pub enum FrenchState {
/// Bas-Rhin /// Bas-Rhin
BasRhin, BasRhin,
@ -20,11 +25,7 @@ pub enum FrenchState {
ANY, ANY,
} }
impl super::StateList for FrenchState { impl super::StateList for FrenchState {}
fn all_states_identifier() -> String {
Self::ANY.to_string()
}
}
impl FrenchHolidays { impl FrenchHolidays {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
@ -136,10 +137,10 @@ mod tests {
let checker = HolidayChecker::new(); let checker = HolidayChecker::new();
let tests = [ let tests = [
(FrenchState::BasRhin, 14), (FrenchState::BasRhin, 13),
(FrenchState::HautRhin, 14), (FrenchState::HautRhin, 13),
(FrenchState::Moselle, 14), (FrenchState::Moselle, 13),
(FrenchState::ANY, 12), (FrenchState::ANY, 11),
]; ];
for (state, num) in tests { for (state, num) in tests {
assert_eq!( assert_eq!(

View File

@ -2,41 +2,39 @@ pub mod de;
pub mod fr; pub mod fr;
pub mod us; pub mod us;
use std::{collections::HashMap, fmt::Display, hash::Hash}; use std::{collections::HashMap, hash::Hash};
use crate::{ use crate::{
countries::{ countries::{de::GermanState, fr::FrenchState, us::USState},
de::{GermanHolidays, GermanState}, holiday::Activity,
fr::{FrenchHolidays, FrenchState},
us::{USHolidays, USState},
},
holiday::{Activity, Holiday},
}; };
/// Represents country codes for holiday calculations /// Enum representing the country and state for holiday checking.
/// ///
/// # Variants /// This enum is used to specify the country and state for which holidays are to be checked.
/// * `DE` - Germany /// Each variant of the enum corresponds to a different country and contains a state-specific type.
/// * `US` - United States
/// * `FR` - France
///
/// # Purpose
/// Provides a type-safe way to specify countries for holiday-related operations
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum CountryState { pub enum CountryState {
/// Germany /// Germany
///
/// Contains a `GermanState` value representing the specific state within Germany.
DE(GermanState), DE(GermanState),
/// The United States /// The United States
///
/// Contains a `USState` value representing the specific state within the United States.
US(USState), US(USState),
/// France /// France
///
/// Contains a `FrenchState` value representing the specific state within France.
FR(FrenchState), FR(FrenchState),
} }
pub(crate) trait StateList pub(crate) trait StateList
where where
Self: Sized + Display + Hash + Eq + Clone + Copy, Self: Sized + Hash + Eq + Clone + Copy,
{ {
fn list(states: &[(Self, &[Activity])]) -> HashMap<Self, Vec<Activity>> { fn list(states: &[(Self, &[Activity])]) -> HashMap<Self, Vec<Activity>> {
let mut map = HashMap::new(); let mut map = HashMap::new();
@ -45,6 +43,4 @@ where
} }
map map
} }
fn all_states_identifier() -> String;
} }

View File

@ -1,4 +1,4 @@
use time::{Date, Month, Weekday}; use time::{Month, Weekday};
use crate::{ use crate::{
countries::StateList, countries::StateList,
@ -6,9 +6,14 @@ use crate::{
utils, utils,
}; };
#[derive(Debug)]
pub(crate) struct USHolidays(pub Vec<Holiday<USState>>); pub(crate) struct USHolidays(pub Vec<Holiday<USState>>);
#[derive(Debug, Clone, Copy, strum::Display, Hash, PartialEq, Eq)] /// Enum representing the states in The United States of America for holiday checking.
///
/// This enum is used to specify the specific state within he United States of America for which holidays are to be checked.
/// Each variant of the enum corresponds to a different state in he United States of America.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum USState { pub enum USState {
// Northeast // Northeast
Maine, Maine,
@ -76,11 +81,7 @@ pub enum USState {
ANY, ANY,
} }
impl super::StateList for USState { impl super::StateList for USState {}
fn all_states_identifier() -> String {
Self::ANY.to_string()
}
}
impl USHolidays { impl USHolidays {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {

View File

@ -1,31 +1,39 @@
pub use time::Date; pub use time::Date;
use crate::countries::de::{GermanHolidays, GermanState}; use crate::countries::de::GermanHolidays;
use crate::countries::fr::{FrenchHolidays, FrenchState}; use crate::countries::fr::FrenchHolidays;
use crate::countries::us::{USHolidays, USState}; use crate::countries::us::USHolidays;
use crate::holiday::{HDate, Holiday};
mod countries; mod countries;
mod holiday; mod holiday;
pub(crate) mod utils; pub(crate) mod utils;
use countries::CountryState; pub use countries::{CountryState, de::GermanState, fr::FrenchState, us::USState};
/// A struct to manage and check holidays for different countries #[derive(Debug)]
/// Struct for checking holidays in different countries.
///
/// The `HolidayChecker` struct provides methods to check holidays for Germany, the United States, and France.
pub struct HolidayChecker { pub struct HolidayChecker {
de: GermanHolidays, de: GermanHolidays,
us: USHolidays, us: USHolidays,
fr: FrenchHolidays, fr: FrenchHolidays,
} }
impl Default for HolidayChecker {
fn default() -> Self {
Self::new()
}
}
impl HolidayChecker { impl HolidayChecker {
/// Creates a new HolidayChecker with holidays for specified countries /// Creates a new `HolidayChecker` instance.
/// ///
/// # Arguments /// This constructor initializes the holiday checkers for Germany, France, and the United States.
/// * `countries` - A vector of country codes to initialize holidays for
/// ///
/// # Returns /// # Returns
/// A new HolidayChecker instance with holidays for the specified countries ///
/// A new `HolidayChecker` instance.
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
de: GermanHolidays::new(), de: GermanHolidays::new(),
@ -34,42 +42,45 @@ impl HolidayChecker {
} }
} }
/// Retrieves a list of holidays for a specific country, state, and year /// Returns a list of holidays for the specified country and year.
/// ///
/// # Arguments /// # Arguments
/// * `country` - The country code ///
/// * `state` - The specific state within the country /// * `country` - The country and state for which to retrieve the holidays.
/// * `year` - The year to retrieve holidays for /// * `year` - The year for which to retrieve the holidays.
/// ///
/// # Returns /// # Returns
/// A vector of HolidayDate for the specified country, state, and year ///
/// A vector of `HolidayDate` objects representing the holidays for the specified country and year.
pub fn holiday_list(&self, country: CountryState, year: i32) -> Vec<HolidayDate> { pub fn holiday_list(&self, country: CountryState, year: i32) -> Vec<HolidayDate> {
self.get_years_holidays(country, year) self.get_years_holidays(country, year)
} }
/// Counts the number of holidays for a specific country, state, and year /// Returns the number of holidays for the specified country and year.
/// ///
/// # Arguments /// # Arguments
/// * `country` - The country code ///
/// * `state` - The specific state within the country /// * `country` - The country and state for which to count the holidays.
/// * `year` - The year to count holidays for /// * `year` - The year for which to count the holidays.
/// ///
/// # Returns /// # Returns
/// The number of holidays for the specified country, state, and year ///
/// The number of holidays for the specified country and year.
pub fn number_of_holidays(&self, country: CountryState, year: i32) -> usize { pub fn number_of_holidays(&self, country: CountryState, year: i32) -> usize {
self.get_years_holidays(country, year).len() self.get_years_holidays(country, year).len()
} }
/// Checks if a specific date is a holiday for a given country, state, and year /// Checks if a given date is a holiday for the specified country and year.
/// ///
/// # Arguments /// # Arguments
/// * `country` - The country code ///
/// * `state` - The specific state within the country /// * `country` - The country and state for which to check the holiday.
/// * `year` - The year to check /// * `year` - The year for which to check the holiday.
/// * `date` - The specific date to check /// * `date` - The date to check.
/// ///
/// # Returns /// # Returns
/// An Option containing the HolidayDate if the date is a holiday, None otherwise ///
/// An `Option` containing a `HolidayDate` if the date is a holiday, otherwise `None`.
pub fn is_holiday(&self, country: CountryState, year: i32, date: Date) -> Option<HolidayDate> { pub fn is_holiday(&self, country: CountryState, year: i32, date: Date) -> Option<HolidayDate> {
self.get_years_holidays(country, year) self.get_years_holidays(country, year)
.iter() .iter()
@ -80,15 +91,19 @@ impl HolidayChecker {
}) })
} }
/// Internal method to retrieve holidays for a specific year and state /// Retrieves the list of holidays for the specified country and year.
///
/// This method is internal and should not be called directly. It is used by the public methods
/// to get the list of holidays for a given country and year.
/// ///
/// # Arguments /// # Arguments
/// * `holidays` - A tuple containing all states identifier and list of holidays ///
/// * `year` - The year to retrieve holidays for /// * `country` - The country and state for which to retrieve the holidays.
/// * `state` - The specific state to filter holidays /// * `year` - The year for which to retrieve the holidays.
/// ///
/// # Returns /// # Returns
/// A vector of HolidayDate for the specified year and state ///
/// A vector of `HolidayDate` objects representing the holidays for the specified country and year.
fn get_years_holidays(&self, country: CountryState, year: i32) -> Vec<HolidayDate> { fn get_years_holidays(&self, country: CountryState, year: i32) -> Vec<HolidayDate> {
match country { match country {
CountryState::DE(state) => self CountryState::DE(state) => self
@ -128,32 +143,6 @@ impl HolidayChecker {
}) })
.collect(), .collect(),
} }
// holidays
// .iter()
// .filter_map(|holiday| {
// // check if holiday is active
// if !holiday.is_active(year, all_states_identifier, state) {
// return None;
// }
// match holiday.date {
// HDate::Calculated(calc_fn) => {
// // For holidays with calculation function
// Some(HolidayDate {
// date: calc_fn(year),
// name: holiday.name.clone(),
// })
// }
// HDate::Fixed(month, day) => {
// // For fixed date holidays
// Some(HolidayDate {
// date: Date::from_calendar_date(year, month, day).unwrap(),
// name: holiday.name.clone(),
// })
// }
// }
// })
// .collect()
} }
} }
@ -163,13 +152,6 @@ impl HolidayChecker {
/// * `name` - The name of the holiday (e.g., "Christmas", "Independence Day") /// * `name` - The name of the holiday (e.g., "Christmas", "Independence Day")
/// * `date` - The specific date when the holiday occurs /// * `date` - The specific date when the holiday occurs
/// ///
/// # Examples
/// ```rust
/// let christmas = HolidayDate {
/// name: String::from("Christmas"),
/// date: Date::from_calendar_date(2023, Month::December, 25).unwrap()
/// };
/// ```
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct HolidayDate { pub struct HolidayDate {
/// The name of the holiday /// The name of the holiday