recurring_event 0.1.3

Calculate dates for recurring events (daily, weekly, monthly and yearly).
Documentation
use std::collections::hash_set::{HashSet, Iter};

use serde::{Serialize, Deserialize};
use time::{Weekday};

use crate::{MonthlyDayOrdinal, MonthlyWeekdayOrdinal};

#[derive(Debug, Deserialize, Serialize)]
pub struct Recurrences {
    weekdays: HashSet<Weekday>,
    monthly_days_ordinals: HashSet<MonthlyDayOrdinal>,
    monthly_weekdays_ordinals: HashSet<MonthlyWeekdayOrdinal>,
}

impl Recurrences {
    pub fn insert_monthly_day_ordinal(&mut self, day_ordinal: MonthlyDayOrdinal) -> bool {
        self.monthly_days_ordinals.insert(day_ordinal)
    }

    pub fn insert_monthly_weekday_ordinal(&mut self, weekday_ordinal: MonthlyWeekdayOrdinal) -> bool {
        self.monthly_weekdays_ordinals.insert(weekday_ordinal)
    }

    pub fn insert_weekday(&mut self, weekday: Weekday) -> bool {
        self.weekdays.insert(weekday)
    }

    pub fn monthly_days_ordinals(&self) -> Iter<MonthlyDayOrdinal> {
        self.monthly_days_ordinals.iter()
    }

    pub fn monthly_weekdays_ordinals(&self) ->Iter<MonthlyWeekdayOrdinal> {
        self.monthly_weekdays_ordinals.iter()
    }

    pub fn new() -> Recurrences {
        Recurrences {
            weekdays: HashSet::new(),
            monthly_days_ordinals: HashSet::new(),
            monthly_weekdays_ordinals: HashSet::new(),
        }
    }

    pub fn remove_monthly_day_ordinal(&mut self, day_ordinal: &MonthlyDayOrdinal) -> bool {
        self.monthly_days_ordinals.remove(day_ordinal)
    }

    pub fn remove_monthly_weekday_ordinal(&mut self, weekday_ordinal: &MonthlyWeekdayOrdinal) -> bool {
        self.monthly_weekdays_ordinals.remove(weekday_ordinal)
    }

    pub fn remove_weekday(&mut self, weekday: &Weekday) -> bool {
        self.weekdays.remove(weekday)
    }

    pub fn weekdays(&self) -> Iter<Weekday> {
        self.weekdays.iter()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    mod fields {
        use super::*;
        use crate::Ordinal;

        #[test]
        fn weekdays() {
            let mut expected = HashSet::new();
            expected.insert(Weekday::Tuesday);
            let mut recurrences = Recurrences::new();
            recurrences.insert_weekday(Weekday::Tuesday);
            let actual: HashSet<Weekday> = recurrences.weekdays().cloned().collect();
            assert_eq!(expected, actual, "actual: {:?}", actual);
        }

        #[test]
        fn monthly_days_ordinals() {
            let mut expected = HashSet::new();
            expected.insert(7);
            let mut recurrences = Recurrences::new();
            recurrences.insert_monthly_day_ordinal(7);
            let actual: HashSet<MonthlyDayOrdinal> = recurrences.monthly_days_ordinals().cloned().collect();
            assert_eq!(expected, actual, "actual: {:?}", actual);
        }

        #[test]
        fn monthly_weekdays_ordinals() {
            let mut expected = HashSet::new();
            expected.insert(MonthlyWeekdayOrdinal::new(Ordinal::Second, Weekday::Tuesday));
            let mut recurrences = Recurrences::new();
            recurrences.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Second, Weekday::Tuesday));
            let actual = recurrences.monthly_weekdays_ordinals().cloned().collect();
            assert_eq!(expected, actual, "actual: {:?}", actual);
        }
    }
}