minor changes and documentation

This commit is contained in:
Hlars 2025-08-04 21:20:28 +02:00
parent 2e033fbd90
commit 243f1a8963
6 changed files with 102 additions and 11 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target
.env

16
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,16 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Package and Upload Documentation",
"command": "source ./.env && CRATE_NAME=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].name') && CRATE_VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version') && cargo doc --no-deps && cd ./target && zip -r doc.zip ./doc && curl -v -H \"Authorization: ${KELLNR_TOKEN}\" https://crates.esteil.dedyn.io/api/v1/docs/$CRATE_NAME/$CRATE_VERSION --upload-file ./doc.zip",
"args": [],
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@ -17,10 +17,24 @@ where
fn new() -> (String, Vec<Holiday>);
}
/// Represents country codes for holiday calculations
///
/// # Variants
/// * `DE` - Germany
/// * `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)]
pub enum CountryCode {
/// Germany
DE,
/// The United States
US,
/// France
FR,
}

View File

@ -75,12 +75,6 @@ impl Holiday {
}
}
#[derive(Debug, Clone)]
pub struct HolidayDate {
pub name: String,
pub date: Date,
}
#[cfg(test)]
mod test {
use crate::holiday::Activity;

View File

@ -1,21 +1,28 @@
use std::collections::HashMap;
use time::Date;
pub use time::Date;
use crate::{
countries::CountryCode,
holiday::{HDate, Holiday, HolidayDate},
};
use crate::holiday::{HDate, Holiday};
mod countries;
mod holiday;
pub(crate) mod utils;
pub use countries::CountryCode;
/// A struct to manage and check holidays for different countries
pub struct HolidayChecker {
countries: HashMap<CountryCode, (String, Vec<Holiday>)>,
}
impl HolidayChecker {
/// Creates a new HolidayChecker with holidays for specified countries
///
/// # Arguments
/// * `countries` - A vector of country codes to initialize holidays for
///
/// # Returns
/// A new HolidayChecker instance with holidays for the specified countries
pub fn new(countries: Vec<CountryCode>) -> Self {
let mut map = HashMap::new();
@ -27,6 +34,15 @@ impl HolidayChecker {
Self { countries: map }
}
/// Retrieves a list of holidays for a specific country, state, and year
///
/// # Arguments
/// * `country` - The country code
/// * `state` - The specific state within the country
/// * `year` - The year to retrieve holidays for
///
/// # Returns
/// A vector of HolidayDate for the specified country, state, and year
pub fn holiday_list(&self, country: CountryCode, state: &str, year: i32) -> Vec<HolidayDate> {
Self::get_years_holidays(
self.countries
@ -37,6 +53,15 @@ impl HolidayChecker {
)
}
/// Counts the number of holidays for a specific country, state, and year
///
/// # Arguments
/// * `country` - The country code
/// * `state` - The specific state within the country
/// * `year` - The year to count holidays for
///
/// # Returns
/// The number of holidays for the specified country, state, and year
pub fn number_of_holidays(&self, country: CountryCode, state: &str, year: i32) -> usize {
Self::get_years_holidays(
self.countries
@ -48,6 +73,16 @@ impl HolidayChecker {
.len()
}
/// Checks if a specific date is a holiday for a given country, state, 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
///
/// # Returns
/// An Option containing the HolidayDate if the date is a holiday, None otherwise
pub fn is_holiday(
&self,
country: CountryCode,
@ -70,6 +105,15 @@ impl HolidayChecker {
})
}
/// Internal method to retrieve holidays for a specific year and state
///
/// # 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
///
/// # Returns
/// A vector of HolidayDate for the specified year and state
fn get_years_holidays(
holidays: &(String, Vec<Holiday>),
year: i32,
@ -103,3 +147,25 @@ impl HolidayChecker {
.collect()
}
}
/// Represents a specific holiday with its name and date
///
/// # Fields
/// * `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
pub name: String,
/// The date when the holiday is celebrated
pub date: Date,
}