Commit 674afce1 authored by Maxime Buquet's avatar Maxime Buquet
Browse files

Remove helpers mod


Signed-off-by: Maxime Buquet's avatarMaxime “pep” Buquet <pep@bouah.net>
parent 470faee3
//
// helpers.rs
// Copyright (C) 2018 Maxime “pep” Buquet <pep@bouah.net>
// Distributed under terms of the LGPLv3+ license.
//
use chrono::{Duration, NaiveTime};
use errors::Error;
use std::cmp::Ordering;
use types::{TimelogDay, TimelogEntry};
pub fn split_by_days(
mut entries: Vec<TimelogEntry>,
vmidnight: NaiveTime,
) -> Result<Vec<TimelogDay>, Error> {
entries.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
let mut last_date = None;
Ok(entries.into_iter().fold(vec![], |mut acc, entry| {
let mut date = entry.datetime.date();
let time = entry.datetime.time();
// TODO: Verify where the split happens in original implementation, "<" or "<="
if time <= vmidnight {
date = date - Duration::days(1);
}
let mut day = TimelogDay {
date,
entries: vec![entry.clone()],
};
if let Some(last_day) = acc.last() {
if last_date.is_some() && date == last_date.unwrap() {
let mut entries = last_day.entries.clone();
entries.push(entry);
day = TimelogDay { date, entries }
}
}
if last_date.is_some() && date == last_date.unwrap() {
let _ = acc.pop();
}
last_date = Some(date);
acc.push(day);
acc
}))
}
#[cfg(test)]
mod test {
use chrono::{Date, NaiveDate, NaiveTime, TimeZone, Utc};
use types::{Task, TimelogDay, TimelogEntry};
use super::split_by_days;
#[test]
fn test_split_by_days_empty() {
let vmidnight = NaiveTime::parse_from_str("00:00", "%H:%M").unwrap();
let entries = vec![];
let result: Vec<TimelogDay> = vec![];
match split_by_days(entries, vmidnight) {
Ok(days) => assert_eq!(result, days),
Err(_) => panic!(),
}
}
#[test]
fn test_split_by_days() {
let vmidnight = NaiveTime::parse_from_str("04:00", "%H:%M").unwrap();
let entries = vec![
TimelogEntry::new_slack(Utc.ymd(2018, 7, 2).and_hms(10, 0, 0), Some("slack1 **")),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(10, 15, 0),
Task::new(vec!["project1", "category1", "activity1", "task1"]),
Some("comment1"),
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(18, 0, 0),
Task::new(vec!["project2", "category2", "activity2", "task2"]),
Some("comment2"),
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(2, 0, 0),
Task::new(vec!["project3", "category3", "activity3", "task3"]),
Some("comment3"),
),
TimelogEntry::new_slack(Utc.ymd(2018, 7, 3).and_hms(10, 0, 0), Some("slack1 **")),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(10, 15, 0),
Task::new(vec!["project1", "category1", "activity1", "task1"]),
Some("comment1"),
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(18, 0, 0),
Task::new(vec!["project2", "category2", "activity2", "task2"]),
Some("comment2"),
),
];
let result: Vec<TimelogDay> = vec![
TimelogDay {
date: Date::from_utc(NaiveDate::from_ymd(2018, 7, 2), Utc),
entries: vec![
TimelogEntry::new_slack(
Utc.ymd(2018, 7, 2).and_hms(10, 0, 0),
Some("slack1 **"),
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(10, 15, 0),
Task::new(vec!["project1", "category1", "activity1", "task1"]),
Some("comment1"),
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(18, 0, 0),
Task::new(vec!["project2", "category2", "activity2", "task2"]),
Some("comment2"),
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(2, 0, 0),
Task::new(vec!["project3", "category3", "activity3", "task3"]),
Some("comment3"),
),
],
},
TimelogDay {
date: Date::from_utc(NaiveDate::from_ymd(2018, 7, 3), Utc),
entries: vec![
TimelogEntry::new_slack(
Utc.ymd(2018, 7, 3).and_hms(10, 0, 0),
Some("slack1 **"),
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(10, 15, 0),
Task::new(vec!["project1", "category1", "activity1", "task1"]),
Some("comment1"),
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(18, 0, 0),
Task::new(vec!["project2", "category2", "activity2", "task2"]),
Some("comment2"),
),
],
},
];
match split_by_days(entries, vmidnight) {
Ok(days) => assert_eq!(result, days),
Err(_) => panic!(),
}
}
#[test]
fn test_split_by_days_order() {
let vmidnight = NaiveTime::parse_from_str("00:00", "%H:%M").unwrap();
let entries = vec![
TimelogEntry::new_slack(Utc.ymd(2018, 7, 2).and_hms(10, 0, 0), Some("slack1 **")),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(10, 15, 0),
Task::new(vec!["project1", "category1", "activity1", "task1"]),
Some("comment1"),
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(18, 0, 0),
Task::new(vec!["project2", "category2", "activity2", "task2"]),
Some("comment2"),
),
];
let result: Vec<TimelogDay> = vec![TimelogDay {
date: Date::from_utc(NaiveDate::from_ymd(2018, 7, 2), Utc),
entries: vec![
TimelogEntry::new_slack(Utc.ymd(2018, 7, 2).and_hms(10, 0, 0), Some("slack1 **")),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(10, 15, 0),
Task::new(vec!["project1", "category1", "activity1", "task1"]),
Some("comment1"),
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(18, 0, 0),
Task::new(vec!["project2", "category2", "activity2", "task2"]),
Some("comment2"),
),
],
}];
match split_by_days(entries, vmidnight) {
Ok(days) => assert_eq!(result, days),
Err(_) => panic!(),
}
}
}
......@@ -8,5 +8,4 @@ extern crate chrono;
extern crate try_from;
pub mod errors;
pub mod helpers;
pub mod types;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment