message_correct: Switch to Into/TryFrom.

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-06 20:16:45 +01:00
parent de8fe4bf02
commit 4f11a067d8
2 changed files with 34 additions and 28 deletions

View file

@ -21,7 +21,7 @@ use chatstates;
use receipts::Receipt; use receipts::Receipt;
use delay; use delay;
use attention::Attention; use attention::Attention;
use message_correct; use message_correct::Replace;
use eme; use eme;
/// Lists every known payload of a `<message/>`. /// Lists every known payload of a `<message/>`.
@ -33,7 +33,7 @@ pub enum MessagePayload {
Receipt(Receipt), Receipt(Receipt),
Delay(delay::Delay), Delay(delay::Delay),
Attention(Attention), Attention(Attention),
MessageCorrect(message_correct::Replace), MessageCorrect(Replace),
ExplicitMessageEncryption(eme::ExplicitMessageEncryption), ExplicitMessageEncryption(eme::ExplicitMessageEncryption),
} }
@ -123,7 +123,7 @@ pub fn parse_message(root: &Element) -> Result<Message, Error> {
Some(MessagePayload::Delay(delay)) Some(MessagePayload::Delay(delay))
} else if let Ok(attention) = Attention::try_from(elem) { } else if let Ok(attention) = Attention::try_from(elem) {
Some(MessagePayload::Attention(attention)) 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)) Some(MessagePayload::MessageCorrect(replace))
} else if let Ok(eme) = eme::parse_explicit_message_encryption(elem) { } else if let Ok(eme) = eme::parse_explicit_message_encryption(elem) {
Some(MessagePayload::ExplicitMessageEncryption(eme)) Some(MessagePayload::ExplicitMessageEncryption(eme))
@ -152,7 +152,7 @@ pub fn serialise_payload(payload: &MessagePayload) -> Element {
MessagePayload::ChatState(ref chatstate) => chatstates::serialise(chatstate), MessagePayload::ChatState(ref chatstate) => chatstates::serialise(chatstate),
MessagePayload::Receipt(ref receipt) => receipt.into(), MessagePayload::Receipt(ref receipt) => receipt.into(),
MessagePayload::Delay(ref delay) => delay::serialise(delay), 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), MessagePayload::ExplicitMessageEncryption(ref eme) => eme::serialise(eme),
} }
} }

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 error::Error; use error::Error;
@ -15,43 +17,47 @@ pub struct Replace {
pub id: String, pub id: String,
} }
pub fn parse_replace(root: &Element) -> Result<Replace, Error> { impl<'a> TryFrom<&'a Element> for Replace {
if !root.is("replace", ns::MESSAGE_CORRECT) { type Error = Error;
fn try_from(elem: &'a Element) -> Result<Replace, Error> {
if !elem.is("replace", ns::MESSAGE_CORRECT) {
return Err(Error::ParseError("This is not a replace element.")); return Err(Error::ParseError("This is not a replace element."));
} }
for _ in root.children() { for _ in elem.children() {
return Err(Error::ParseError("Unknown child in replace element.")); return Err(Error::ParseError("Unknown child in replace 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 replace.")), None => return Err(Error::ParseError("No 'id' attribute present in replace.")),
}; };
Ok(Replace { id: id }) Ok(Replace { id: id })
} }
}
pub fn serialise(replace: &Replace) -> Element { impl<'a> Into<Element> for &'a Replace {
fn into(self) -> Element {
Element::builder("replace") Element::builder("replace")
.ns(ns::MESSAGE_CORRECT) .ns(ns::MESSAGE_CORRECT)
.attr("id", replace.id.clone()) .attr("id", self.id.clone())
.build() .build()
} }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use minidom::Element; use super::*;
use error::Error;
use message_correct;
#[test] #[test]
fn test_simple() { fn test_simple() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".parse().unwrap(); let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".parse().unwrap();
message_correct::parse_replace(&elem).unwrap(); Replace::try_from(&elem).unwrap();
} }
#[test] #[test]
fn test_invalid_child() { fn test_invalid_child() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'><coucou/></replace>".parse().unwrap(); let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'><coucou/></replace>".parse().unwrap();
let error = message_correct::parse_replace(&elem).unwrap_err(); let error = Replace::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -62,7 +68,7 @@ mod tests {
#[test] #[test]
fn test_invalid_id() { fn test_invalid_id() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap(); let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
let error = message_correct::parse_replace(&elem).unwrap_err(); let error = Replace::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -73,8 +79,8 @@ mod tests {
#[test] #[test]
fn test_serialise() { fn test_serialise() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".parse().unwrap(); let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".parse().unwrap();
let replace = message_correct::Replace { id: String::from("coucou") }; let replace = Replace { id: String::from("coucou") };
let elem2 = message_correct::serialise(&replace); let elem2 = (&replace).into();
assert_eq!(elem, elem2); assert_eq!(elem, elem2);
} }
} }