diff options
-rw-r--r-- | src/options.rs | 16 | ||||
-rw-r--r-- | src/parse_options.rs | 20 | ||||
-rw-r--r-- | src/rrulestr.rs | 21 |
3 files changed, 39 insertions, 18 deletions
diff --git a/src/options.rs b/src/options.rs index ac35315..7f291bc 100644 --- a/src/options.rs +++ b/src/options.rs @@ -17,19 +17,25 @@ pub enum Frequenzy { Secondly = 6, } +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum NWeekdayIdentifier { + Every, + Identifier(isize), +} + #[derive(Copy, Clone, Debug)] pub struct NWeekday { pub weekday: usize, - pub n: isize, + pub n: NWeekdayIdentifier, } impl NWeekday { - pub fn new(weekday: usize, n: isize) -> Self { + pub fn new(weekday: usize, n: NWeekdayIdentifier) -> Self { // if (n === 0) throw new Error("Can't create weekday with n == 0") Self { weekday, n } } - pub fn from(weekday: &Weekday, n: isize) -> Self { + pub fn from(weekday: &Weekday, n: NWeekdayIdentifier) -> Self { // if (n === 0) throw new Error("Can't create weekday with n == 0") Self { weekday: get_weekday_val(weekday), @@ -37,7 +43,7 @@ impl NWeekday { } } - pub fn nth(&self, n: isize) -> Self { + pub fn nth(&self, n: NWeekdayIdentifier) -> Self { if self.n == n { return self.clone(); } @@ -235,7 +241,7 @@ impl Options { let byweekday = byweekday .iter() .map(|w| get_weekday_val(w)) - .map(|w| NWeekday::new(w, 1)) + .map(|w| NWeekday::new(w, NWeekdayIdentifier::Every)) .collect(); self.byweekday = Some(byweekday); self diff --git a/src/parse_options.rs b/src/parse_options.rs index 028686d..7953887 100644 --- a/src/parse_options.rs +++ b/src/parse_options.rs @@ -1,4 +1,4 @@ -use crate::options::{Frequenzy, NWeekday, Options, ParsedOptions, RRuleParseError}; +use crate::options::{Frequenzy, NWeekday, NWeekdayIdentifier, Options, ParsedOptions, RRuleParseError}; use crate::utils::is_some_and_not_empty; use chrono::prelude::*; use chrono_tz::{Tz, UTC}; @@ -70,7 +70,7 @@ pub fn parse_options(options: &Options) -> Result<ParsedOptions, RRuleParseError } Frequenzy::Weekly => { partial_options.byweekday = - Some(vec![NWeekday::new(dtstart.weekday() as usize, 1)]); + Some(vec![NWeekday::new(dtstart.weekday() as usize, NWeekdayIdentifier::Every)]); } _ => (), }; @@ -99,11 +99,19 @@ pub fn parse_options(options: &Options) -> Result<ParsedOptions, RRuleParseError if let Some(opts_byweekday) = partial_options.byweekday { for wday in opts_byweekday { - if wday.n == 1 { - byweekday.push(wday.weekday); - } else { - bynweekday.push(vec![wday.weekday as isize, wday.n]); + match wday.n { + NWeekdayIdentifier::Every => { + byweekday.push(wday.weekday) + } + NWeekdayIdentifier::Identifier(n) => { + bynweekday.push(vec![wday.weekday as isize, n]); + } } + // if wday.n == { + // byweekday.push(wday.weekday); + // } else { + // bynweekday.push(vec![wday.weekday as isize, wday.n]); + // } } } diff --git a/src/rrulestr.rs b/src/rrulestr.rs index 756a5ae..0ca4451 100644 --- a/src/rrulestr.rs +++ b/src/rrulestr.rs @@ -270,7 +270,7 @@ fn parse_weekday(val: &str) -> Result<Vec<NWeekday>, RRuleParseError> { if day.len() == 2 { // MO, TU, ... let wday = str_to_weekday(day)?; - wdays.push(NWeekday::new(wday, 1)); + wdays.push(NWeekday::new(wday, NWeekdayIdentifier::Every)); continue; } @@ -278,7 +278,7 @@ fn parse_weekday(val: &str) -> Result<Vec<NWeekday>, RRuleParseError> { let n = parts.get(1).unwrap().as_str().parse().unwrap(); let wdaypart = parts.get(2).unwrap(); let wday = str_to_weekday(wdaypart.as_str())?; - wdays.push(NWeekday::new(wday, n)); + wdays.push(NWeekday::new(wday, NWeekdayIdentifier::Identifier(n))); } Ok(wdays) } @@ -568,6 +568,7 @@ pub fn build_rrule(s: &str) -> Result<RRule, RRuleParseError> { #[cfg(test)] mod test { use super::*; + use super::NWeekdayIdentifier; use chrono_tz::{Tz, UTC}; #[test] @@ -674,6 +675,12 @@ mod test { } #[test] + fn parses_byday_as_nweekday_when_n_is_first() { + let res = build_rrule("DTSTART;VALUE=DATE:20200701\nRRULE:FREQ=MONTHLY;UNTIL=20210303T090000Z;INTERVAL=1;BYDAY=1WE").unwrap(); + assert_eq!(res.options.bynweekday, vec![vec![2,1]]); + } + + #[test] fn parses_byday_with_n() { let cases = vec![ "DTSTART:20200901T174500\nRRULE:FREQ=MONTHLY;UNTIL=20210504T154500Z;INTERVAL=1;BYDAY=1TU", @@ -693,11 +700,11 @@ mod test { "RRULE:FREQ=MONTHLY;UNTIL=20210524T090000Z;INTERVAL=1;BYDAY=4MO", ]; let opts = vec![ - vec![NWeekday::new(1,1)], - vec![NWeekday::new(2,1)], - vec![NWeekday::new(2,-1)], - vec![NWeekday::new(6, 12)], - vec![NWeekday::new(0,4)] + vec![NWeekday::new(1, NWeekdayIdentifier::Identifier(1))], + vec![NWeekday::new(2, NWeekdayIdentifier::Identifier(1))], + vec![NWeekday::new(2,NWeekdayIdentifier::Identifier(-1))], + vec![NWeekday::new(6, NWeekdayIdentifier::Identifier(12))], + vec![NWeekday::new(0,NWeekdayIdentifier::Identifier(4))] ]; for i in 0..cases.len() { let opts_or_err = parse_string(cases[i]); |