From 2b96751e5fd59fece078cca80282b28a9f3dd71f Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 6 May 2017 21:03:42 +0100 Subject: [PATCH] eme: Switch to Into/TryFrom. --- src/eme.rs | 60 +++++++++++++++++++++++++++----------------------- src/message.rs | 8 +++---- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/eme.rs b/src/eme.rs index 879ddda..ad28afc 100644 --- a/src/eme.rs +++ b/src/eme.rs @@ -4,6 +4,8 @@ // 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/. +use std::convert::TryFrom; + use minidom::Element; use error::Error; @@ -16,44 +18,48 @@ pub struct ExplicitMessageEncryption { pub name: Option, } -pub fn parse_explicit_message_encryption(root: &Element) -> Result { - if !root.is("encryption", ns::EME) { - return Err(Error::ParseError("This is not an encryption element.")); +impl<'a> TryFrom<&'a Element> for ExplicitMessageEncryption { + type Error = Error; + + fn try_from(elem: &'a Element) -> Result { + if !elem.is("encryption", ns::EME) { + return Err(Error::ParseError("This is not an encryption element.")); + } + for _ in elem.children() { + return Err(Error::ParseError("Unknown child in encryption element.")); + } + let namespace = elem.attr("namespace").ok_or(Error::ParseError("Mandatory argument 'namespace' not present in encryption element."))?.to_owned(); + let name = elem.attr("name").and_then(|value| value.parse().ok()); + Ok(ExplicitMessageEncryption { + namespace: namespace, + name: name, + }) } - for _ in root.children() { - return Err(Error::ParseError("Unknown child in encryption element.")); - } - let namespace = root.attr("namespace").ok_or(Error::ParseError("Mandatory argument 'namespace' not present in encryption element."))?.to_owned(); - let name = root.attr("name").and_then(|value| value.parse().ok()); - Ok(ExplicitMessageEncryption { - namespace: namespace, - name: name, - }) } -pub fn serialise(eme: &ExplicitMessageEncryption) -> Element { - Element::builder("encryption") - .ns(ns::EME) - .attr("namespace", eme.namespace.clone()) - .attr("name", eme.name.clone()) - .build() +impl<'a> Into for &'a ExplicitMessageEncryption { + fn into(self) -> Element { + Element::builder("encryption") + .ns(ns::EME) + .attr("namespace", self.namespace.clone()) + .attr("name", self.name.clone()) + .build() + } } #[cfg(test)] mod tests { - use minidom::Element; - use error::Error; - use eme; + use super::*; #[test] fn test_simple() { let elem: Element = "".parse().unwrap(); - let encryption = eme::parse_explicit_message_encryption(&elem).unwrap(); + let encryption = ExplicitMessageEncryption::try_from(&elem).unwrap(); assert_eq!(encryption.namespace, "urn:xmpp:otr:0"); assert_eq!(encryption.name, None); let elem: Element = "".parse().unwrap(); - let encryption = eme::parse_explicit_message_encryption(&elem).unwrap(); + let encryption = ExplicitMessageEncryption::try_from(&elem).unwrap(); assert_eq!(encryption.namespace, "some.unknown.mechanism"); assert_eq!(encryption.name, Some(String::from("SuperMechanism"))); } @@ -61,7 +67,7 @@ mod tests { #[test] fn test_unknown() { let elem: Element = "".parse().unwrap(); - let error = eme::parse_explicit_message_encryption(&elem).unwrap_err(); + let error = ExplicitMessageEncryption::try_from(&elem).unwrap_err(); let message = match error { Error::ParseError(string) => string, _ => panic!(), @@ -72,7 +78,7 @@ mod tests { #[test] fn test_invalid_child() { let elem: Element = "".parse().unwrap(); - let error = eme::parse_explicit_message_encryption(&elem).unwrap_err(); + let error = ExplicitMessageEncryption::try_from(&elem).unwrap_err(); let message = match error { Error::ParseError(string) => string, _ => panic!(), @@ -83,8 +89,8 @@ mod tests { #[test] fn test_serialise() { let elem: Element = "".parse().unwrap(); - let eme = eme::ExplicitMessageEncryption { namespace: String::from("coucou"), name: None }; - let elem2 = eme::serialise(&eme); + let eme = ExplicitMessageEncryption { namespace: String::from("coucou"), name: None }; + let elem2 = (&eme).into(); assert_eq!(elem, elem2); } } diff --git a/src/message.rs b/src/message.rs index 476920a..ce6622a 100644 --- a/src/message.rs +++ b/src/message.rs @@ -22,7 +22,7 @@ use receipts::Receipt; use delay::Delay; use attention::Attention; use message_correct::Replace; -use eme; +use eme::ExplicitMessageEncryption; /// Lists every known payload of a ``. #[derive(Debug, Clone)] @@ -34,7 +34,7 @@ pub enum MessagePayload { Delay(Delay), Attention(Attention), MessageCorrect(Replace), - ExplicitMessageEncryption(eme::ExplicitMessageEncryption), + ExplicitMessageEncryption(ExplicitMessageEncryption), } #[derive(Debug, Clone, PartialEq)] @@ -125,7 +125,7 @@ pub fn parse_message(root: &Element) -> Result { Some(MessagePayload::Attention(attention)) } else if let Ok(replace) = Replace::try_from(elem) { Some(MessagePayload::MessageCorrect(replace)) - } else if let Ok(eme) = eme::parse_explicit_message_encryption(elem) { + } else if let Ok(eme) = ExplicitMessageEncryption::try_from(elem) { Some(MessagePayload::ExplicitMessageEncryption(eme)) } else { None @@ -153,7 +153,7 @@ pub fn serialise_payload(payload: &MessagePayload) -> Element { MessagePayload::Receipt(ref receipt) => receipt.into(), MessagePayload::Delay(ref delay) => delay.into(), MessagePayload::MessageCorrect(ref replace) => replace.into(), - MessagePayload::ExplicitMessageEncryption(ref eme) => eme::serialise(eme), + MessagePayload::ExplicitMessageEncryption(ref eme) => eme.into(), } }