From d1a7d222f053ea081b2ec2fb747cb58c40314b30 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 27 May 2017 12:29:21 +0100 Subject: [PATCH] delay: Use chrono to parse the stamp. --- src/delay.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/delay.rs b/src/delay.rs index 19b1a495..65349040 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -7,6 +7,7 @@ use std::convert::TryFrom; use minidom::Element; +use chrono::prelude::*; use error::Error; use jid::Jid; @@ -16,7 +17,7 @@ use ns; #[derive(Debug, Clone)] pub struct Delay { pub from: Option, - pub stamp: String, + pub stamp: DateTime, pub data: Option, } @@ -31,7 +32,7 @@ impl TryFrom for Delay { return Err(Error::ParseError("Unknown child in delay element.")); } let from = get_attr!(elem, "from", optional); - let stamp = get_attr!(elem, "stamp", required); + let stamp = get_attr!(elem, "stamp", required, stamp, DateTime::parse_from_rfc3339(stamp)?); let data = match elem.text().as_ref() { "" => None, text => Some(text.to_owned()), @@ -49,7 +50,7 @@ impl Into for Delay { Element::builder("delay") .ns(ns::DELAY) .attr("from", self.from.and_then(|value| Some(String::from(value)))) - .attr("stamp", self.stamp) + .attr("stamp", self.stamp.to_rfc3339()) .append(self.data) .build() } @@ -65,7 +66,14 @@ mod tests { let elem: Element = "".parse().unwrap(); let delay = Delay::try_from(elem).unwrap(); assert_eq!(delay.from, Some(Jid::from_str("capulet.com").unwrap())); - assert_eq!(delay.stamp, "2002-09-10T23:08:25Z"); + assert_eq!(delay.stamp.year(), 2002); + assert_eq!(delay.stamp.month(), 9); + assert_eq!(delay.stamp.day(), 10); + assert_eq!(delay.stamp.hour(), 23); + assert_eq!(delay.stamp.minute(), 08); + assert_eq!(delay.stamp.second(), 25); + assert_eq!(delay.stamp.nanosecond(), 0); + assert_eq!(delay.stamp.timezone(), FixedOffset::east(0)); assert_eq!(delay.data, None); } @@ -93,10 +101,10 @@ mod tests { #[test] fn test_serialise() { - let elem: Element = "".parse().unwrap(); + let elem: Element = "".parse().unwrap(); let delay = Delay { from: None, - stamp: "2002-09-10T23:08:25Z".to_owned(), + stamp: DateTime::parse_from_rfc3339("2002-09-10T23:08:25Z").unwrap(), data: None, }; let elem2 = delay.into(); @@ -105,10 +113,10 @@ mod tests { #[test] fn test_serialise_data() { - let elem: Element = "Reason".parse().unwrap(); + let elem: Element = "Reason".parse().unwrap(); let delay = Delay { from: Some(Jid::from_str("juliet@example.org").unwrap()), - stamp: "2002-09-10T23:08:25Z".to_owned(), + stamp: DateTime::parse_from_rfc3339("2002-09-10T23:08:25Z").unwrap(), data: Some(String::from("Reason")), }; let elem2 = delay.into();