Verified Commit 7cd3a9c6 authored by Maxime Buquet's avatar Maxime Buquet
Browse files

impl Serialize for chronophage::Day


Signed-off-by: Maxime Buquet's avatarMaxime “pep” Buquet <pep@bouah.net>
parent d32b96e5
......@@ -5,8 +5,9 @@
//
extern crate chrono;
extern crate try_from;
extern crate serde;
extern crate try_from;
extern crate serde_urlencoded;
pub mod errors;
pub mod types;
......@@ -6,7 +6,7 @@
use chrono::{Date, Duration, Utc};
use errors::ChronophageError as Error;
use serde::{Serialize, Serializer};
use serde::{ser::SerializeMap, Serialize, Serializer};
use std::fmt;
use try_from::TryFrom;
......@@ -170,6 +170,23 @@ pub struct Day {
items: Vec<Entry>,
}
impl Serialize for Day {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(self.items.len()))?;
let s_date = self.date.format("%Y-%m-%d").to_string();
let s_items = self.items.iter().fold(String::new(), |mut s, item| {
s.push_str(String::from(item).as_str());
s.push_str("\n");
s
});
map.serialize_entry(&s_date, &s_items)?;
map.end()
}
}
impl TryFrom<TimesheetDay> for Day {
type Err = Error;
......@@ -206,6 +223,7 @@ mod test {
use super::{Day, Entry, Task};
use chrono::{Date, Duration, NaiveDate, Utc};
use errors::ChronophageError as Error;
use serde_urlencoded;
use types::{Task as BaseTask, TimesheetDay, TimesheetItem};
use try_from::TryFrom;
......@@ -281,6 +299,29 @@ mod test {
assert_eq!(entry, result);
}
#[test]
fn serialize_day() {
let day = Day {
date: Date::from_utc(NaiveDate::from_ymd(2018, 7, 5), Utc),
items: vec![
Entry::new(
Duration::minutes(5),
Task::new("foo", "bar", "baz", "qxx"),
Some("Test"),
),
Entry::new(
Duration::hours(7),
Task::new("foo", "bar", "baz", "qxx"),
Some("Test"),
),
],
};
let result = "2018-07-05=0%3A05+foo%3A+bar%3A+baz%3A+qxx%3A+Test%0A7%3A00+foo%3A+bar%3A+baz%3A+qxx%3A+Test%0A";
assert_eq!(serde_urlencoded::to_string(day), Ok(String::from(result)));
}
#[test]
fn from_timelog_day() {
let task = BaseTask::new(vec!["foo", "bar", "baz", "qxx"]);
......
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