diff --git a/src/message.rs b/src/message.rs index 48001452..c513abee 100644 --- a/src/message.rs +++ b/src/message.rs @@ -21,7 +21,7 @@ use chatstates; use receipts::Receipt; use delay; use attention::Attention; -use message_correct; +use message_correct::Replace; use eme; /// Lists every known payload of a ``. @@ -33,7 +33,7 @@ pub enum MessagePayload { Receipt(Receipt), Delay(delay::Delay), Attention(Attention), - MessageCorrect(message_correct::Replace), + MessageCorrect(Replace), ExplicitMessageEncryption(eme::ExplicitMessageEncryption), } @@ -123,7 +123,7 @@ pub fn parse_message(root: &Element) -> Result { Some(MessagePayload::Delay(delay)) } else if let Ok(attention) = Attention::try_from(elem) { Some(MessagePayload::Attention(attention)) - } else if let Ok(replace) = message_correct::parse_replace(elem) { + } else if let Ok(replace) = Replace::try_from(elem) { Some(MessagePayload::MessageCorrect(replace)) } else if let Ok(eme) = eme::parse_explicit_message_encryption(elem) { Some(MessagePayload::ExplicitMessageEncryption(eme)) @@ -152,7 +152,7 @@ pub fn serialise_payload(payload: &MessagePayload) -> Element { MessagePayload::ChatState(ref chatstate) => chatstates::serialise(chatstate), MessagePayload::Receipt(ref receipt) => receipt.into(), MessagePayload::Delay(ref delay) => delay::serialise(delay), - MessagePayload::MessageCorrect(ref replace) => message_correct::serialise(replace), + MessagePayload::MessageCorrect(ref replace) => replace.into(), MessagePayload::ExplicitMessageEncryption(ref eme) => eme::serialise(eme), } } diff --git a/src/message_correct.rs b/src/message_correct.rs index 27ac7e91..ecc555cc 100644 --- a/src/message_correct.rs +++ b/src/message_correct.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; @@ -15,43 +17,47 @@ pub struct Replace { pub id: String, } -pub fn parse_replace(root: &Element) -> Result { - if !root.is("replace", ns::MESSAGE_CORRECT) { - return Err(Error::ParseError("This is not a replace element.")); +impl<'a> TryFrom<&'a Element> for Replace { + type Error = Error; + + fn try_from(elem: &'a Element) -> Result { + if !elem.is("replace", ns::MESSAGE_CORRECT) { + return Err(Error::ParseError("This is not a replace element.")); + } + for _ in elem.children() { + return Err(Error::ParseError("Unknown child in replace element.")); + } + let id = match elem.attr("id") { + Some(id) => id.to_owned(), + None => return Err(Error::ParseError("No 'id' attribute present in replace.")), + }; + Ok(Replace { id: id }) } - for _ in root.children() { - return Err(Error::ParseError("Unknown child in replace element.")); - } - let id = match root.attr("id") { - Some(id) => id.to_owned(), - None => return Err(Error::ParseError("No 'id' attribute present in replace.")), - }; - Ok(Replace { id: id }) } -pub fn serialise(replace: &Replace) -> Element { - Element::builder("replace") - .ns(ns::MESSAGE_CORRECT) - .attr("id", replace.id.clone()) - .build() +impl<'a> Into for &'a Replace { + fn into(self) -> Element { + Element::builder("replace") + .ns(ns::MESSAGE_CORRECT) + .attr("id", self.id.clone()) + .build() + } } #[cfg(test)] mod tests { - use minidom::Element; - use error::Error; - use message_correct; + use super::*; #[test] fn test_simple() { let elem: Element = "".parse().unwrap(); - message_correct::parse_replace(&elem).unwrap(); + Replace::try_from(&elem).unwrap(); } #[test] fn test_invalid_child() { let elem: Element = "".parse().unwrap(); - let error = message_correct::parse_replace(&elem).unwrap_err(); + let error = Replace::try_from(&elem).unwrap_err(); let message = match error { Error::ParseError(string) => string, _ => panic!(), @@ -62,7 +68,7 @@ mod tests { #[test] fn test_invalid_id() { let elem: Element = "".parse().unwrap(); - let error = message_correct::parse_replace(&elem).unwrap_err(); + let error = Replace::try_from(&elem).unwrap_err(); let message = match error { Error::ParseError(string) => string, _ => panic!(), @@ -73,8 +79,8 @@ mod tests { #[test] fn test_serialise() { let elem: Element = "".parse().unwrap(); - let replace = message_correct::Replace { id: String::from("coucou") }; - let elem2 = message_correct::serialise(&replace); + let replace = Replace { id: String::from("coucou") }; + let elem2 = (&replace).into(); assert_eq!(elem, elem2); } }