diff options
author | Fredrik Meringdal <fmeringdal@hotmail.com> | 2020-10-25 01:33:33 +0200 |
---|---|---|
committer | Fredrik Meringdal <fmeringdal@hotmail.com> | 2020-10-25 01:33:33 +0200 |
commit | 6acba0e4ee286a6412261a89ad15db01717ac8bc (patch) | |
tree | 98a553ad25a2a634112a0b146593e7099582df4c /src | |
parent | ea7f196d627868a7cde403e35421a1f085d84b92 (diff) | |
download | rust_rrule-6acba0e4ee286a6412261a89ad15db01717ac8bc.zip |
small refactor
Diffstat (limited to 'src')
-rw-r--r-- | src/iter.rs | 36 | ||||
-rw-r--r-- | src/iter_set.rs | 6 | ||||
-rw-r--r-- | src/iterinfo.rs | 14 | ||||
-rw-r--r-- | src/monthinfo.rs | 4 | ||||
-rw-r--r-- | src/options.rs | 14 | ||||
-rw-r--r-- | src/parse_options.rs | 16 | ||||
-rw-r--r-- | src/rrule.rs | 35 | ||||
-rw-r--r-- | src/rruleset.rs | 52 | ||||
-rw-r--r-- | src/rrulestr.rs | 40 |
9 files changed, 145 insertions, 72 deletions
diff --git a/src/iter.rs b/src/iter.rs index 5a3da4d..72c0c0a 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -103,10 +103,10 @@ pub fn increment_counter_date( filtered: bool, ) -> DateTime<Utc> { match options.freq { - Frequenzy::YEARLY => counter_date + Frequenzy::Yearly => counter_date .with_year(counter_date.year() + options.interval as i32) .unwrap(), - Frequenzy::MONTHLY => { + Frequenzy::Monthly => { let new_month = counter_date.month() + options.interval as u32; if new_month > 12 { let mut year_div = new_month / 12; @@ -125,7 +125,7 @@ pub fn increment_counter_date( counter_date.with_month(new_month).unwrap() } } - Frequenzy::WEEKLY => { + Frequenzy::Weekly => { let mut day_delta = 0; let weekday = get_weekday_val(&counter_date.weekday()); if options.wkst > weekday { @@ -136,8 +136,8 @@ pub fn increment_counter_date( } counter_date + Duration::days(day_delta as i64) } - Frequenzy::DAILY => counter_date + Duration::days(options.interval as i64), - Frequenzy::HOURLY => { + Frequenzy::Daily => counter_date + Duration::days(options.interval as i64), + Frequenzy::Hourly => { let mut new_hours = counter_date.hour() as usize; if filtered { new_hours += ((23 - new_hours) as f32 / options.interval as f32).floor() as usize @@ -157,8 +157,8 @@ pub fn increment_counter_date( } counter_date.with_hour(0).unwrap() + Duration::hours(new_hours as i64) } - Frequenzy::MINUTELY => counter_date + Duration::minutes(options.interval as i64), - Frequenzy::SECONDLY => counter_date + Duration::seconds(options.interval as i64), + Frequenzy::Minutely => counter_date + Duration::minutes(options.interval as i64), + Frequenzy::Secondly => counter_date + Duration::seconds(options.interval as i64), } } @@ -232,10 +232,10 @@ pub fn remove_filtered_days( pub fn build_timeset(options: &ParsedOptions) -> Vec<Time> { let millisecond_mod = (options.dtstart.timestamp_millis() & 1000) as usize; - if !(options.freq == Frequenzy::DAILY - || options.freq == Frequenzy::MONTHLY - || options.freq == Frequenzy::WEEKLY - || options.freq == Frequenzy::YEARLY) + if !(options.freq == Frequenzy::Daily + || options.freq == Frequenzy::Monthly + || options.freq == Frequenzy::Weekly + || options.freq == Frequenzy::Yearly) { return vec![]; } @@ -257,27 +257,27 @@ pub fn make_timeset( counter_date: &DateTime<Utc>, options: &ParsedOptions, ) -> Vec<Time> { - if options.freq == Frequenzy::DAILY - || options.freq == Frequenzy::MONTHLY - || options.freq == Frequenzy::WEEKLY - || options.freq == Frequenzy::YEARLY + if options.freq == Frequenzy::Daily + || options.freq == Frequenzy::Monthly + || options.freq == Frequenzy::Weekly + || options.freq == Frequenzy::Yearly { return build_timeset(options); } - if (options.freq >= Frequenzy::HOURLY + if (options.freq >= Frequenzy::Hourly && !options.byhour.is_empty() && !options .byhour .iter() .any(|&h| h == counter_date.hour() as usize)) - || (options.freq >= Frequenzy::MINUTELY + || (options.freq >= Frequenzy::Minutely && !options.byminute.is_empty() && !options .byminute .iter() .any(|&m| m == counter_date.minute() as usize)) - || (options.freq >= Frequenzy::SECONDLY + || (options.freq >= Frequenzy::Secondly && !options.bysecond.is_empty() && !options .bysecond diff --git a/src/iter_set.rs b/src/iter_set.rs index 077154b..e3c37db 100644 --- a/src/iter_set.rs +++ b/src/iter_set.rs @@ -119,9 +119,9 @@ pub fn iter_v2<T: IterResult>( return iter_result.get_value(); } - if options.freq == Frequenzy::HOURLY - || options.freq == Frequenzy::MINUTELY - || options.freq == Frequenzy::SECONDLY + if options.freq == Frequenzy::Hourly + || options.freq == Frequenzy::Minutely + || options.freq == Frequenzy::Secondly { timeset = ii.gettimeset( &options.freq, diff --git a/src/iterinfo.rs b/src/iterinfo.rs index c0285f6..6f3044e 100644 --- a/src/iterinfo.rs +++ b/src/iterinfo.rs @@ -229,10 +229,10 @@ impl<'a> IterInfo<'a> { day: usize, ) -> (Vec<usize>, usize, usize) { match freq { - Frequenzy::YEARLY => self.ydayset(), - Frequenzy::MONTHLY => self.mdayset(month), - Frequenzy::WEEKLY => self.wdayset(year, month, day), - Frequenzy::DAILY => self.ddayset(year, month, day), + Frequenzy::Yearly => self.ydayset(), + Frequenzy::Monthly => self.mdayset(month), + Frequenzy::Weekly => self.wdayset(year, month, day), + Frequenzy::Daily => self.ddayset(year, month, day), _ => self.ddayset(year, month, day), } } @@ -246,9 +246,9 @@ impl<'a> IterInfo<'a> { millisecond: usize, ) -> Vec<Time> { match freq { - Frequenzy::HOURLY => self.htimeset(hour, minute, second, millisecond), - Frequenzy::MINUTELY => self.mtimeset(hour, minute, second, millisecond), - Frequenzy::SECONDLY => self.stimeset(hour, minute, second, millisecond), + Frequenzy::Hourly => self.htimeset(hour, minute, second, millisecond), + Frequenzy::Minutely => self.mtimeset(hour, minute, second, millisecond), + Frequenzy::Secondly => self.stimeset(hour, minute, second, millisecond), _ => panic!("Invalid freq"), } } diff --git a/src/monthinfo.rs b/src/monthinfo.rs index 8ccda94..c0af98a 100644 --- a/src/monthinfo.rs +++ b/src/monthinfo.rs @@ -23,7 +23,7 @@ pub fn rebuild_month( }; let mut ranges: Vec<(isize, isize)> = vec![]; - if options.freq == Frequenzy::YEARLY { + if options.freq == Frequenzy::Yearly { if options.bymonth.is_empty() { ranges = vec![(0, yearlen as isize)]; } else { @@ -32,7 +32,7 @@ pub fn rebuild_month( ranges.push((mrange[m - 1] as isize, mrange[m] as isize)) } } - } else if options.freq == Frequenzy::MONTHLY { + } else if options.freq == Frequenzy::Monthly { ranges.push((mrange[month - 1] as isize, mrange[month] as isize)); } diff --git a/src/options.rs b/src/options.rs index 67c779e..a6736a8 100644 --- a/src/options.rs +++ b/src/options.rs @@ -16,13 +16,13 @@ pub struct YearInfo { #[derive(Debug, PartialEq, PartialOrd, Clone)] pub enum Frequenzy { - YEARLY = 0, - MONTHLY = 1, - WEEKLY = 2, - DAILY = 3, - HOURLY = 4, - MINUTELY = 5, - SECONDLY = 6, + Yearly = 0, + Monthly = 1, + Weekly = 2, + Daily = 3, + Hourly = 4, + Minutely = 5, + Secondly = 6, } #[derive(Debug, Clone)] diff --git a/src/parse_options.rs b/src/parse_options.rs index e9cffc3..86c3f94 100644 --- a/src/parse_options.rs +++ b/src/parse_options.rs @@ -6,13 +6,13 @@ use crate::options::{ParsedOptions, Frequenzy}; 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.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); + partial_options.freq = Some(Frequenzy::Yearly); } let freq = partial_options.freq.unwrap(); @@ -43,16 +43,16 @@ pub fn parse_options(options: &PartialOptions) -> ParsedOptions { partial_options.byeaster.is_some() ) { match &freq { - Frequenzy::YEARLY => { + 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 => { + Frequenzy::Monthly => { partial_options.bymonthday = Some(vec![partial_options.dtstart.unwrap().day() as isize]); }, - Frequenzy::WEEKLY => { + Frequenzy::Weekly => { partial_options.byweekday = Some(vec![partial_options.dtstart.unwrap().weekday() as usize]); }, _ => unreachable!("Shouldnt be reached") @@ -83,18 +83,18 @@ pub fn parse_options(options: &PartialOptions) -> ParsedOptions { } // byhour - if partial_options.byhour.is_none() && freq < Frequenzy::HOURLY { + 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 { + 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 { + if partial_options.bysecond.is_none() && freq < Frequenzy::Secondly { partial_options.bysecond = Some(vec![partial_options.dtstart.unwrap().second() as usize]); } diff --git a/src/rrule.rs b/src/rrule.rs index 4b4e6c5..b7f53ea 100644 --- a/src/rrule.rs +++ b/src/rrule.rs @@ -4,24 +4,45 @@ use crate::options::*; use chrono::prelude::*; use chrono_tz::{Tz, UTC}; + +/// A type that produces instances of a given a RFC1241 string representation. +/// +/// The first element is traditionally the path of the executable, but it can be +/// set to arbitrary text, and may not even exist. This means this property should +/// not be relied upon for security purposes. +/// +/// On Unix systems shell usually expands unquoted arguments with glob patterns +/// (such as `*` and `?`). On Windows this is not done, and such arguments are +/// passed as-is. +/// +/// # Panics +/// +/// The returned iterator will panic during iteration if any argument to the +/// process is not valid unicode. If this is not desired, +/// use the [`args_os`] function instead. +/// +/// # Examples +/// +/// ``` +/// use std::env; +/// +/// // Prints each argument on a separate line +/// for argument in env::args() { +/// println!("{}", argument); +/// } +/// ``` #[derive(Clone, Debug)] pub struct RRule { - cache: bool, pub options: ParsedOptions, } impl RRule { pub fn new(options: ParsedOptions) -> Self { Self { - options, - cache: true, + options } } - pub fn disable_cache(&mut self) { - self.cache = false; - } - pub fn all(&mut self) -> Vec<DateTime<Tz>> { let iter_args = IterArgs { inc: true, diff --git a/src/rruleset.rs b/src/rruleset.rs index 29093ae..c6a4356 100644 --- a/src/rruleset.rs +++ b/src/rruleset.rs @@ -6,6 +6,32 @@ use chrono::prelude::*; use chrono_tz::{Tz, UTC}; use std::collections::HashMap; +/// A type that produces instances of a given a RFC1241 string representation. +/// +/// The first element is traditionally the path of the executable, but it can be +/// set to arbitrary text, and may not even exist. This means this property should +/// not be relied upon for security purposes. +/// +/// On Unix systems shell usually expands unquoted arguments with glob patterns +/// (such as `*` and `?`). On Windows this is not done, and such arguments are +/// passed as-is. +/// +/// # Panics +/// +/// The returned iterator will panic during iteration if any argument to the +/// process is not valid unicode. If this is not desired, +/// use the [`args_os`] function instead. +/// +/// # Examples +/// +/// ``` +/// use std::env; +/// +/// // Prints each argument on a separate line +/// for argument in env::args() { +/// println!("{}", argument); +/// } +/// ``` #[derive(Debug)] pub struct RRuleSet { rrule: Vec<RRule>, @@ -315,7 +341,7 @@ mod test_iter_set { let mut set = RRuleSet::new(); let options1 = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: Some(6), bymonth: vec![], dtstart: Utc.ymd(1997, 9, 2).and_hms(9, 0, 0), @@ -338,7 +364,7 @@ mod test_iter_set { let rrule = RRule::new(options1); set.rrule(rrule); let options2 = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: Some(3), bymonth: vec![], dtstart: Utc.ymd(1997, 9, 2).and_hms(9, 0, 0), @@ -408,7 +434,7 @@ mod test_iter_set { set.rdate(ymd_hms(1997, 9, 18, 9, 0, 0)); let options = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: Some(3), bymonth: vec![], dtstart: Utc.ymd(1997, 9, 2).and_hms(9, 0, 0), @@ -446,7 +472,7 @@ mod test_iter_set { let mut set = RRuleSet::new(); let options = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: Some(6), bymonth: vec![], dtstart: Utc.ymd(1997, 9, 2).and_hms(9, 0, 0), @@ -488,7 +514,7 @@ mod test_iter_set { let mut set = RRuleSet::new(); let options = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: Some(13), bymonth: vec![9], dtstart: Utc.ymd(1997, 9, 2).and_hms(9, 0, 0), @@ -512,7 +538,7 @@ mod test_iter_set { set.rrule(rrule); let options = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: Some(10), bymonth: vec![9], dtstart: Utc.ymd(1997, 9, 2).and_hms(9, 0, 0), @@ -550,7 +576,7 @@ mod test_iter_set { let mut set = RRuleSet::new(); let options = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: None, bymonth: vec![9], dtstart: Utc.ymd(1997, 9, 2).and_hms(9, 0, 0), @@ -574,7 +600,7 @@ mod test_iter_set { set.rrule(rrule); let options = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: Some(10), bymonth: vec![9], dtstart: Utc.ymd(1997, 9, 2).and_hms(9, 0, 0), @@ -608,7 +634,7 @@ mod test_iter_set { let mut set = RRuleSet::new(); let options = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: None, bymonth: vec![9], dtstart: Utc.ymd(1997, 9, 2).and_hms(9, 0, 0), @@ -632,7 +658,7 @@ mod test_iter_set { set.rrule(rrule); let options = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: Some(10), bymonth: vec![9], dtstart: Utc.ymd(1997, 9, 2).and_hms(9, 0, 0), @@ -666,7 +692,7 @@ mod test_iter_set { let mut set = RRuleSet::new(); let options = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: None, bymonth: vec![9], dtstart: Utc.ymd(1997, 9, 2).and_hms(9, 0, 0), @@ -690,7 +716,7 @@ mod test_iter_set { set.rrule(rrule); let options = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: Some(10), bymonth: vec![9], dtstart: Utc.ymd(1997, 9, 2).and_hms(9, 0, 0), @@ -732,7 +758,7 @@ mod test_iter_set { let mut set = RRuleSet::new(); let options = ParsedOptions { - freq: Frequenzy::YEARLY, + freq: Frequenzy::Yearly, count: Some(2), bymonth: vec![1], dtstart: Utc.ymd(1960, 1, 1).and_hms(9, 0, 0), diff --git a/src/rrulestr.rs b/src/rrulestr.rs index 5df7fd7..996749d 100644 --- a/src/rrulestr.rs +++ b/src/rrulestr.rs @@ -132,13 +132,13 @@ fn parse_dtstart(s: &str) -> Option<PartialOptions> { fn from_str_to_freq(s: &str) -> Option<Frequenzy> { match s.to_uppercase().as_str() { - "YEARLY" => Some(Frequenzy::YEARLY), - "MONTHLY" => Some(Frequenzy::MONTHLY), - "WEEKLY" => Some(Frequenzy::WEEKLY), - "DAILY" => Some(Frequenzy::DAILY), - "HOURLY" => Some(Frequenzy::HOURLY), - "MINUTELY" => Some(Frequenzy::MINUTELY), - "SECONDLY" => Some(Frequenzy::SECONDLY), + "YEARLY" => Some(Frequenzy::Yearly), + "MONTHLY" => Some(Frequenzy::Monthly), + "WEEKLY" => Some(Frequenzy::Weekly), + "DAILY" => Some(Frequenzy::Daily), + "HOURLY" => Some(Frequenzy::Hourly), + "MINUTELY" => Some(Frequenzy::Minutely), + "SECONDLY" => Some(Frequenzy::Secondly), _ => None, } } @@ -433,6 +433,32 @@ fn parse_rdate(rdateval: &str, params: Vec<String>) -> Vec<DateTime<Utc>> { rdateval.split(",").map(|datestr| datestring_to_date(datestr)).collect() } +/// A type that produces instances of a given a RFC1241 string representation. +/// +/// The first element is traditionally the path of the executable, but it can be +/// set to arbitrary text, and may not even exist. This means this property should +/// not be relied upon for security purposes. +/// +/// On Unix systems shell usually expands unquoted arguments with glob patterns +/// (such as `*` and `?`). On Windows this is not done, and such arguments are +/// passed as-is. +/// +/// # Panics +/// +/// The returned iterator will panic during iteration if any argument to the +/// process is not valid unicode. If this is not desired, +/// use the [`args_os`] function instead. +/// +/// # Examples +/// +/// ``` +/// use std::env; +/// +/// // Prints each argument on a separate line +/// for argument in env::args() { +/// println!("{}", argument); +/// } +/// ``` pub fn build_rule(s: &str) -> RRuleSet { let ParsedInput { mut rrule_vals, |