stanza_id: Switch to Into/TryFrom.

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-06 20:10:35 +01:00
parent 151635f5fb
commit 7ebabf7e91

View file

@ -4,6 +4,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::convert::TryFrom;
use minidom::Element; use minidom::Element;
use jid::Jid; use jid::Jid;
@ -22,20 +24,23 @@ pub enum StanzaId {
}, },
} }
pub fn parse_stanza_id(root: &Element) -> Result<StanzaId, Error> { impl<'a> TryFrom<&'a Element> for StanzaId {
let is_stanza_id = root.is("stanza-id", ns::SID); type Error = Error;
if !is_stanza_id && !root.is("origin-id", ns::SID) {
fn try_from(elem: &'a Element) -> Result<StanzaId, Error> {
let is_stanza_id = elem.is("stanza-id", ns::SID);
if !is_stanza_id && !elem.is("origin-id", ns::SID) {
return Err(Error::ParseError("This is not a stanza-id or origin-id element.")); return Err(Error::ParseError("This is not a stanza-id or origin-id element."));
} }
for _ in root.children() { for _ in elem.children() {
return Err(Error::ParseError("Unknown child in stanza-id or origin-id element.")); return Err(Error::ParseError("Unknown child in stanza-id or origin-id element."));
} }
let id = match root.attr("id") { let id = match elem.attr("id") {
Some(id) => id.to_owned(), Some(id) => id.to_owned(),
None => return Err(Error::ParseError("No 'id' attribute present in stanza-id or origin-id.")), None => return Err(Error::ParseError("No 'id' attribute present in stanza-id or origin-id.")),
}; };
Ok(if is_stanza_id { Ok(if is_stanza_id {
let by = match root.attr("by") { let by = match elem.attr("by") {
Some(by) => by.parse().unwrap(), Some(by) => by.parse().unwrap(),
None => return Err(Error::ParseError("No 'by' attribute present in stanza-id.")), None => return Err(Error::ParseError("No 'by' attribute present in stanza-id.")),
}; };
@ -43,10 +48,12 @@ pub fn parse_stanza_id(root: &Element) -> Result<StanzaId, Error> {
} else { } else {
StanzaId::OriginId { id } StanzaId::OriginId { id }
}) })
}
} }
pub fn serialise(stanza_id: &StanzaId) -> Element { impl<'a> Into<Element> for &'a StanzaId {
match *stanza_id { fn into(self) -> Element {
match *self {
StanzaId::StanzaId { ref id, ref by } => { StanzaId::StanzaId { ref id, ref by } => {
Element::builder("stanza-id") Element::builder("stanza-id")
.ns(ns::SID) .ns(ns::SID)
@ -61,22 +68,19 @@ pub fn serialise(stanza_id: &StanzaId) -> Element {
.build() .build()
}, },
} }
}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
use std::str::FromStr; use std::str::FromStr;
use minidom::Element;
use jid::Jid;
use error::Error;
use stanza_id;
#[test] #[test]
fn test_simple() { fn test_simple() {
let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>".parse().unwrap(); let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>".parse().unwrap();
let stanza_id = stanza_id::parse_stanza_id(&elem).unwrap(); let stanza_id = StanzaId::try_from(&elem).unwrap();
if let stanza_id::StanzaId::StanzaId { id, by } = stanza_id { if let StanzaId::StanzaId { id, by } = stanza_id {
assert_eq!(id, String::from("coucou")); assert_eq!(id, String::from("coucou"));
assert_eq!(by, Jid::from_str("coucou@coucou").unwrap()); assert_eq!(by, Jid::from_str("coucou@coucou").unwrap());
} else { } else {
@ -84,8 +88,8 @@ mod tests {
} }
let elem: Element = "<origin-id xmlns='urn:xmpp:sid:0' id='coucou'/>".parse().unwrap(); let elem: Element = "<origin-id xmlns='urn:xmpp:sid:0' id='coucou'/>".parse().unwrap();
let stanza_id = stanza_id::parse_stanza_id(&elem).unwrap(); let stanza_id = StanzaId::try_from(&elem).unwrap();
if let stanza_id::StanzaId::OriginId { id } = stanza_id { if let StanzaId::OriginId { id } = stanza_id {
assert_eq!(id, String::from("coucou")); assert_eq!(id, String::from("coucou"));
} else { } else {
panic!(); panic!();
@ -95,7 +99,7 @@ mod tests {
#[test] #[test]
fn test_invalid_child() { fn test_invalid_child() {
let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0'><coucou/></stanza-id>".parse().unwrap(); let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0'><coucou/></stanza-id>".parse().unwrap();
let error = stanza_id::parse_stanza_id(&elem).unwrap_err(); let error = StanzaId::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -106,7 +110,7 @@ mod tests {
#[test] #[test]
fn test_invalid_id() { fn test_invalid_id() {
let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0'/>".parse().unwrap(); let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0'/>".parse().unwrap();
let error = stanza_id::parse_stanza_id(&elem).unwrap_err(); let error = StanzaId::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -117,7 +121,7 @@ mod tests {
#[test] #[test]
fn test_invalid_by() { fn test_invalid_by() {
let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou'/>".parse().unwrap(); let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou'/>".parse().unwrap();
let error = stanza_id::parse_stanza_id(&elem).unwrap_err(); let error = StanzaId::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -128,8 +132,8 @@ mod tests {
#[test] #[test]
fn test_serialise() { fn test_serialise() {
let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>".parse().unwrap(); let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>".parse().unwrap();
let stanza_id = stanza_id::StanzaId::StanzaId { id: String::from("coucou"), by: Jid::from_str("coucou@coucou").unwrap() }; let stanza_id = StanzaId::StanzaId { id: String::from("coucou"), by: Jid::from_str("coucou@coucou").unwrap() };
let elem2 = stanza_id::serialise(&stanza_id); let elem2 = (&stanza_id).into();
assert_eq!(elem, elem2); assert_eq!(elem, elem2);
} }
} }