summaryrefslogtreecommitdiff
path: root/src/options.rs
diff options
context:
space:
mode:
authorFredrik Meringdal <fmeringdal@hotmail.com>2020-12-04 16:13:58 +0100
committerFredrik Meringdal <fmeringdal@hotmail.com>2020-12-04 16:13:58 +0100
commit3931287312234b6a5f5da225c2a5b30f8d29e3c6 (patch)
tree7cd4549e9a23a468e5e4db2d013528445cfa2e07 /src/options.rs
parentc437b568f9dad67b212a9b17d2ac32dd40263571 (diff)
downloadrust_rrule-3931287312234b6a5f5da225c2a5b30f8d29e3c6.zip
support for nweekday
Diffstat (limited to 'src/options.rs')
-rw-r--r--src/options.rs49
1 files changed, 43 insertions, 6 deletions
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<isize>,
pub byweekno: Vec<isize>,
pub byweekday: Vec<usize>,
+ pub bynweekday: Vec<Vec<isize>>,
pub byhour: Vec<usize>,
pub byminute: Vec<usize>,
pub bysecond: Vec<usize>,
- pub bynweekday: Vec<Vec<isize>>,
pub byeaster: Option<isize>,
}
@@ -54,7 +88,7 @@ pub struct Options {
pub bymonthday: Option<Vec<isize>>,
pub byyearday: Option<Vec<isize>>,
pub byweekno: Option<Vec<isize>>,
- pub byweekday: Option<Vec<usize>>,
+ pub byweekday: Option<Vec<NWeekday>>,
pub byhour: Option<Vec<usize>>,
pub byminute: Option<Vec<usize>>,
pub bysecond: Option<Vec<usize>>,
@@ -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<Weekday>) -> 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<Weekday, String> {
match val {
"MO" => Ok(Weekday::Mon),
@@ -262,4 +299,4 @@ pub fn weekday_from_str(val: &str) -> Result<Weekday, String> {
"SU" => Ok(Weekday::Sun),
_ => Err(format!("Invalid weekday: {}", val)),
}
-} \ No newline at end of file
+}