From f963715e77c2899a35e3d521609b9696c3da28f8 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 6 May 2017 20:58:22 +0100 Subject: [PATCH] ecaps2: Switch to Into/TryFrom. --- src/ecaps2.rs | 55 ++++++++++++++++++++++++++----------------------- src/presence.rs | 8 +++---- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/ecaps2.rs b/src/ecaps2.rs index 62242db..fdddfda 100644 --- a/src/ecaps2.rs +++ b/src/ecaps2.rs @@ -25,33 +25,37 @@ pub struct ECaps2 { hashes: Vec, } -pub fn parse_ecaps2(root: &Element) -> Result { - if !root.is("c", ns::ECAPS2) { - return Err(Error::ParseError("This is not an ecaps2 element.")); - } - let mut hashes = vec!(); - for child in root.children() { - if child.is("hash", ns::HASHES) { - let hash = Hash::try_from(child)?; - hashes.push(hash); - } else { - return Err(Error::ParseError("Unknown child in ecaps2 element.")); +impl<'a> TryFrom<&'a Element> for ECaps2 { + type Error = Error; + + fn try_from(elem: &'a Element) -> Result { + if !elem.is("c", ns::ECAPS2) { + return Err(Error::ParseError("This is not an ecaps2 element.")); } + let mut hashes = vec!(); + for child in elem.children() { + if child.is("hash", ns::HASHES) { + let hash = Hash::try_from(child)?; + hashes.push(hash); + } else { + return Err(Error::ParseError("Unknown child in ecaps2 element.")); + } + } + Ok(ECaps2 { + hashes: hashes, + }) } - Ok(ECaps2 { - hashes: hashes, - }) } -pub fn serialise(ecaps2: &ECaps2) -> Element { - let mut c = Element::builder("c") - .ns(ns::ECAPS2) - .build(); - for hash in ecaps2.hashes.clone() { - let hash_elem = (&hash).into(); - c.append_child(hash_elem); +impl<'a> Into for &'a ECaps2 { + fn into(self) -> Element { + Element::builder("c") + .ns(ns::ECAPS2) + .append(self.hashes.iter() + .map(|hash| hash.into()) + .collect::>()) + .build() } - c } fn compute_item(field: &str) -> Vec { @@ -161,8 +165,7 @@ pub fn hash_ecaps2(data: &[u8], algo: &str) -> String { #[cfg(test)] mod tests { - use minidom::Element; - use error::Error; + use super::*; use disco; use ecaps2; use base64; @@ -170,7 +173,7 @@ mod tests { #[test] fn test_parse() { let elem: Element = "K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=+sDTQqBmX6iG/X3zjt06fjZMBBqL/723knFIyRf0sg8=".parse().unwrap(); - let ecaps2 = ecaps2::parse_ecaps2(&elem).unwrap(); + let ecaps2 = ECaps2::try_from(&elem).unwrap(); assert_eq!(ecaps2.hashes.len(), 2); assert_eq!(ecaps2.hashes[0].algo, "sha-256"); assert_eq!(ecaps2.hashes[0].hash, "K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4="); @@ -181,7 +184,7 @@ mod tests { #[test] fn test_invalid_child() { let elem: Element = "K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=+sDTQqBmX6iG/X3zjt06fjZMBBqL/723knFIyRf0sg8=".parse().unwrap(); - let error = ecaps2::parse_ecaps2(&elem).unwrap_err(); + let error = ECaps2::try_from(&elem).unwrap_err(); let message = match error { Error::ParseError(string) => string, _ => panic!(), diff --git a/src/presence.rs b/src/presence.rs index de5944c..6ae97ec 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -18,7 +18,7 @@ use ns; use stanza_error; use delay::Delay; -use ecaps2; +use ecaps2::ECaps2; #[derive(Debug, Clone, PartialEq)] pub enum Show { @@ -53,7 +53,7 @@ pub enum PresencePayload { Priority(Priority), StanzaError(stanza_error::StanzaError), Delay(Delay), - ECaps2(ecaps2::ECaps2), + ECaps2(ECaps2), } #[derive(Debug, Clone, PartialEq)] @@ -183,7 +183,7 @@ pub fn parse_presence(root: &Element) -> Result { Some(PresencePayload::StanzaError(stanza_error)) } else if let Ok(delay) = Delay::try_from(elem) { Some(PresencePayload::Delay(delay)) - } else if let Ok(ecaps2) = ecaps2::parse_ecaps2(elem) { + } else if let Ok(ecaps2) = ECaps2::try_from(elem) { Some(PresencePayload::ECaps2(ecaps2)) } else { None @@ -228,7 +228,7 @@ pub fn serialise_payload(payload: &PresencePayload) -> Element { }, PresencePayload::StanzaError(ref stanza_error) => stanza_error::serialise(stanza_error), PresencePayload::Delay(ref delay) => delay.into(), - PresencePayload::ECaps2(ref ecaps2) => ecaps2::serialise(ecaps2), + PresencePayload::ECaps2(ref ecaps2) => ecaps2.into(), } }