From a5509152dd94e7c09a2d52e17c27804aa099ab17 Mon Sep 17 00:00:00 2001 From: Hlars Date: Wed, 6 Aug 2025 21:14:20 +0200 Subject: [PATCH] improvement & updated documentation --- Cargo.lock | 30 +----------- Cargo.toml | 12 +---- src/countries/de.rs | 13 ++--- src/countries/fr.rs | 21 +++++---- src/countries/mod.rs | 32 ++++++------- src/countries/us.rs | 15 +++--- src/lib.rs | 110 ++++++++++++++++++------------------------- 7 files changed, 88 insertions(+), 145 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f9057c0..c944e38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,17 +11,10 @@ dependencies = [ "powerfmt", ] -[[package]] -name = "heck" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" - [[package]] name = "holidays" -version = "0.1.1" +version = "0.2.0" dependencies = [ - "strum", "time", ] @@ -75,27 +68,6 @@ dependencies = [ "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]] name = "syn" version = "2.0.104" diff --git a/Cargo.toml b/Cargo.toml index 6117786..6d65794 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,21 +1,11 @@ [package] name = "holidays" -version = "0.1.1" +version = "0.2.0" edition = "2024" publish = ["kellnr"] -# [features] -# default = ["de"] -# # Includes Germany -# de = [] -# # Includes France -# fr = [] -# # Includes The United States -# us = [] - [dependencies] time = "0.3.41" -strum = { version = "0.27.2", features = ["derive"] } [dev-dependencies] time = { version = "0.3.41", features = ["macros"] } diff --git a/src/countries/de.rs b/src/countries/de.rs index 74ddde4..64f5d80 100644 --- a/src/countries/de.rs +++ b/src/countries/de.rs @@ -6,9 +6,14 @@ use crate::{ utils::{self}, }; +#[derive(Debug)] pub(crate) struct GermanHolidays(pub Vec>); -#[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 { /// Baden-Württemberg BW, @@ -46,11 +51,7 @@ pub enum GermanState { ANY, } -impl super::StateList for GermanState { - fn all_states_identifier() -> String { - Self::ANY.to_string() - } -} +impl super::StateList for GermanState {} impl GermanHolidays { pub(crate) fn new() -> Self { diff --git a/src/countries/fr.rs b/src/countries/fr.rs index b662eaf..6dc93be 100644 --- a/src/countries/fr.rs +++ b/src/countries/fr.rs @@ -6,9 +6,14 @@ use crate::{ utils, }; +#[derive(Debug)] pub(crate) struct FrenchHolidays(pub Vec>); -#[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 { /// Bas-Rhin BasRhin, @@ -20,11 +25,7 @@ pub enum FrenchState { ANY, } -impl super::StateList for FrenchState { - fn all_states_identifier() -> String { - Self::ANY.to_string() - } -} +impl super::StateList for FrenchState {} impl FrenchHolidays { pub(crate) fn new() -> Self { @@ -136,10 +137,10 @@ mod tests { let checker = HolidayChecker::new(); let tests = [ - (FrenchState::BasRhin, 14), - (FrenchState::HautRhin, 14), - (FrenchState::Moselle, 14), - (FrenchState::ANY, 12), + (FrenchState::BasRhin, 13), + (FrenchState::HautRhin, 13), + (FrenchState::Moselle, 13), + (FrenchState::ANY, 11), ]; for (state, num) in tests { assert_eq!( diff --git a/src/countries/mod.rs b/src/countries/mod.rs index c62cbb7..50506de 100644 --- a/src/countries/mod.rs +++ b/src/countries/mod.rs @@ -2,41 +2,39 @@ pub mod de; pub mod fr; pub mod us; -use std::{collections::HashMap, fmt::Display, hash::Hash}; +use std::{collections::HashMap, hash::Hash}; use crate::{ - countries::{ - de::{GermanHolidays, GermanState}, - fr::{FrenchHolidays, FrenchState}, - us::{USHolidays, USState}, - }, - holiday::{Activity, Holiday}, + countries::{de::GermanState, fr::FrenchState, us::USState}, + holiday::Activity, }; -/// Represents country codes for holiday calculations +/// Enum representing the country and state for holiday checking. /// -/// # Variants -/// * `DE` - Germany -/// * `US` - United States -/// * `FR` - France -/// -/// # Purpose -/// Provides a type-safe way to specify countries for holiday-related operations +/// This enum is used to specify the country and state for which holidays are to be checked. +/// Each variant of the enum corresponds to a different country and contains a state-specific type. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] + pub enum CountryState { /// Germany + /// + /// Contains a `GermanState` value representing the specific state within Germany. DE(GermanState), /// The United States + /// + /// Contains a `USState` value representing the specific state within the United States. US(USState), /// France + /// + /// Contains a `FrenchState` value representing the specific state within France. FR(FrenchState), } pub(crate) trait StateList where - Self: Sized + Display + Hash + Eq + Clone + Copy, + Self: Sized + Hash + Eq + Clone + Copy, { fn list(states: &[(Self, &[Activity])]) -> HashMap> { let mut map = HashMap::new(); @@ -45,6 +43,4 @@ where } map } - - fn all_states_identifier() -> String; } diff --git a/src/countries/us.rs b/src/countries/us.rs index d2db733..c33da83 100644 --- a/src/countries/us.rs +++ b/src/countries/us.rs @@ -1,4 +1,4 @@ -use time::{Date, Month, Weekday}; +use time::{Month, Weekday}; use crate::{ countries::StateList, @@ -6,9 +6,14 @@ use crate::{ utils, }; +#[derive(Debug)] pub(crate) struct USHolidays(pub Vec>); -#[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 { // Northeast Maine, @@ -76,11 +81,7 @@ pub enum USState { ANY, } -impl super::StateList for USState { - fn all_states_identifier() -> String { - Self::ANY.to_string() - } -} +impl super::StateList for USState {} impl USHolidays { pub(crate) fn new() -> Self { diff --git a/src/lib.rs b/src/lib.rs index d48f703..1fbcdc4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,31 +1,39 @@ pub use time::Date; -use crate::countries::de::{GermanHolidays, GermanState}; -use crate::countries::fr::{FrenchHolidays, FrenchState}; -use crate::countries::us::{USHolidays, USState}; -use crate::holiday::{HDate, Holiday}; +use crate::countries::de::GermanHolidays; +use crate::countries::fr::FrenchHolidays; +use crate::countries::us::USHolidays; mod countries; mod holiday; 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 { de: GermanHolidays, us: USHolidays, fr: FrenchHolidays, } +impl Default for HolidayChecker { + fn default() -> Self { + Self::new() + } +} + impl HolidayChecker { - /// Creates a new HolidayChecker with holidays for specified countries + /// Creates a new `HolidayChecker` instance. /// - /// # Arguments - /// * `countries` - A vector of country codes to initialize holidays for + /// This constructor initializes the holiday checkers for Germany, France, and the United States. /// /// # Returns - /// A new HolidayChecker instance with holidays for the specified countries + /// + /// A new `HolidayChecker` instance. pub fn new() -> Self { Self { 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 - /// * `country` - The country code - /// * `state` - The specific state within the country - /// * `year` - The year to retrieve holidays for + /// + /// * `country` - The country and state for which to retrieve the holidays. + /// * `year` - The year for which to retrieve the holidays. /// /// # 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 { 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 - /// * `country` - The country code - /// * `state` - The specific state within the country - /// * `year` - The year to count holidays for + /// + /// * `country` - The country and state for which to count the holidays. + /// * `year` - The year for which to count the holidays. /// /// # 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 { 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 - /// * `country` - The country code - /// * `state` - The specific state within the country - /// * `year` - The year to check - /// * `date` - The specific date to check + /// + /// * `country` - The country and state for which to check the holiday. + /// * `year` - The year for which to check the holiday. + /// * `date` - The date to check. /// /// # 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 { self.get_years_holidays(country, year) .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 - /// * `holidays` - A tuple containing all states identifier and list of holidays - /// * `year` - The year to retrieve holidays for - /// * `state` - The specific state to filter holidays + /// + /// * `country` - The country and state for which to retrieve the holidays. + /// * `year` - The year for which to retrieve the holidays. /// /// # 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 { match country { CountryState::DE(state) => self @@ -128,32 +143,6 @@ impl HolidayChecker { }) .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") /// * `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)] pub struct HolidayDate { /// The name of the holiday