Verified Commit 8597eab5 authored by Maxime Buquet's avatar Maxime Buquet
Browse files

Rustfmt!


Signed-off-by: Maxime Buquet's avatarMaxime “pep” Buquet <pep@bouah.net>
parent 7395ec23
......@@ -8,24 +8,23 @@
extern crate clap;
// #[macro_use]
extern crate log;
extern crate env_logger;
extern crate log;
extern crate chrono;
extern crate nom;
extern crate timelog;
extern crate chrono;
use std::fs::File;
use std::path::Path;
use std::io::Error as IOError;
use std::io::prelude::*;
use std::io::Error as IOError;
use std::path::Path;
use chrono::NaiveTime;
use nom::types::CompleteStr;
use timelog::types::TimesheetDay;
use timelog::parsers::parse_entries;
use timelog::helpers::split_by_days;
use chrono::NaiveTime;
use timelog::parsers::parse_entries;
use timelog::types::TimesheetDay;
pub fn read_file<P: AsRef<Path>>(path: P) -> Result<String, IOError> {
let mut fh = File::open(path)?;
......@@ -59,14 +58,16 @@ fn main() {
println!("Days: {:?}", days);
if let Ok(days) = days {
let timesheets: Vec<TimesheetDay> = days.into_iter()
let timesheets: Vec<TimesheetDay> = days
.into_iter()
.map(|d| {
println!("Day: {:?}", d);
TimesheetDay::from(d)
}).collect();
})
.collect();
println!("Timesheet: {:?}", timesheets);
}
},
}
Err(err) => println!("Entries error: {:?}", err),
}
}
......@@ -4,12 +4,15 @@
// Distributed under terms of the LGPLv3+ license.
//
use chrono::{NaiveTime, Duration};
use chrono::{Duration, NaiveTime};
use types::{TimelogDay, TimelogEntry};
use errors::Error;
use types::{TimelogDay, TimelogEntry};
pub fn split_by_days(entries: Vec<TimelogEntry>, vmidnight: NaiveTime) -> Result<Vec<TimelogDay>, Error> {
pub fn split_by_days(
entries: Vec<TimelogEntry>,
vmidnight: NaiveTime,
) -> Result<Vec<TimelogDay>, Error> {
// TODO: Sort entries
let mut last_date = None;
......@@ -30,10 +33,7 @@ pub fn split_by_days(entries: Vec<TimelogEntry>, vmidnight: NaiveTime) -> Result
if last_date.is_some() && date == last_date.unwrap() {
let mut entries = last_day.entries.clone();
entries.push(entry);
day = TimelogDay {
date,
entries,
}
day = TimelogDay { date, entries }
}
}
......@@ -49,8 +49,8 @@ pub fn split_by_days(entries: Vec<TimelogEntry>, vmidnight: NaiveTime) -> Result
#[cfg(test)]
mod test {
use chrono::{Date, NaiveDate, NaiveTime, TimeZone, Utc};
use types::{TimelogDay, TimelogEntry};
use chrono::{Date, NaiveDate, NaiveTime, Utc, TimeZone};
use super::split_by_days;
......@@ -70,10 +70,7 @@ mod 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),
"slack1 **",
),
TimelogEntry::new_slack(Utc.ymd(2018, 7, 2).and_hms(10, 0, 0), "slack1 **"),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(10, 15, 0),
"project1",
......@@ -98,10 +95,7 @@ mod test {
"task3",
"comment3",
),
TimelogEntry::new_slack(
Utc.ymd(2018, 7, 3).and_hms(10, 0, 0),
"slack1 **",
),
TimelogEntry::new_slack(Utc.ymd(2018, 7, 3).and_hms(10, 0, 0), "slack1 **"),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(10, 15, 0),
"project1",
......@@ -120,63 +114,60 @@ mod test {
),
];
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),
"slack1 **",
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(10, 15, 0),
"project1",
"category1",
"activity1",
"task1",
"comment1",
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(18, 0, 0),
"project2",
"category2",
"activity2",
"task2",
"comment2",
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(2, 0, 0),
"project3",
"category3",
"activity3",
"task3",
"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),
"slack1 **",
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(10, 15, 0),
"project1",
"category1",
"activity1",
"task1",
"comment1",
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(18, 0, 0),
"project2",
"category2",
"activity2",
"task2",
"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), "slack1 **"),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(10, 15, 0),
"project1",
"category1",
"activity1",
"task1",
"comment1",
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(18, 0, 0),
"project2",
"category2",
"activity2",
"task2",
"comment2",
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(2, 0, 0),
"project3",
"category3",
"activity3",
"task3",
"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), "slack1 **"),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(10, 15, 0),
"project1",
"category1",
"activity1",
"task1",
"comment1",
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(18, 0, 0),
"project2",
"category2",
"activity2",
"task2",
"comment2",
),
],
},
];
match split_by_days(entries, vmidnight) {
Ok(days) => assert_eq!(result, days),
......@@ -189,10 +180,7 @@ mod 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),
"slack1 **",
),
TimelogEntry::new_slack(Utc.ymd(2018, 7, 2).and_hms(10, 0, 0), "slack1 **"),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(10, 15, 0),
"project1",
......@@ -214,10 +202,7 @@ mod test {
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),
"slack1 **",
),
TimelogEntry::new_slack(Utc.ymd(2018, 7, 2).and_hms(10, 0, 0), "slack1 **"),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 2).and_hms(10, 15, 0),
"project1",
......
......@@ -11,7 +11,7 @@ extern crate regex;
extern crate lazy_static;
extern crate chrono;
pub mod types;
pub mod errors;
pub mod parsers;
pub mod helpers;
pub mod parsers;
pub mod types;
......@@ -6,8 +6,8 @@
use types::TimelogEntry;
use chrono::prelude::{DateTime, Utc, FixedOffset};
use nom::{types::CompleteStr, space};
use chrono::prelude::{DateTime, FixedOffset, Utc};
use nom::{space, types::CompleteStr};
pub fn is_name(chr: char) -> bool {
chr.is_alphanumeric() || chr == '-' || chr == '_'
......@@ -98,11 +98,11 @@ named!(pub parse_entries(CompleteStr) -> Vec<TimelogEntry>,
#[cfg(test)]
mod test {
use types::{TimelogEntry};
use super::{parse_dt, parse_name, parse_rest, parse_slack};
use super::{parse_entry_full, parse_entry_slack, parse_entry, parse_entries};
use nom::{types::CompleteStr, ErrorKind, Context, Err as NomErr};
use chrono::{DateTime, Utc, TimeZone};
use super::{parse_entries, parse_entry, parse_entry_full, parse_entry_slack};
use chrono::{DateTime, TimeZone, Utc};
use nom::{types::CompleteStr, Context, Err as NomErr, ErrorKind};
use types::TimelogEntry;
#[test]
fn test_parse_dt_colon() {
......@@ -134,7 +134,7 @@ mod test {
} else {
panic!()
}
},
}
_ => panic!(),
}
}
......@@ -152,7 +152,7 @@ mod test {
assert_eq!(name1, result);
assert_eq!(name2, result);
assert_eq!(name3, result);
},
}
_ => panic!(),
}
}
......@@ -167,7 +167,7 @@ mod test {
(Ok((_, name1)), Ok((_, name2))) => {
assert_eq!(name1, result);
assert_eq!(name2, result);
},
}
_ => panic!(),
}
}
......@@ -181,7 +181,7 @@ mod test {
match parse_entry_full(entry1) {
Ok((_, entry1)) => {
assert_eq!(entry1, result);
},
}
_ => panic!(),
}
}
......@@ -194,7 +194,7 @@ mod test {
match parse_slack(entry1) {
Ok((_, entry1)) => {
assert_eq!(entry1, result);
},
}
_ => panic!(),
}
}
......@@ -208,7 +208,7 @@ mod test {
match parse_entry_slack(entry1) {
Ok((_, entry1)) => {
assert_eq!(entry1, result);
},
}
_ => panic!(),
}
}
......@@ -227,30 +227,32 @@ mod test {
(Ok((_, entry1)), Ok((_, entry2))) => {
assert_eq!(entry1, result_full);
assert_eq!(entry2, result_slack);
},
}
_ => panic!(),
}
}
#[test]
fn test_parse_entries() {
let entries = CompleteStr(r#"
let entries = CompleteStr(
r#"
2018-02-03 10:43 +0100: Arrived **
2018-02-05 21:08 +0900: foo: bar: baz: meh: AAAA
"#);
"#,
);
let d1: DateTime<Utc> = Utc.ymd(2018, 2, 3).and_hms(9, 43, 0);
let d2: DateTime<Utc> = Utc.ymd(2018, 2, 5).and_hms(12, 8, 0);
let result = vec!(
let result = vec![
TimelogEntry::new_slack(d1, "Arrived **"),
TimelogEntry::new_full(d2, "foo", "bar", "baz", "meh", "AAAA"),
);
];
println!("FOO: {:?}", parse_entries(entries));
match parse_entries(entries) {
Ok((_, entries1)) => {
assert_eq!(entries1, result);
},
}
_ => panic!(),
}
}
......
......@@ -14,7 +14,13 @@ pub struct FullEntry {
}
impl FullEntry {
pub fn new(project: &str, category: &str, activity: &str, task: &str, comment: &str) -> FullEntry {
pub fn new(
project: &str,
category: &str,
activity: &str,
task: &str,
comment: &str,
) -> FullEntry {
FullEntry {
project: String::from(project),
category: String::from(category),
......
......@@ -6,14 +6,14 @@
pub mod full_entry;
pub mod slack_entry;
pub mod timelog_entry;
pub mod timelog_day;
pub mod timesheet_item;
pub mod timelog_entry;
pub mod timesheet_day;
pub mod timesheet_item;
pub use self::full_entry::FullEntry;
pub use self::slack_entry::SlackEntry;
pub use self::timelog_entry::{EntryType, TimelogEntry};
pub use self::timelog_day::TimelogDay;
pub use self::timesheet_item::TimesheetItem;
pub use self::timelog_entry::{EntryType, TimelogEntry};
pub use self::timesheet_day::TimesheetDay;
pub use self::timesheet_item::TimesheetItem;
......@@ -16,4 +16,3 @@ impl SlackEntry {
}
}
}
......@@ -4,8 +4,8 @@
// Distributed under terms of the LGPLv3+ license.
//
use ::types::TimelogEntry;
use chrono::{Date, Utc};
use types::TimelogEntry;
#[derive(Debug, PartialEq)]
pub struct TimelogDay {
......
......@@ -4,8 +4,8 @@
// Distributed under terms of the LGPLv3+ license.
//
use ::types::{FullEntry, SlackEntry};
use chrono::{DateTime, Utc};
use types::{FullEntry, SlackEntry};
#[derive(Debug, Clone, PartialEq)]
pub enum EntryType {
......@@ -20,7 +20,14 @@ pub struct TimelogEntry {
}
impl TimelogEntry {
pub fn new_full(datetime: DateTime<Utc>, project: &str, category: &str, activity: &str, task: &str, comment: &str) -> TimelogEntry {
pub fn new_full(
datetime: DateTime<Utc>,
project: &str,
category: &str,
activity: &str,
task: &str,
comment: &str,
) -> TimelogEntry {
TimelogEntry {
datetime,
entry: EntryType::Full(FullEntry::new(project, category, activity, task, comment)),
......@@ -51,8 +58,8 @@ impl TimelogEntry {
#[cfg(test)]
mod test {
use ::types::{TimelogEntry};
use chrono::{Utc, TimeZone};
use chrono::{TimeZone, Utc};
use types::TimelogEntry;
#[test]
fn test_entry_is_full() {
......@@ -64,10 +71,8 @@ mod test {
"task1",
"comment1",
);
let entry_slack = TimelogEntry::new_slack(
Utc.ymd(2018, 7, 3).and_hms(10, 0, 0),
"slack1 **",
);
let entry_slack =
TimelogEntry::new_slack(Utc.ymd(2018, 7, 3).and_hms(10, 0, 0), "slack1 **");
assert!(entry_full.is_full());
assert!(!entry_slack.is_full());
......@@ -83,10 +88,8 @@ mod test {
"task1",
"comment1",
);
let entry_slack = TimelogEntry::new_slack(
Utc.ymd(2018, 7, 3).and_hms(10, 0, 0),
"slack1 **",
);
let entry_slack =
TimelogEntry::new_slack(Utc.ymd(2018, 7, 3).and_hms(10, 0, 0), "slack1 **");
assert!(!entry_full.is_slack());
assert!(entry_slack.is_slack());
......
......@@ -4,8 +4,8 @@
// Distributed under terms of the LGPLv3+ license.
//
use ::types::{EntryType, TimelogDay, TimesheetItem};
use chrono::{Date, Utc};
use types::{EntryType, TimelogDay, TimesheetItem};
#[derive(Debug, PartialEq)]
pub struct TimesheetDay {
......@@ -15,30 +15,34 @@ pub struct TimesheetDay {
impl From<TimelogDay> for TimesheetDay {
fn from(day: TimelogDay) -> TimesheetDay {
let items = day.entries.into_iter().fold((vec![], None), |(mut acc, last_entry), entry| {
if last_entry.is_none() || entry.is_slack() {
return (acc, Some(entry));
}
// TODO: Sort entries, or fail if not sorted
if let Some(last_entry) = last_entry {
if let EntryType::Full(ref entry_foo) = entry.entry {
let item = TimesheetItem::new(
entry.datetime - last_entry.datetime,
&entry_foo.project,
&entry_foo.category,
&entry_foo.activity,
&entry_foo.task,
&entry_foo.comment,
);
acc.push(item);
let items = day
.entries
.into_iter()
.fold((vec![], None), |(mut acc, last_entry), entry| {
if last_entry.is_none() || entry.is_slack() {
return (acc, Some(entry));
}
}
return (acc, Some(entry.clone()))
}).0;
// TODO: Sort entries, or fail if not sorted
if let Some(last_entry) = last_entry {
if let EntryType::Full(ref entry_foo) = entry.entry {
let item = TimesheetItem::new(
entry.datetime - last_entry.datetime,
&entry_foo.project,
&entry_foo.category,
&entry_foo.activity,
&entry_foo.task,
&entry_foo.comment,
);
acc.push(item);
}
}
return (acc, Some(entry.clone()));
})
.0;
TimesheetDay {
date: day.date,
......@@ -49,8 +53,8 @@ impl From<TimelogDay> for TimesheetDay {
#[cfg(test)]
mod test {
use ::types::{TimesheetItem, TimesheetDay, TimelogDay, TimelogEntry};
use chrono::{Date, NaiveDate, Utc, TimeZone, Duration};
use chrono::{Date, Duration, NaiveDate, TimeZone, Utc};
use types::{TimelogDay, TimelogEntry, TimesheetDay, TimesheetItem};
#[test]
fn test_timesheet_from_day_empty() {
......@@ -72,18 +76,9 @@ mod test {
let day = 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),
"slack1 **",
),
TimelogEntry::new_slack(
Utc.ymd(2018, 7, 3).and_hms(13, 0, 0),
"slack2 **",
),
TimelogEntry::new_slack(
Utc.ymd(2018, 7, 3).and_hms(15, 0, 0),
"slack3 **",
),
TimelogEntry::new_slack(Utc.ymd(2018, 7, 3).and_hms(10, 0, 0), "slack1 **"),
TimelogEntry::new_slack(Utc.ymd(2018, 7, 3).and_hms(13, 0, 0), "slack2 **"),
TimelogEntry::new_slack(Utc.ymd(2018, 7, 3).and_hms(15, 0, 0), "slack3 **"),
],
};
......@@ -100,10 +95,7 @@ mod test {
let day = 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),
"slack1 **",
),
TimelogEntry::new_slack(Utc.ymd(2018, 7, 3).and_hms(10, 0, 0), "slack1 **"),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(10, 15, 0),
"project1",
......
......@@ -4,8 +4,8 @@
// Distributed under terms of the LGPLv3+ license.
//
use ::types::FullEntry;
use chrono::Duration;
use types::FullEntry;
#[derive(Debug, PartialEq)]
pub struct TimesheetItem {
......@@ -14,7 +14,14 @@ pub struct TimesheetItem {
}