diff options
author | Fredrik Meringdal <fmeringdal@hotmail.com> | 2020-10-24 13:44:15 +0200 |
---|---|---|
committer | Fredrik Meringdal <fmeringdal@hotmail.com> | 2020-10-24 13:44:15 +0200 |
commit | b38d3c0a0493ca5a1fe5e63ff5cc00ebe6277a49 (patch) | |
tree | ba0e25bbe26d50a173d8e6573b2b182478a34f92 | |
parent | 99d5b3d0fea0c3b40dc84ea18953c4f9cfae1ace (diff) | |
download | rust_rrule-b38d3c0a0493ca5a1fe5e63ff5cc00ebe6277a49.zip |
rrule string parsing works!!!!!
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/parse_options.rs | 130 | ||||
-rw-r--r-- | src/rrulestr.rs | 32 |
3 files changed, 148 insertions, 15 deletions
@@ -12,6 +12,7 @@ mod masks; mod monthinfo; mod poslist; mod rrulestr; +mod parse_options; mod yearinfo; pub mod options; diff --git a/src/parse_options.rs b/src/parse_options.rs new file mode 100644 index 0000000..e9cffc3 --- /dev/null +++ b/src/parse_options.rs @@ -0,0 +1,130 @@ +use chrono::prelude::*; +use crate::rrulestr::PartialOptions; +use crate::options::{ParsedOptions, Frequenzy}; + +// TODO: make this method on partialoptions struct +pub fn parse_options(options: &PartialOptions) -> ParsedOptions { + let mut default_partial_options = PartialOptions::new(); + default_partial_options.interval = Some(1); + default_partial_options.freq = Some(Frequenzy::YEARLY); + default_partial_options.wkst = Some(0); + + let mut partial_options = PartialOptions::concat(&default_partial_options, options); + + if partial_options.byeaster.is_some() { + partial_options.freq = Some(Frequenzy::YEARLY); + } + let freq = partial_options.freq.unwrap(); + + if partial_options.dtstart.is_none() { + partial_options.dtstart = Some(Utc::now()); + } + + if partial_options.wkst.is_none() { + partial_options.wkst = Some(0); + } + + if let Some(bysetpos) = &partial_options.bysetpos { + for pos in bysetpos { + if *pos == 0 || !(*pos >= -366 && *pos <= 366) { + panic!("bysetpos must be between 1 and 366, or between -366 and -1"); + } + } + } + + + if !( + partial_options.byweekno.is_some() || + is_some_and_not_empty(&partial_options.byweekno) || + is_some_and_not_empty(&partial_options.byyearday) || + partial_options.bymonthday.is_some() || + is_some_and_not_empty(&partial_options.bymonthday) || + partial_options.byweekday.is_some() || + partial_options.byeaster.is_some() + ) { + match &freq { + Frequenzy::YEARLY => { + if let Some(bymonth) = partial_options.bymonth { + partial_options.bymonth = Some(vec![partial_options.dtstart.unwrap().month() as usize]); + } + partial_options.bymonthday = Some(vec![partial_options.dtstart.unwrap().day() as isize]); + }, + Frequenzy::MONTHLY => { + partial_options.bymonthday = Some(vec![partial_options.dtstart.unwrap().day() as isize]); + }, + Frequenzy::WEEKLY => { + partial_options.byweekday = Some(vec![partial_options.dtstart.unwrap().weekday() as usize]); + }, + _ => unreachable!("Shouldnt be reached") + }; + } + + if partial_options.bymonthday.is_none() { + partial_options.bynmonthday = None; + } else { + let mut bymonthday = vec![]; + let mut bynmonthday = vec![]; + + for v in &partial_options.bymonthday.unwrap() { + if *v > 0 { + bymonthday.push(*v); + } else if *v < 0 { + bynmonthday.push(*v); + } + } + + partial_options.bymonthday = Some(bymonthday); + partial_options.bynmonthday = Some(bynmonthday); + } + + // byweekday / bynweekday + if partial_options.byweekday.is_some() { + partial_options.bynweekday = None; + } + + // byhour + if partial_options.byhour.is_none() && freq < Frequenzy::HOURLY { + partial_options.byhour = Some(vec![partial_options.dtstart.unwrap().hour() as usize]); + } + + // byminute + if partial_options.byminute.is_none() && freq < Frequenzy::MINUTELY { + partial_options.byminute = Some(vec![partial_options.dtstart.unwrap().minute() as usize]); +} + + + // bysecond + if partial_options.bysecond.is_none() && freq < Frequenzy::SECONDLY { + partial_options.bysecond = Some(vec![partial_options.dtstart.unwrap().second() as usize]); + } + + + ParsedOptions { + freq, + interval: partial_options.interval.unwrap(), + count: partial_options.count, + until: partial_options.until, + tzid: partial_options.tzid, + dtstart: partial_options.dtstart.unwrap(), + wkst: partial_options.wkst.unwrap(), + bysetpos: partial_options.bysetpos.unwrap_or(vec![]), + bymonth: partial_options.bymonth.unwrap_or(vec![]), + bymonthday: partial_options.bymonthday.unwrap_or(vec![]), + bynmonthday: partial_options.bynmonthday.unwrap_or(vec![]), + byyearday: partial_options.byyearday.unwrap_or(vec![]), + byweekno: partial_options.byweekno.unwrap_or(vec![]), + byweekday: partial_options.byweekday.unwrap_or(vec![]), + byhour: partial_options.byhour.unwrap_or(vec![]), + byminute: partial_options.byminute.unwrap_or(vec![]), + bysecond: partial_options.bysecond.unwrap_or(vec![]), + bynweekday: partial_options.bynweekday.unwrap_or(vec![]), + byeaster: partial_options.byeaster, + } +} + +fn is_some_and_not_empty<T>(v: &Option<Vec<T>>) -> bool { + match v { + Some(v) => !v.is_empty(), + None => false + } +}
\ No newline at end of file diff --git a/src/rrulestr.rs b/src/rrulestr.rs index 607513f..71cbfe1 100644 --- a/src/rrulestr.rs +++ b/src/rrulestr.rs @@ -1,5 +1,5 @@ use crate::options::*; -use crate::rrule::RRule; +use crate::parse_options::parse_options; use crate::rruleset::RRuleSet; use chrono::prelude::*; use chrono::DateTime; @@ -371,11 +371,9 @@ fn parse_input(s: &str) -> ParsedInput { } = parse_dtstart(s).unwrap(); - let mut lines: Vec<&str> = s.split("\n").collect(); + let lines: Vec<&str> = s.split("\n").collect(); for line in &lines { let parsed_line = break_down_line(line); - println!("Parsed line: {:?}", parsed_line); - match parsed_line.name.to_uppercase().as_str() { "RRULE" => { if !parsed_line.params.is_empty() { @@ -410,16 +408,6 @@ fn parse_input(s: &str) -> ParsedInput { return ParsedInput { dtstart, tzid, - // fn it_works_2() { - // let options = build_rule("DTSTART:19970902T090000Z\nRRULE:FREQ=YEARLY;COUNT=3\n"); - // println!("?????????????=================?????????????"); - // println!("{:?}", options); - // } - // fn it_works_2() { - // let options = build_rule("DTSTART:19970902T090000Z\nRRULE:FREQ=YEARLY;COUNT=3\n"); - // println!("?????????????=================?????????????"); - // println!("{:?}", options); - // } rrule_vals, rdate_vals, exrule_vals, @@ -515,10 +503,24 @@ mod test { // println!("{:?}", options); // } + // #[test] + // fn it_works() { + // let options = build_rule("RRULE:UNTIL=19990404T110000Z;DTSTART;TZID=America/New_York:19990104T110000Z;FREQ=WEEKLY;BYDAY=TU,WE"); + // let parsed_opts = parse_options(&options); + // println!("?????????????=================?????????????"); + // println!("{:?}", options); + // } + #[test] fn it_works() { - let options = build_rule("RRULE:UNTIL=19990404T110000Z;DTSTART;TZID=America/New_York:19990104T110000Z;FREQ=WEEKLY;BYDAY=TU,WE"); + let options = parse_string("RRULE:UNTIL=19990404T110000Z;DTSTART;TZID=America/New_York:19990104T110000Z;FREQ=WEEKLY;BYDAY=TU,WE"); + let parsed_opts = parse_options(&options); println!("?????????????=================?????????????"); println!("{:?}", options); + println!("?????????????=== PARSED ==============?????????????"); + println!("{:?}", parsed_opts); + let all = crate::rrule::RRule::new(parsed_opts).all(); + println!("------------------ alll ----------------"); + println!("{:?}", all); } } |