Skip to content

Commit

Permalink
Rewrite store get_*() from iterators to slices
Browse files Browse the repository at this point in the history
With that we can re-use the result.
  • Loading branch information
martinpitt committed Jun 12, 2022
1 parent 4a8ce09 commit 365c7e5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct Activities {
}

impl Activities {
pub fn new_from_entries<'a>(entries: impl Iterator<Item = &'a Entry>) -> Activities {
pub fn new_from_entries(entries: &[Entry]) -> Activities {
// don't use a hashmap here, we do want to keep this sorted by "first occurrence of task"
let mut activities = Vec::new();
let mut total_work = Duration::minutes(0);
Expand Down Expand Up @@ -157,7 +157,7 @@ mod tests {

#[test]
fn test_activities_empty() {
let a = Activities::new_from_entries(vec![].iter());
let a = Activities::new_from_entries(&[]);
assert_eq!(a.activities.len(), 0);
assert_eq!(a.total_work, Duration::minutes(0));
assert_eq!(a.total_slack, Duration::minutes(0));
Expand Down
53 changes: 27 additions & 26 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,37 @@ impl Timelog {
return self.entries.iter();
}

pub fn get_day(&self, day: &NaiveDate) -> impl Iterator<Item = &Entry> {
let begin = day.and_hms(0, 0, 0);
let end = day.and_hms(23, 59, 59);
self.entries
pub fn get_time_range(&self, begin: NaiveDateTime, end: NaiveDateTime) -> &[Entry] {
let first = self
.entries
.iter()
.filter(move |e| e.stop >= begin && e.stop <= end)
.position(move |e| e.stop >= begin)
.unwrap_or(self.entries.len());
let last = self
.entries
.iter()
.position(move |e| e.stop > end)
.unwrap_or(self.entries.len());

&self.entries[first..last]
}

pub fn get_today(&self) -> impl Iterator<Item = &Entry> {
pub fn get_day(&self, day: &NaiveDate) -> &[Entry] {
self.get_time_range(day.and_hms(0, 0, 0), day.and_hms(23, 59, 59))
}

pub fn get_today(&self) -> &[Entry] {
self.get_day(&Local::today().naive_local())
}

pub fn get_week(&self, day: &NaiveDate) -> impl Iterator<Item = &Entry> {
pub fn get_week(&self, day: &NaiveDate) -> &[Entry] {
let week = day.iso_week().week();
let begin = NaiveDate::from_isoywd(day.year(), week, Weekday::Mon).and_hms(0, 0, 0);
let end = NaiveDate::from_isoywd(day.year(), week + 1, Weekday::Mon).and_hms(0, 0, 0);
self.entries
.iter()
.filter(move |e| e.stop >= begin && e.stop < end)
self.get_time_range(begin, end)
}

pub fn get_this_week(&self) -> impl Iterator<Item = &Entry> {
pub fn get_this_week(&self) -> &[Entry] {
self.get_week(&Local::today().naive_local())
}

Expand Down Expand Up @@ -305,21 +314,17 @@ mod tests {
#[test]
fn test_get_day() {
let tl = Timelog::new_from_string("");
assert_eq!(tl.get_day(&NaiveDate::from_ymd(2022, 6, 8)).next(), None);
assert_eq!(tl.get_day(&NaiveDate::from_ymd(2022, 6, 8)), &[]);

let tl = Timelog::new_from_string(TWO_DAYS);
assert_eq!(tl.get_day(&NaiveDate::from_ymd(2022, 6, 8)).next(), None);
assert_eq!(tl.get_day(&NaiveDate::from_ymd(2022, 6, 8)), &[]);

let entries = tl
.get_day(&NaiveDate::from_ymd(2022, 6, 9))
.collect::<Vec<&Entry>>();
let entries = tl.get_day(&NaiveDate::from_ymd(2022, 6, 9));
assert_eq!(entries.len(), 4);
assert_eq!(&format!("{}", entries[0]), "2022-06-09 06:02: arrived");
assert_eq!(&format!("{}", entries[3]), "2022-06-09 12:00: work");

let entries = tl
.get_day(&NaiveDate::from_ymd(2022, 6, 10))
.collect::<Vec<&Entry>>();
let entries = tl.get_day(&NaiveDate::from_ymd(2022, 6, 10));
assert_eq!(entries.len(), 6);
assert_eq!(&format!("{}", entries[0]), "2022-06-10 07:00: arrived");
assert_eq!(
Expand All @@ -331,21 +336,17 @@ mod tests {
#[test]
fn test_get_week() {
let tl = Timelog::new_from_string("");
assert_eq!(tl.get_week(&NaiveDate::from_ymd(2022, 6, 2)).next(), None);
assert_eq!(tl.get_week(&NaiveDate::from_ymd(2022, 6, 2)), &[]);

let tl = Timelog::new_from_string(TWO_WEEKS);
// select Wed, data has Tue and Thu
let entries = tl
.get_week(&NaiveDate::from_ymd(2022, 6, 2))
.collect::<Vec<&Entry>>();
let entries = tl.get_week(&NaiveDate::from_ymd(2022, 6, 2));
assert_eq!(entries.len(), 6);
assert_eq!(&format!("{}", entries[0]), "2022-06-01 06:00: arrived");
assert_eq!(&format!("{}", entries[5]), "2022-06-03 07:10: ** tea");

// select Tue, data has Wed to Fri
let entries = tl
.get_week(&NaiveDate::from_ymd(2022, 6, 7))
.collect::<Vec<&Entry>>();
let entries = tl.get_week(&NaiveDate::from_ymd(2022, 6, 7));
assert_eq!(entries.len(), 7);
assert_eq!(&format!("{}", entries[0]), "2022-06-08 06:00: arrived");
assert_eq!(&format!("{}", entries[6]), "2022-06-10 07:00: workw2");
Expand Down

0 comments on commit 365c7e5

Please sign in to comment.