Skip to content

Commit

Permalink
removed lifetimes and added from_str example
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbanWS committed May 23, 2024
1 parent dee6785 commit be917c4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
10 changes: 10 additions & 0 deletions rrule/examples/manual_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ fn main() {

let iter = rrule.into_iter();

// Or:
//
// let iter: RRuleSetIter = "DTSTART;TZID=America/New_York:20200902T130000\n\
// RRULE:FREQ=Weekly"
// .parse()
// .expect("The RRule is not valid");
//


for next in iter.take(50) {
if next.year() == 2021 {
println!("These are all the weeks before 2021.");
break;
}
println!("Date: {}", next.to_rfc3339());
}

}
16 changes: 8 additions & 8 deletions rrule/src/iter/iterinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ use crate::{Frequency, NWeekday, RRule};
use chrono::{Datelike, NaiveTime, TimeZone};

#[derive(Debug, Clone)]
pub(crate) struct IterInfo<'a> {
pub(crate) struct IterInfo {
year_info: YearInfo,
month_info: Option<MonthInfo>,
easter_mask: Option<Vec<i32>>,
rrule: &'a RRule,
rrule: RRule,
}

impl<'a> IterInfo<'a> {
pub fn new(rrule: &'a RRule, dt_start: &DateTime) -> Self {
impl IterInfo {
pub fn new(rrule: &RRule, dt_start: &DateTime) -> Self {
let year = dt_start.year();
let month = get_month(dt_start);

let year_info = YearInfo::new(year, rrule);
let mut ii = Self {
rrule,
rrule: rrule.clone(),
year_info,
month_info: None,
easter_mask: None,
Expand All @@ -35,7 +35,7 @@ impl<'a> IterInfo<'a> {
if !skip_year_info
&& !matches!(&self.month_info, Some(month_info) if month_info.last_year == year)
{
self.year_info = YearInfo::new(year, self.rrule);
self.year_info = YearInfo::new(year, &self.rrule);
}

let contains_nth_by_weekday = self
Expand All @@ -47,7 +47,7 @@ impl<'a> IterInfo<'a> {
if contains_nth_by_weekday
&& !(matches!(&self.month_info, Some(month_info) if month_info.last_month == month && month_info.last_year == year))
{
let new_month_info = MonthInfo::new(&self.year_info, month, self.rrule);
let new_month_info = MonthInfo::new(&self.year_info, month, &self.rrule);
self.month_info = Some(new_month_info);
}

Expand Down Expand Up @@ -269,6 +269,6 @@ impl<'a> IterInfo<'a> {
}

pub fn rrule(&self) -> &RRule {
self.rrule
&self.rrule
}
}
12 changes: 6 additions & 6 deletions rrule/src/iter/rrule_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use chrono::{NaiveTime, TimeZone};
use std::collections::VecDeque;

#[derive(Debug, Clone)]
pub(crate) struct RRuleIter<'a> {
pub(crate) struct RRuleIter {
/// Date the iterator is currently at.
pub(crate) counter_date: DateTimeIter,
pub(crate) ii: IterInfo<'a>,
pub(crate) ii: IterInfo,
pub(crate) timeset: Vec<NaiveTime>,
pub(crate) dt_start: DateTime,
/// Buffer of datetimes is not yet yielded
Expand All @@ -28,8 +28,8 @@ pub(crate) struct RRuleIter<'a> {
pub(crate) was_limited: bool,
}

impl<'a> RRuleIter<'a> {
pub(crate) fn new(rrule: &'a RRule, dt_start: &DateTime, limited: bool) -> Self {
impl RRuleIter {
pub(crate) fn new(rrule: &RRule, dt_start: &DateTime, limited: bool) -> Self {
let ii = IterInfo::new(rrule, dt_start);

let hour = get_hour(dt_start);
Expand Down Expand Up @@ -204,7 +204,7 @@ impl<'a> RRuleIter<'a> {
}
}

impl<'a> Iterator for RRuleIter<'a> {
impl Iterator for RRuleIter {
type Item = DateTime;

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -229,7 +229,7 @@ pub(crate) trait WasLimited {
fn was_limited(&self) -> bool;
}

impl<'a> WasLimited for RRuleIter<'a> {
impl WasLimited for RRuleIter {
fn was_limited(&self) -> bool {
self.was_limited
}
Expand Down
26 changes: 18 additions & 8 deletions rrule/src/iter/rruleset_iter.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use super::rrule_iter::WasLimited;
use super::{rrule_iter::RRuleIter, MAX_ITER_LOOP};
use crate::RRuleError;
use crate::{core::DateTime, RRuleSet};
use std::collections::BTreeSet;
use std::str::FromStr;
use std::{collections::HashMap, iter::Iterator};

#[derive(Debug, Clone)]
/// Iterator over all the dates in an [`RRuleSet`].
pub struct RRuleSetIter<'a> {
pub struct RRuleSetIter {
queue: HashMap<usize, DateTime>,
limited: bool,
rrule_iters: Vec<RRuleIter<'a>>,
exrules: Vec<RRuleIter<'a>>,
rrule_iters: Vec<RRuleIter>,
exrules: Vec<RRuleIter>,
exdates: BTreeSet<i64>,
/// Sorted additional dates in descending order
rdates: Vec<DateTime>,
was_limited: bool,
}

impl<'a> RRuleSetIter<'a> {
impl RRuleSetIter {
fn generate_date(
dates: &mut Vec<DateTime>,
exrules: &mut [RRuleIter],
Expand Down Expand Up @@ -104,7 +106,7 @@ impl<'a> RRuleSetIter<'a> {
}
}

impl<'a> Iterator for RRuleSetIter<'a> {
impl Iterator for RRuleSetIter {
type Item = DateTime;

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -191,10 +193,10 @@ impl<'a> Iterator for RRuleSetIter<'a> {
}
}

impl<'a> IntoIterator for &'a RRuleSet {
impl IntoIterator for &RRuleSet {
type Item = DateTime;

type IntoIter = RRuleSetIter<'a>;
type IntoIter = RRuleSetIter;

fn into_iter(self) -> Self::IntoIter {
// Sort in decreasing order
Expand Down Expand Up @@ -224,8 +226,16 @@ impl<'a> IntoIterator for &'a RRuleSet {
}
}

impl<'a> WasLimited for RRuleSetIter<'a> {
impl WasLimited for RRuleSetIter {
fn was_limited(&self) -> bool {
self.was_limited
}
}

impl FromStr for RRuleSetIter {
type Err = RRuleError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(RRuleSet::from_str(s)?.into_iter())
}
}

0 comments on commit be917c4

Please sign in to comment.