From 3931287312234b6a5f5da225c2a5b30f8d29e3c6 Mon Sep 17 00:00:00 2001 From: Fredrik Meringdal Date: Fri, 4 Dec 2020 16:13:58 +0100 Subject: support for nweekday --- src/options.rs | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'src/options.rs') diff --git a/src/options.rs b/src/options.rs index 0863294..ac35315 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,8 +1,8 @@ use crate::datetime::{get_weekday_val, DTime}; use crate::parse_options::parse_options; use chrono::prelude::*; -use serde::{Serialize, Deserialize}; use chrono_tz::{Tz, UTC}; +use serde::{Deserialize, Serialize}; use std::error::Error; use std::fmt::{Display, Formatter}; @@ -17,6 +17,40 @@ pub enum Frequenzy { Secondly = 6, } +#[derive(Copy, Clone, Debug)] +pub struct NWeekday { + pub weekday: usize, + pub n: isize, +} + +impl NWeekday { + pub fn new(weekday: usize, n: isize) -> 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 { + // if (n === 0) throw new Error("Can't create weekday with n == 0") + Self { + weekday: get_weekday_val(weekday), + n, + } + } + + pub fn nth(&self, n: isize) -> Self { + if self.n == n { + return self.clone(); + } + Self::new(self.weekday, n) + } +} + +impl PartialEq for NWeekday { + fn eq(&self, other: &Self) -> bool { + self.n == other.n && self.weekday == other.weekday + } +} + #[derive(Debug, Clone)] pub struct ParsedOptions { pub freq: Frequenzy, @@ -33,10 +67,10 @@ pub struct ParsedOptions { pub byyearday: Vec, pub byweekno: Vec, pub byweekday: Vec, + pub bynweekday: Vec>, pub byhour: Vec, pub byminute: Vec, pub bysecond: Vec, - pub bynweekday: Vec>, pub byeaster: Option, } @@ -54,7 +88,7 @@ pub struct Options { pub bymonthday: Option>, pub byyearday: Option>, pub byweekno: Option>, - pub byweekday: Option>, + pub byweekday: Option>, pub byhour: Option>, pub byminute: Option>, pub bysecond: Option>, @@ -198,7 +232,11 @@ impl Options { /// When given, these variables will define the weekdays where the recurrence /// will be applied. pub fn byweekday(mut self, byweekday: Vec) -> Self { - let byweekday = byweekday.iter().map(|w| get_weekday_val(w)).collect(); + let byweekday = byweekday + .iter() + .map(|w| get_weekday_val(w)) + .map(|w| NWeekday::new(w, 1)) + .collect(); self.byweekday = Some(byweekday); self } @@ -250,7 +288,6 @@ impl Display for RRuleParseError { impl Error for RRuleParseError {} - pub fn weekday_from_str(val: &str) -> Result { match val { "MO" => Ok(Weekday::Mon), @@ -262,4 +299,4 @@ pub fn weekday_from_str(val: &str) -> Result { "SU" => Ok(Weekday::Sun), _ => Err(format!("Invalid weekday: {}", val)), } -} \ No newline at end of file +} -- cgit v1.2.3