delay: Use chrono to parse the stamp.
This commit is contained in:
parent
bdaced7603
commit
d1a7d222f0
1 changed files with 16 additions and 8 deletions
24
src/delay.rs
24
src/delay.rs
|
@ -7,6 +7,7 @@
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use minidom::Element;
|
use minidom::Element;
|
||||||
|
use chrono::prelude::*;
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use jid::Jid;
|
use jid::Jid;
|
||||||
|
@ -16,7 +17,7 @@ use ns;
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Delay {
|
pub struct Delay {
|
||||||
pub from: Option<Jid>,
|
pub from: Option<Jid>,
|
||||||
pub stamp: String,
|
pub stamp: DateTime<FixedOffset>,
|
||||||
pub data: Option<String>,
|
pub data: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +32,7 @@ impl TryFrom<Element> for Delay {
|
||||||
return Err(Error::ParseError("Unknown child in delay element."));
|
return Err(Error::ParseError("Unknown child in delay element."));
|
||||||
}
|
}
|
||||||
let from = get_attr!(elem, "from", optional);
|
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() {
|
let data = match elem.text().as_ref() {
|
||||||
"" => None,
|
"" => None,
|
||||||
text => Some(text.to_owned()),
|
text => Some(text.to_owned()),
|
||||||
|
@ -49,7 +50,7 @@ impl Into<Element> for Delay {
|
||||||
Element::builder("delay")
|
Element::builder("delay")
|
||||||
.ns(ns::DELAY)
|
.ns(ns::DELAY)
|
||||||
.attr("from", self.from.and_then(|value| Some(String::from(value))))
|
.attr("from", self.from.and_then(|value| Some(String::from(value))))
|
||||||
.attr("stamp", self.stamp)
|
.attr("stamp", self.stamp.to_rfc3339())
|
||||||
.append(self.data)
|
.append(self.data)
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
@ -65,7 +66,14 @@ mod tests {
|
||||||
let elem: Element = "<delay xmlns='urn:xmpp:delay' from='capulet.com' stamp='2002-09-10T23:08:25Z'/>".parse().unwrap();
|
let elem: Element = "<delay xmlns='urn:xmpp:delay' from='capulet.com' stamp='2002-09-10T23:08:25Z'/>".parse().unwrap();
|
||||||
let delay = Delay::try_from(elem).unwrap();
|
let delay = Delay::try_from(elem).unwrap();
|
||||||
assert_eq!(delay.from, Some(Jid::from_str("capulet.com").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);
|
assert_eq!(delay.data, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,10 +101,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialise() {
|
fn test_serialise() {
|
||||||
let elem: Element = "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25Z'/>".parse().unwrap();
|
let elem: Element = "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25+00:00'/>".parse().unwrap();
|
||||||
let delay = Delay {
|
let delay = Delay {
|
||||||
from: None,
|
from: None,
|
||||||
stamp: "2002-09-10T23:08:25Z".to_owned(),
|
stamp: DateTime::parse_from_rfc3339("2002-09-10T23:08:25Z").unwrap(),
|
||||||
data: None,
|
data: None,
|
||||||
};
|
};
|
||||||
let elem2 = delay.into();
|
let elem2 = delay.into();
|
||||||
|
@ -105,10 +113,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialise_data() {
|
fn test_serialise_data() {
|
||||||
let elem: Element = "<delay xmlns='urn:xmpp:delay' from='juliet@example.org' stamp='2002-09-10T23:08:25Z'>Reason</delay>".parse().unwrap();
|
let elem: Element = "<delay xmlns='urn:xmpp:delay' from='juliet@example.org' stamp='2002-09-10T23:08:25+00:00'>Reason</delay>".parse().unwrap();
|
||||||
let delay = Delay {
|
let delay = Delay {
|
||||||
from: Some(Jid::from_str("juliet@example.org").unwrap()),
|
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")),
|
data: Some(String::from("Reason")),
|
||||||
};
|
};
|
||||||
let elem2 = delay.into();
|
let elem2 = delay.into();
|
||||||
|
|
Loading…
Reference in a new issue