summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFredrik Meringdal <fmeringdal@hotmail.com>2020-10-15 20:46:09 +0200
committerFredrik Meringdal <fmeringdal@hotmail.com>2020-10-15 20:46:09 +0200
commit3575783180b9faa54e6973f2dadaa4a8bd5a66eb (patch)
tree7f4f489cd3e5f76165a977cd789fab7ced47a908 /src
parent37cc6eac78444d4abdb4988a8f6eeca3beacf7bd (diff)
downloadrust_rrule-3575783180b9faa54e6973f2dadaa4a8bd5a66eb.zip
easier building of options
Diffstat (limited to 'src')
-rw-r--r--src/iter.rs1
-rw-r--r--src/iterinfo.rs1
-rw-r--r--src/lib.rs1
-rw-r--r--src/monthinfo.rs33
-rw-r--r--src/options.rs145
-rw-r--r--src/yearinfo.rs80
6 files changed, 150 insertions, 111 deletions
diff --git a/src/iter.rs b/src/iter.rs
index 026ae31..fe10479 100644
--- a/src/iter.rs
+++ b/src/iter.rs
@@ -1,5 +1,6 @@
use crate::datetime::*;
use crate::iterinfo::*;
+use crate::options::*;
use crate::poslist::*;
use crate::yearinfo::*;
use chrono::prelude::*;
diff --git a/src/iterinfo.rs b/src/iterinfo.rs
index 04fdb77..5b9a8d6 100644
--- a/src/iterinfo.rs
+++ b/src/iterinfo.rs
@@ -1,5 +1,6 @@
use crate::datetime::*;
use crate::monthinfo::*;
+use crate::options::*;
use crate::yearinfo::*;
use chrono::prelude::*;
use chrono::*;
diff --git a/src/lib.rs b/src/lib.rs
index 8e80b7d..e8eb2af 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -5,5 +5,6 @@ pub mod iter;
pub mod iterinfo;
pub mod masks;
pub mod monthinfo;
+pub mod options;
pub mod poslist;
pub mod yearinfo;
diff --git a/src/monthinfo.rs b/src/monthinfo.rs
index 3b82ee5..4b70c17 100644
--- a/src/monthinfo.rs
+++ b/src/monthinfo.rs
@@ -1,4 +1,5 @@
use crate::masks::Masks;
+use crate::options::*;
use crate::yearinfo::*;
use chrono::prelude::*;
use chrono::{DateTime, Duration};
@@ -72,35 +73,3 @@ pub fn rebuild_month(
result
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn it_works() {
- let options = ParsedOptions {
- freq: Frequenzy::YEARLY,
- interval: 1,
- count: Some(10),
- until: None,
- tzid: None,
- dtstart: Utc.ymd(2020, 1, 1).and_hms(0, 0, 0),
- wkst: 0,
- bysetpos: vec![],
- bymonth: vec![],
- bymonthday: vec![],
- bynmonthday: vec![],
- byyearday: vec![],
- byweekno: vec![1],
- byweekday: vec![],
- byhour: vec![],
- byminute: vec![],
- bysecond: vec![],
- bynweekday: vec![],
- };
- let res = rebuild_month(2020, 1, 366, &vec![], &vec![], &options);
- println!("Res: {:?}", res);
- assert_eq!(2 + 2, 4);
- }
-}
diff --git a/src/options.rs b/src/options.rs
new file mode 100644
index 0000000..257eb17
--- /dev/null
+++ b/src/options.rs
@@ -0,0 +1,145 @@
+use chrono::prelude::*;
+
+#[derive(Debug)]
+pub struct YearInfo {
+ pub yearlen: usize,
+ pub nextyearlen: usize,
+ pub yearordinal: isize,
+ pub yearweekday: usize,
+ pub mmask: Vec<usize>,
+ pub mrange: Vec<usize>,
+ pub mdaymask: Vec<usize>,
+ pub nmdaymask: Vec<isize>,
+ pub wdaymask: Vec<usize>,
+ pub wnomask: Option<Vec<usize>>,
+}
+
+#[derive(Debug, PartialEq, PartialOrd)]
+pub enum Frequenzy {
+ YEARLY = 0,
+ MONTHLY = 1,
+ WEEKLY = 2,
+ DAILY = 3,
+ HOURLY = 4,
+ MINUTELY = 5,
+ SECONDLY = 6,
+}
+
+#[derive(Debug)]
+pub struct ParsedOptions {
+ pub freq: Frequenzy,
+ pub interval: usize,
+ pub count: Option<u32>,
+ pub until: Option<DateTime<Utc>>,
+ pub tzid: Option<String>,
+ pub dtstart: DateTime<Utc>,
+ pub wkst: usize,
+ pub bysetpos: Vec<usize>,
+ pub bymonth: Vec<usize>,
+ pub bymonthday: Vec<usize>,
+ pub bynmonthday: Vec<isize>,
+ pub byyearday: Vec<usize>,
+ pub byweekno: Vec<isize>,
+ pub byweekday: Vec<usize>,
+ pub byhour: Vec<usize>,
+ pub byminute: Vec<usize>,
+ pub bysecond: Vec<usize>,
+ pub bynweekday: Vec<Vec<isize>>,
+}
+impl ParsedOptions {
+ pub fn new(freq: Frequenzy, interval: usize, dtstart: &DateTime<Utc>) -> Self {
+ Self {
+ freq,
+ interval,
+ count: None,
+ until: None,
+ tzid: None,
+ dtstart: dtstart.clone(),
+ wkst: 0,
+ bysetpos: vec![],
+ bymonth: vec![],
+ bymonthday: vec![],
+ bynmonthday: vec![],
+ byyearday: vec![],
+ byweekno: vec![],
+ byweekday: vec![],
+ bynweekday: vec![],
+ byhour: vec![dtstart.hour() as usize],
+ byminute: vec![dtstart.minute() as usize],
+ bysecond: vec![dtstart.second() as usize],
+ }
+ }
+
+ pub fn freq(mut self, freq: Frequenzy) -> Self {
+ self.freq = freq;
+ self
+ }
+ pub fn interval(mut self, interval: usize) -> Self {
+ self.interval = interval;
+ self
+ }
+ pub fn until(mut self, until: &DateTime<Utc>) -> Self {
+ self.until = Some(until.clone());
+ self
+ }
+ pub fn tzid(mut self, tzid: &String) -> Self {
+ self.tzid = Some(tzid.clone());
+ self
+ }
+ pub fn count(mut self, count: u32) -> Self {
+ self.count = Some(count);
+ self
+ }
+ pub fn dtstart(mut self, dtstart: DateTime<Utc>) -> Self {
+ self.dtstart = dtstart;
+ self
+ }
+ pub fn wkst(mut self, wkst: usize) -> Self {
+ self.wkst = wkst;
+ self
+ }
+ pub fn bysetpos(mut self, bysetpos: Vec<usize>) -> Self {
+ self.bysetpos = bysetpos;
+ self
+ }
+ pub fn bymonth(mut self, bymonth: Vec<usize>) -> Self {
+ self.bymonth = bymonth;
+ self
+ }
+ pub fn bymonthday(mut self, bymonthday: Vec<usize>) -> Self {
+ self.bymonthday = bymonthday;
+ self
+ }
+ pub fn bynmonthday(mut self, bynmonthday: Vec<isize>) -> Self {
+ self.bynmonthday = bynmonthday;
+ self
+ }
+ pub fn byyearday(mut self, byyearday: Vec<usize>) -> Self {
+ self.byyearday = byyearday;
+ self
+ }
+ pub fn byweekno(mut self, byweekno: Vec<isize>) -> Self {
+ self.byweekno = byweekno;
+ self
+ }
+ pub fn byweekday(mut self, byweekday: Vec<usize>) -> Self {
+ self.byweekday = byweekday;
+ self
+ }
+ pub fn bynweekday(mut self, bynweekday: Vec<Vec<isize>>) -> Self {
+ self.bynweekday = bynweekday;
+ self
+ }
+ pub fn byhour(mut self, byhour: Vec<usize>) -> Self {
+ self.byhour = byhour;
+ self
+ }
+ pub fn byminute(mut self, byminute: Vec<usize>) -> Self {
+ self.byminute = byminute;
+ self
+ }
+ pub fn bysecond(mut self, bysecond: Vec<usize>) -> Self {
+ self.bysecond = bysecond;
+ self
+ }
+}
diff --git a/src/yearinfo.rs b/src/yearinfo.rs
index 37af64d..a2cd972 100644
--- a/src/yearinfo.rs
+++ b/src/yearinfo.rs
@@ -1,54 +1,8 @@
use crate::masks::Masks;
+use crate::options::*;
use chrono::prelude::*;
use chrono::{DateTime, Duration};
-#[derive(Debug)]
-pub struct YearInfo {
- pub yearlen: usize,
- pub nextyearlen: usize,
- pub yearordinal: isize,
- pub yearweekday: usize,
- pub mmask: Vec<usize>,
- pub mrange: Vec<usize>,
- pub mdaymask: Vec<usize>,
- pub nmdaymask: Vec<isize>,
- pub wdaymask: Vec<usize>,
- pub wnomask: Option<Vec<usize>>,
-}
-
-#[derive(Debug, PartialEq, PartialOrd)]
-pub enum Frequenzy {
- YEARLY = 0,
- MONTHLY = 1,
- WEEKLY = 2,
- DAILY = 3,
- HOURLY = 4,
- MINUTELY = 5,
- SECONDLY = 6,
-}
-
-#[derive(Debug)]
-pub struct ParsedOptions {
- pub freq: Frequenzy,
- pub interval: usize,
- pub count: Option<u32>,
- pub until: Option<DateTime<Utc>>,
- pub tzid: Option<String>,
- pub dtstart: DateTime<Utc>,
- pub wkst: usize,
- pub bysetpos: Vec<usize>,
- pub bymonth: Vec<usize>,
- pub bymonthday: Vec<usize>,
- pub bynmonthday: Vec<isize>,
- pub byyearday: Vec<usize>,
- pub byweekno: Vec<isize>,
- pub byweekday: Vec<usize>,
- pub byhour: Vec<usize>,
- pub byminute: Vec<usize>,
- pub bysecond: Vec<usize>,
- pub bynweekday: Vec<Vec<isize>>,
-}
-
fn is_leap_year(year: i32) -> bool {
year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
}
@@ -250,35 +204,3 @@ pub fn rebuild_year(year: i32, options: &ParsedOptions) -> YearInfo {
result
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn it_works() {
- let options = ParsedOptions {
- freq: Frequenzy::YEARLY,
- interval: 1,
- count: Some(10),
- until: None,
- tzid: None,
- dtstart: Utc.ymd(2020, 1, 1).and_hms(0, 0, 0),
- wkst: 0,
- bysetpos: vec![],
- bymonth: vec![],
- bymonthday: vec![],
- bynmonthday: vec![],
- byyearday: vec![],
- byweekno: vec![1],
- byweekday: vec![],
- byhour: vec![],
- byminute: vec![],
- bysecond: vec![],
- bynweekday: vec![],
- };
- let res = rebuild_year(2020, &options);
- println!("Res: {:?}", res);
- assert_eq!(2 + 2, 4);
- }
-}