From ca355e4958fa547c92c851835fa8edf7ba113ff9 Mon Sep 17 00:00:00 2001 From: Fredrik Meringdal Date: Sun, 25 Oct 2020 12:15:14 +0100 Subject: restructuring --- Cargo.toml | 2 +- src/lib.rs | 11 +++--- src/options.rs | 94 ++++++++++++++++++++++++++++++++++++++++++++-------- src/parse_options.rs | 3 +- src/rrulestr.rs | 78 ------------------------------------------- src/yearinfo.rs | 15 +++++++++ tests/rrule.rs | 1 - 7 files changed, 102 insertions(+), 102 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e2e20e5..c00d167 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "rrule" description = "A pure Rust (partial) implementation of recurrence rules as defined in the iCalendar RFC." license = "MIT" -version = "0.2.1" +version = "0.2.2" authors = ["Fredrik Meringdal"] edition = "2018" diff --git a/src/lib.rs b/src/lib.rs index f56120c..61a5d00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,15 +11,14 @@ mod iterinfo; mod masks; mod monthinfo; mod poslist; -mod rrulestr; mod parse_options; mod yearinfo; mod options; +mod rrulestr; mod rrule; mod rruleset; -pub use rrulestr::build_rule; -pub use rrule::RRule; -pub use rruleset::RRuleSet; -pub use options::{Frequenzy, ParsedOptions}; -pub use rrulestr::PartialOptions; \ No newline at end of file +pub use crate::rrule::RRule; +pub use crate::rruleset::RRuleSet; +pub use crate::rrulestr::build_rule; +pub use crate::options::{Frequenzy, ParsedOptions, PartialOptions}; \ No newline at end of file diff --git a/src/options.rs b/src/options.rs index a6736a8..e29d8fe 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,19 +1,5 @@ use chrono::prelude::*; -#[derive(Debug)] -pub struct YearInfo { - pub yearlen: usize, - pub nextyearlen: usize, - pub yearordinal: isize, - pub yearweekday: usize, - pub mmask: Vec, - pub mrange: Vec, - pub mdaymask: Vec, - pub nmdaymask: Vec, - pub wdaymask: Vec, - pub wnomask: Option>, -} - #[derive(Debug, PartialEq, PartialOrd, Clone)] pub enum Frequenzy { Yearly = 0, @@ -46,4 +32,84 @@ pub struct ParsedOptions { pub bysecond: Vec, pub bynweekday: Vec>, pub byeaster: Option, +} + +#[derive(Debug, Clone)] +pub struct PartialOptions { + pub freq: Option, + pub interval: Option, + pub count: Option, + pub until: Option>, + pub tzid: Option, + pub dtstart: Option>, + pub wkst: Option, + pub bysetpos: Option>, + pub bymonth: Option>, + pub bymonthday: Option>, + pub bynmonthday: Option>, + pub byyearday: Option>, + pub byweekno: Option>, + pub byweekday: Option>, + pub byhour: Option>, + pub byminute: Option>, + pub bysecond: Option>, + pub bynweekday: Option>>, + pub byeaster: Option, +} + +impl PartialOptions { + pub fn new() -> Self { + Self { + freq: None, + interval: None, + count: None, + until: None, + tzid: None, + dtstart: None, + wkst: None, + bysetpos: None, + bymonth: None, + bymonthday: None, + bynmonthday: None, + byyearday: None, + byweekno: None, + byweekday: None, + byhour: None, + byminute: None, + bysecond: None, + bynweekday: None, + byeaster: None, + } + } + + fn is_some_or_none<'a, T>(prop1: &'a Option, prop2: &'a Option) -> &'a Option { + if prop2.is_some() { + return prop2; + } + prop1 + } + + pub fn concat(opt1: &Self, opt2: &Self) -> Self { + Self { + freq: Self::is_some_or_none(&opt1.freq, &opt2.freq).clone(), + interval: Self::is_some_or_none(&opt1.interval, &opt2.interval).clone(), + count: Self::is_some_or_none(&opt1.count, &opt2.count).clone(), + until: Self::is_some_or_none(&opt1.until, &opt2.until).clone(), + tzid: Self::is_some_or_none(&opt1.tzid, &opt2.tzid).clone(), + dtstart: Self::is_some_or_none(&opt1.dtstart, &opt2.dtstart).clone(), + wkst: Self::is_some_or_none(&opt1.wkst, &opt2.wkst).clone(), + bysetpos: Self::is_some_or_none(&opt1.bysetpos, &opt2.bysetpos).clone(), + bymonth: Self::is_some_or_none(&opt1.bymonth, &opt2.bymonth).clone(), + bymonthday: Self::is_some_or_none(&opt1.bymonthday, &opt2.bymonthday).clone(), + bynmonthday: Self::is_some_or_none(&opt1.bynmonthday, &opt2.bynmonthday).clone(), + byyearday: Self::is_some_or_none(&opt1.byyearday, &opt2.byyearday).clone(), + byweekno: Self::is_some_or_none(&opt1.byweekno, &opt2.byweekno).clone(), + byweekday: Self::is_some_or_none(&opt1.byweekday, &opt2.byweekday).clone(), + byhour: Self::is_some_or_none(&opt1.byhour, &opt2.byhour).clone(), + byminute: Self::is_some_or_none(&opt1.byminute, &opt2.byminute).clone(), + bysecond: Self::is_some_or_none(&opt1.bysecond, &opt2.bysecond).clone(), + bynweekday: Self::is_some_or_none(&opt1.bynweekday, &opt2.bynweekday).clone(), + byeaster: Self::is_some_or_none(&opt1.byeaster, &opt2.byeaster).clone(), + } + } } \ No newline at end of file diff --git a/src/parse_options.rs b/src/parse_options.rs index 86c3f94..4071cbc 100644 --- a/src/parse_options.rs +++ b/src/parse_options.rs @@ -1,6 +1,5 @@ use chrono::prelude::*; -use crate::rrulestr::PartialOptions; -use crate::options::{ParsedOptions, Frequenzy}; +use crate::options::{ParsedOptions, Frequenzy, PartialOptions}; // TODO: make this method on partialoptions struct pub fn parse_options(options: &PartialOptions) -> ParsedOptions { diff --git a/src/rrulestr.rs b/src/rrulestr.rs index 98db39a..c1bfc67 100644 --- a/src/rrulestr.rs +++ b/src/rrulestr.rs @@ -15,85 +15,7 @@ static DTSTART_RE: Lazy = static RRUle_RE: Lazy = Lazy::new(|| Regex::new(r"(?m)^(?:RRULE|EXRULE):").unwrap()); -#[derive(Debug, Clone)] -pub struct PartialOptions { - pub freq: Option, - pub interval: Option, - pub count: Option, - pub until: Option>, - pub tzid: Option, - pub dtstart: Option>, - pub wkst: Option, - pub bysetpos: Option>, - pub bymonth: Option>, - pub bymonthday: Option>, - pub bynmonthday: Option>, - pub byyearday: Option>, - pub byweekno: Option>, - pub byweekday: Option>, - pub byhour: Option>, - pub byminute: Option>, - pub bysecond: Option>, - pub bynweekday: Option>>, - pub byeaster: Option, -} -impl PartialOptions { - pub fn new() -> Self { - Self { - freq: None, - interval: None, - count: None, - until: None, - tzid: None, - dtstart: None, - wkst: None, - bysetpos: None, - bymonth: None, - bymonthday: None, - bynmonthday: None, - byyearday: None, - byweekno: None, - byweekday: None, - byhour: None, - byminute: None, - bysecond: None, - bynweekday: None, - byeaster: None, - } - } - - fn is_some_or_none<'a, T>(prop1: &'a Option, prop2: &'a Option) -> &'a Option { - if prop2.is_some() { - return prop2; - } - prop1 - } - - pub fn concat(opt1: &Self, opt2: &Self) -> Self { - Self { - freq: Self::is_some_or_none(&opt1.freq, &opt2.freq).clone(), - interval: Self::is_some_or_none(&opt1.interval, &opt2.interval).clone(), - count: Self::is_some_or_none(&opt1.count, &opt2.count).clone(), - until: Self::is_some_or_none(&opt1.until, &opt2.until).clone(), - tzid: Self::is_some_or_none(&opt1.tzid, &opt2.tzid).clone(), - dtstart: Self::is_some_or_none(&opt1.dtstart, &opt2.dtstart).clone(), - wkst: Self::is_some_or_none(&opt1.wkst, &opt2.wkst).clone(), - bysetpos: Self::is_some_or_none(&opt1.bysetpos, &opt2.bysetpos).clone(), - bymonth: Self::is_some_or_none(&opt1.bymonth, &opt2.bymonth).clone(), - bymonthday: Self::is_some_or_none(&opt1.bymonthday, &opt2.bymonthday).clone(), - bynmonthday: Self::is_some_or_none(&opt1.bynmonthday, &opt2.bynmonthday).clone(), - byyearday: Self::is_some_or_none(&opt1.byyearday, &opt2.byyearday).clone(), - byweekno: Self::is_some_or_none(&opt1.byweekno, &opt2.byweekno).clone(), - byweekday: Self::is_some_or_none(&opt1.byweekday, &opt2.byweekday).clone(), - byhour: Self::is_some_or_none(&opt1.byhour, &opt2.byhour).clone(), - byminute: Self::is_some_or_none(&opt1.byminute, &opt2.byminute).clone(), - bysecond: Self::is_some_or_none(&opt1.bysecond, &opt2.bysecond).clone(), - bynweekday: Self::is_some_or_none(&opt1.bynweekday, &opt2.bynweekday).clone(), - byeaster: Self::is_some_or_none(&opt1.byeaster, &opt2.byeaster).clone(), - } - } -} fn datestring_to_date(dt: &str) -> DateTime { let bits = DATESTR_RE.captures(dt).unwrap(); diff --git a/src/yearinfo.rs b/src/yearinfo.rs index 8d7fd4b..9bc0906 100644 --- a/src/yearinfo.rs +++ b/src/yearinfo.rs @@ -3,6 +3,21 @@ use crate::options::*; use chrono::prelude::*; use chrono::DateTime; + +#[derive(Debug)] +pub struct YearInfo { + pub yearlen: usize, + pub nextyearlen: usize, + pub yearordinal: isize, + pub yearweekday: usize, + pub mmask: Vec, + pub mrange: Vec, + pub mdaymask: Vec, + pub nmdaymask: Vec, + pub wdaymask: Vec, + pub wnomask: Option>, +} + fn is_leap_year(year: i32) -> bool { year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) } diff --git a/tests/rrule.rs b/tests/rrule.rs index e23b270..6c5ffa3 100644 --- a/tests/rrule.rs +++ b/tests/rrule.rs @@ -4,7 +4,6 @@ extern crate rrule; use chrono::prelude::*; use chrono_tz::UTC; -use rrule::options::*; use rrule::{RRule, ParsedOptions, Frequenzy}; #[cfg(test)] -- cgit v1.2.3