Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ScheduleIteratorBuf #104

Merged
merged 3 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ mod specifier;
mod queries;
mod parsing;

pub use crate::schedule::{Schedule, ScheduleIterator};
pub use crate::schedule::{Schedule, ScheduleIterator, OwnedScheduleIterator};
pub use crate::time_unit::TimeUnitSpec;
113 changes: 93 additions & 20 deletions src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ impl From<Schedule> for String {
}
}

/// Representation of a cron expression
#[derive(Clone, Debug, Eq)]
pub struct Schedule {
source: String,
Expand Down Expand Up @@ -227,6 +226,11 @@ impl Schedule {
self.after(&timezone.from_utc_datetime(&Utc::now().naive_utc()))
}

/// The same, but with an iterator with a static ownership
pub fn upcoming_owned<Z: TimeZone>(&self, timezone: Z) -> OwnedScheduleIterator<Z> {
self.after_owned(timezone.from_utc_datetime(&Utc::now().naive_utc()))
}

/// Like the `upcoming` method, but allows you to specify a start time other than the present.
pub fn after<Z>(&self, after: &DateTime<Z>) -> ScheduleIterator<'_, Z>
where
Expand All @@ -235,6 +239,11 @@ impl Schedule {
ScheduleIterator::new(self, after)
}

/// The same, but with a static ownership.
pub fn after_owned<Z: TimeZone>(&self, after: DateTime<Z>) -> OwnedScheduleIterator<Z> {
OwnedScheduleIterator::new(self.clone(), after)
}

pub fn includes<Z>(&self, date_time: DateTime<Z>) -> bool
where
Z: TimeZone,
Expand Down Expand Up @@ -333,26 +342,23 @@ impl ScheduleFields {
}
}

/// Iterates `DateTime` occurrences of a schedule.
pub struct ScheduleIterator<'a, Z>
where
Z: TimeZone,
{
is_done: bool,
schedule: &'a Schedule,
previous_datetime: DateTime<Z>,
previous_datetime: Option<DateTime<Z>>,
}
//TODO: Cutoff datetime?

impl<'a, Z> ScheduleIterator<'a, Z>
where
Z: TimeZone,
{
fn new(schedule: &'a Schedule, starting_datetime: &DateTime<Z>) -> ScheduleIterator<'a, Z> {
fn new(schedule: &'a Schedule, starting_datetime: &DateTime<Z>) -> Self {
ScheduleIterator {
is_done: false,
schedule,
previous_datetime: starting_datetime.clone(),
previous_datetime: Some(starting_datetime.clone()),
}
}
}
Expand All @@ -364,14 +370,12 @@ where
type Item = DateTime<Z>;

fn next(&mut self) -> Option<DateTime<Z>> {
if self.is_done {
return None;
}
if let Some(next_datetime) = self.schedule.next_after(&self.previous_datetime) {
self.previous_datetime = next_datetime.clone();
Some(next_datetime)
let previous = self.previous_datetime.take()?;

if let Some(next) = self.schedule.next_after(&previous) {
self.previous_datetime = Some(next.clone());
Some(next)
} else {
self.is_done = true;
None
}
}
Expand All @@ -382,15 +386,52 @@ where
Z: TimeZone,
{
fn next_back(&mut self) -> Option<Self::Item> {
if self.is_done {
return None;
let previous = self.previous_datetime.take()?;

if let Some(prev) = self.schedule.prev_from(&previous) {
self.previous_datetime = Some(prev.clone());
Some(prev)
} else {
None
}
}
}

/// A `ScheduleIterator` with a static lifetime.
pub struct OwnedScheduleIterator<Z> where Z: TimeZone {
schedule: Schedule,
previous_datetime: Option<DateTime<Z>>
}

impl<Z> OwnedScheduleIterator<Z> where Z: TimeZone {
pub fn new(schedule: Schedule, starting_datetime: DateTime<Z>) -> Self {
Self { schedule, previous_datetime: Some(starting_datetime) }
}
}

impl<Z> Iterator for OwnedScheduleIterator<Z> where Z: TimeZone {
type Item = DateTime<Z>;

fn next(&mut self) -> Option<DateTime<Z>> {
let previous = self.previous_datetime.take()?;

if let Some(next) = self.schedule.next_after(&previous) {
self.previous_datetime = Some(next.clone());
Some(next)
} else {
None
}
}
}

impl<Z: TimeZone> DoubleEndedIterator for OwnedScheduleIterator<Z> {
fn next_back(&mut self) -> Option<Self::Item> {
let previous = self.previous_datetime.take()?;

if let Some(prev_datetime) = self.schedule.prev_from(&self.previous_datetime) {
self.previous_datetime = prev_datetime.clone();
Some(prev_datetime)
if let Some(prev) = self.schedule.prev_from(&previous) {
self.previous_datetime = Some(prev.clone());
Some(prev)
} else {
self.is_done = true;
None
}
}
Expand Down Expand Up @@ -471,6 +512,22 @@ mod test {
println!("Upcoming 3 for {} {:?}", expression, next3);
}

#[test]
fn test_upcoming_utc_owned() {
let expression = "0 0,30 0,6,12,18 1,15 Jan-March Thurs";
let schedule = Schedule::from_str(expression).unwrap();
let mut upcoming = schedule.upcoming_owned(Utc);
let next1 = upcoming.next();
assert!(next1.is_some());
let next2 = upcoming.next();
assert!(next2.is_some());
let next3 = upcoming.next();
assert!(next3.is_some());
println!("Upcoming 1 for {} {:?}", expression, next1);
println!("Upcoming 2 for {} {:?}", expression, next2);
println!("Upcoming 3 for {} {:?}", expression, next3);
}

#[test]
fn test_upcoming_rev_utc() {
let expression = "0 0,30 0,6,12,18 1,15 Jan-March Thurs";
Expand All @@ -487,6 +544,22 @@ mod test {
println!("Prev Upcoming 3 for {} {:?}", expression, prev3);
}

#[test]
fn test_upcoming_rev_utc_owned() {
let expression = "0 0,30 0,6,12,18 1,15 Jan-March Thurs";
let schedule = Schedule::from_str(expression).unwrap();
let mut upcoming = schedule.upcoming_owned(Utc).rev();
let prev1 = upcoming.next();
assert!(prev1.is_some());
let prev2 = upcoming.next();
assert!(prev2.is_some());
let prev3 = upcoming.next();
assert!(prev3.is_some());
println!("Prev Upcoming 1 for {} {:?}", expression, prev1);
println!("Prev Upcoming 2 for {} {:?}", expression, prev2);
println!("Prev Upcoming 3 for {} {:?}", expression, prev3);
}

#[test]
fn test_upcoming_local() {
use chrono::Local;
Expand Down