minor changes and documentation
This commit is contained in:
parent
2e033fbd90
commit
243f1a8963
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
|
.env
|
16
.vscode/tasks.json
vendored
Normal file
16
.vscode/tasks.json
vendored
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -17,10 +17,24 @@ where
|
|||||||
fn new() -> (String, Vec<Holiday>);
|
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)]
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
|
||||||
pub enum CountryCode {
|
pub enum CountryCode {
|
||||||
|
/// Germany
|
||||||
DE,
|
DE,
|
||||||
|
|
||||||
|
/// The United States
|
||||||
US,
|
US,
|
||||||
|
|
||||||
|
/// France
|
||||||
FR,
|
FR,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,12 +75,6 @@ impl Holiday {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct HolidayDate {
|
|
||||||
pub name: String,
|
|
||||||
pub date: Date,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::holiday::Activity;
|
use crate::holiday::Activity;
|
||||||
|
76
src/lib.rs
76
src/lib.rs
@ -1,21 +1,28 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use time::Date;
|
pub use time::Date;
|
||||||
|
|
||||||
use crate::{
|
use crate::holiday::{HDate, Holiday};
|
||||||
countries::CountryCode,
|
|
||||||
holiday::{HDate, Holiday, HolidayDate},
|
|
||||||
};
|
|
||||||
|
|
||||||
mod countries;
|
mod countries;
|
||||||
mod holiday;
|
mod holiday;
|
||||||
pub(crate) mod utils;
|
pub(crate) mod utils;
|
||||||
|
|
||||||
|
pub use countries::CountryCode;
|
||||||
|
|
||||||
|
/// A struct to manage and check holidays for different countries
|
||||||
pub struct HolidayChecker {
|
pub struct HolidayChecker {
|
||||||
countries: HashMap<CountryCode, (String, Vec<Holiday>)>,
|
countries: HashMap<CountryCode, (String, Vec<Holiday>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HolidayChecker {
|
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 {
|
pub fn new(countries: Vec<CountryCode>) -> Self {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
|
|
||||||
@ -27,6 +34,15 @@ impl HolidayChecker {
|
|||||||
Self { countries: map }
|
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> {
|
pub fn holiday_list(&self, country: CountryCode, state: &str, year: i32) -> Vec<HolidayDate> {
|
||||||
Self::get_years_holidays(
|
Self::get_years_holidays(
|
||||||
self.countries
|
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 {
|
pub fn number_of_holidays(&self, country: CountryCode, state: &str, year: i32) -> usize {
|
||||||
Self::get_years_holidays(
|
Self::get_years_holidays(
|
||||||
self.countries
|
self.countries
|
||||||
@ -48,6 +73,16 @@ impl HolidayChecker {
|
|||||||
.len()
|
.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(
|
pub fn is_holiday(
|
||||||
&self,
|
&self,
|
||||||
country: CountryCode,
|
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(
|
fn get_years_holidays(
|
||||||
holidays: &(String, Vec<Holiday>),
|
holidays: &(String, Vec<Holiday>),
|
||||||
year: i32,
|
year: i32,
|
||||||
@ -103,3 +147,25 @@ impl HolidayChecker {
|
|||||||
.collect()
|
.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,
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user