diff options
Diffstat (limited to 'src/parse_options.rs')
-rw-r--r-- | src/parse_options.rs | 130 |
1 files changed, 130 insertions, 0 deletions
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 |