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 delay;
use attention::Attention;
use message_correct;
use message_correct::Replace;
use eme;
/// Lists every known payload of a `<message/>`.
@ -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<Message, Error> {
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),
}
}

View file

@ -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<Replace, Error> {
if !root.is("replace", ns::MESSAGE_CORRECT) {
impl<'a> TryFrom<&'a Element> for Replace {
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."));
}
for _ in root.children() {
for _ in elem.children() {
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(),
None => return Err(Error::ParseError("No 'id' attribute present in replace.")),
};
Ok(Replace { id: id })
}
}
pub fn serialise(replace: &Replace) -> Element {
impl<'a> Into<Element> for &'a Replace {
fn into(self) -> Element {
Element::builder("replace")
.ns(ns::MESSAGE_CORRECT)
.attr("id", replace.id.clone())
.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 = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".parse().unwrap();
message_correct::parse_replace(&elem).unwrap();
Replace::try_from(&elem).unwrap();
}
#[test]
fn test_invalid_child() {
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 {
Error::ParseError(string) => string,
_ => panic!(),
@ -62,7 +68,7 @@ mod tests {
#[test]
fn test_invalid_id() {
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 {
Error::ParseError(string) => string,
_ => panic!(),
@ -73,8 +79,8 @@ mod tests {
#[test]
fn test_serialise() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".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);
}
}