Verified Commit 08d33261 authored by Maxime Buquet's avatar Maxime Buquet
Browse files

chronophage example: rustfmt


Signed-off-by: Maxime Buquet's avatarMaxime “pep” Buquet <pep@bouah.net>
parent 1a35c885
......@@ -12,7 +12,7 @@ extern crate timelog;
use try_from::TryFrom;
use chrono::{Date, Duration, NaiveDate, TimeZone, Utc};
use timelog::types::{Task, EntryType, TimelogEntry, TimelogDay, TimesheetItem, TimesheetDay};
use timelog::types::{EntryType, Task, TimelogDay, TimelogEntry, TimesheetDay, TimesheetItem};
/// Tasks in Collabora Chronophage are more restricted than standard gtimelog
/// tasks. They must contain 4 identifiers, and these identifiers have specific
......@@ -26,12 +26,7 @@ pub struct ChronophageTask {
}
impl ChronophageTask {
fn new<S: Into<String>>(
category: S,
project: S,
activity: S,
task: S,
) -> ChronophageTask {
fn new<S: Into<String>>(category: S, project: S, activity: S, task: S) -> ChronophageTask {
ChronophageTask {
category: category.into(),
project: project.into(),
......@@ -86,14 +81,14 @@ impl TryFrom<TimesheetItem> for ChronophageEntry {
fn try_from(timesheet: TimesheetItem) -> Result<ChronophageEntry, String> {
match timesheet.entry {
EntryType::Slack => return Err(String::from("Chronophage doesn't accept slack entries")),
EntryType::Full(task) => {
Ok(ChronophageEntry::new(
timesheet.duration,
ChronophageTask::try_from(task)?,
timesheet.comment,
))
EntryType::Slack => {
return Err(String::from("Chronophage doesn't accept slack entries"))
}
EntryType::Full(task) => Ok(ChronophageEntry::new(
timesheet.duration,
ChronophageTask::try_from(task)?,
timesheet.comment,
)),
}
}
}
......@@ -110,20 +105,25 @@ impl TryFrom<TimesheetDay> for ChronophageDay {
type Err = String;
fn try_from(day: TimesheetDay) -> Result<ChronophageDay, String> {
let items = day
.items
.into_iter()
.try_fold(vec![], |mut acc, tentry|
-> Result<Vec<ChronophageEntry>, String> {
let items = day.items.into_iter().try_fold(
vec![],
|mut acc, tentry| -> Result<Vec<ChronophageEntry>, String> {
match tentry {
TimesheetItem { entry: EntryType::Slack, ..} => (),
item @ TimesheetItem { entry: EntryType::Full(_), .. } => {
TimesheetItem {
entry: EntryType::Slack,
..
} => (),
item @ TimesheetItem {
entry: EntryType::Full(_),
..
} => {
acc.push(ChronophageEntry::try_from(item)?);
},
}
}
return Ok(acc);
})?;
},
)?;
Ok(ChronophageDay {
date: day.date,
......@@ -132,8 +132,6 @@ impl TryFrom<TimesheetDay> for ChronophageDay {
}
}
fn main() {
let task1 = Task::new(vec!["foo", "bar"]);
let task2 = Task::new(vec!["baz", "qxx", "hqh", "hxh"]);
......@@ -143,25 +141,49 @@ fn main() {
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("Arrived **")),
TimelogEntry::new_full(Utc.ymd(2018, 7, 3).and_hms(13, 0, 0), task1.clone(), Some("Test")),
TimelogEntry::new_full(Utc.ymd(2018, 7, 3).and_hms(18, 0, 0), task1.clone(), Some("Test")),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(13, 0, 0),
task1.clone(),
Some("Test"),
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 3).and_hms(18, 0, 0),
task1.clone(),
Some("Test"),
),
],
},
TimelogDay {
date: Date::from_utc(NaiveDate::from_ymd(2018, 7, 4), Utc),
entries: vec![
TimelogEntry::new_slack(Utc.ymd(2018, 7, 4).and_hms(10, 5, 0), Some("Arrived **")),
TimelogEntry::new_full(Utc.ymd(2018, 7, 4).and_hms(13, 0, 0), task1.clone(), Some("Test")),
TimelogEntry::new_full(Utc.ymd(2018, 7, 4).and_hms(18, 0, 0), task2.clone(), Some("Test")),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 4).and_hms(13, 0, 0),
task1.clone(),
Some("Test"),
),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 4).and_hms(18, 0, 0),
task2.clone(),
Some("Test"),
),
],
},
TimelogDay {
date: Date::from_utc(NaiveDate::from_ymd(2018, 7, 5), Utc),
entries: vec![
TimelogEntry::new_slack(Utc.ymd(2018, 7, 5).and_hms(18, 5, 0), Some("Arrived **")),
TimelogEntry::new_full(Utc.ymd(2018, 7, 5).and_hms(19, 0, 0), task2.clone(), Some("Test")),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 5).and_hms(19, 0, 0),
task2.clone(),
Some("Test"),
),
TimelogEntry::new_slack(Utc.ymd(2018, 7, 5).and_hms(20, 0, 0), Some("Foo **")),
TimelogEntry::new_full(Utc.ymd(2018, 7, 6).and_hms(2, 0, 0), task2.clone(), Some("Test")),
TimelogEntry::new_full(
Utc.ymd(2018, 7, 6).and_hms(2, 0, 0),
task2.clone(),
Some("Test"),
),
],
},
];
......@@ -172,14 +194,16 @@ fn main() {
.collect();
println!("Foo: {:?}", timesheets);
timesheets.into_iter().for_each(|t| println!("Foo1: {:?}", ChronophageDay::try_from(t)));
timesheets
.into_iter()
.for_each(|t| println!("Foo1: {:?}", ChronophageDay::try_from(t)));
}
#[cfg(test)]
mod test {
use super::{ChronophageDay, ChronophageEntry, ChronophageTask};
use chrono::{Date, Duration, NaiveDate, Utc};
use timelog::types::{Task, TimesheetItem, TimesheetDay};
use super::{ChronophageTask, ChronophageEntry, ChronophageDay};
use timelog::types::{Task, TimesheetDay, TimesheetItem};
use try_from::TryFrom;
......
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