Verified Commit d32b96e5 authored by Maxime Buquet's avatar Maxime Buquet
Browse files

Implement From<X> for String and use in Display and Serialize



Semantics.
Signed-off-by: Maxime Buquet's avatarMaxime “pep” Buquet <pep@bouah.net>
parent 1c4ac07a
......@@ -55,31 +55,37 @@ impl TryFrom<BaseTask> for Task {
impl Into<BaseTask> for Task {
fn into(self) -> BaseTask {
BaseTask::new(vec![
self.category,
self.project,
self.activity,
self.task,
])
BaseTask::new(vec![self.category, self.project, self.activity, self.task])
}
}
impl fmt::Display for Task {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
impl<'a> From<&'a Task> for String {
fn from(t: &Task) -> String {
format!(
"{}: {}: {}: {}",
self.category, self.project, self.activity, self.task
&t.category, &t.project, &t.activity, &t.task
)
}
}
impl From<Task> for String {
fn from(t: Task) -> String {
String::from(&t)
}
}
impl fmt::Display for Task {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", String::from(self))
}
}
impl Serialize for Task {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("{}", self))
serializer.serialize_str(&String::from(self))
}
}
......@@ -106,24 +112,36 @@ pub fn duration_to_string(d: &Duration) -> String {
format!("{}:{:0>2}", hours, minutes)
}
impl fmt::Display for Entry {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.comment {
None => write!(f, "{}: {}:", self.duration, self.task),
impl<'a> From<&'a Entry> for String {
fn from(e: &Entry) -> String {
match &e.comment {
None => format!("{}: {}:", &e.duration, &e.task),
Some(ref c) => {
let d = duration_to_string(&self.duration);
write!(f, "{} {}: {}", d, self.task, c)
let d = duration_to_string(&e.duration);
format!("{} {}: {}", d, &e.task, c)
}
}
}
}
impl From<Entry> for String {
fn from(entry: Entry) -> String {
String::from(&entry)
}
}
impl fmt::Display for Entry {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", String::from(self))
}
}
impl Serialize for Entry {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("{}", self))
serializer.serialize_str(&String::from(self))
}
}
......@@ -240,7 +258,10 @@ mod test {
Task::new("foo", "bar", "baz", "qxx"),
Some("hah"),
);
assert_eq!(format!("{}", entry), String::from("1:05 foo: bar: baz: qxx: hah"));
assert_eq!(
format!("{}", entry),
String::from("1:05 foo: bar: baz: qxx: hah")
);
}
#[test]
......
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