receipts: Switch to Into/TryFrom.

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-06 20:07:03 +01:00
parent f99c667eab
commit 151635f5fb
2 changed files with 40 additions and 35 deletions

View file

@ -18,7 +18,7 @@ use ns;
use body; use body;
use stanza_error; use stanza_error;
use chatstates; use chatstates;
use receipts; use receipts::Receipt;
use delay; use delay;
use attention::Attention; use attention::Attention;
use message_correct; use message_correct;
@ -30,7 +30,7 @@ pub enum MessagePayload {
Body(body::Body), Body(body::Body),
StanzaError(stanza_error::StanzaError), StanzaError(stanza_error::StanzaError),
ChatState(chatstates::ChatState), ChatState(chatstates::ChatState),
Receipt(receipts::Receipt), Receipt(Receipt),
Delay(delay::Delay), Delay(delay::Delay),
Attention(Attention), Attention(Attention),
MessageCorrect(message_correct::Replace), MessageCorrect(message_correct::Replace),
@ -117,7 +117,7 @@ pub fn parse_message(root: &Element) -> Result<Message, Error> {
Some(MessagePayload::StanzaError(stanza_error)) Some(MessagePayload::StanzaError(stanza_error))
} else if let Ok(chatstate) = chatstates::parse_chatstate(elem) { } else if let Ok(chatstate) = chatstates::parse_chatstate(elem) {
Some(MessagePayload::ChatState(chatstate)) Some(MessagePayload::ChatState(chatstate))
} else if let Ok(receipt) = receipts::parse_receipt(elem) { } else if let Ok(receipt) = Receipt::try_from(elem) {
Some(MessagePayload::Receipt(receipt)) Some(MessagePayload::Receipt(receipt))
} else if let Ok(delay) = delay::parse_delay(elem) { } else if let Ok(delay) = delay::parse_delay(elem) {
Some(MessagePayload::Delay(delay)) Some(MessagePayload::Delay(delay))
@ -150,7 +150,7 @@ pub fn serialise_payload(payload: &MessagePayload) -> Element {
MessagePayload::StanzaError(ref stanza_error) => stanza_error::serialise(stanza_error), MessagePayload::StanzaError(ref stanza_error) => stanza_error::serialise(stanza_error),
MessagePayload::Attention(ref attention) => attention.into(), MessagePayload::Attention(ref attention) => attention.into(),
MessagePayload::ChatState(ref chatstate) => chatstates::serialise(chatstate), MessagePayload::ChatState(ref chatstate) => chatstates::serialise(chatstate),
MessagePayload::Receipt(ref receipt) => receipts::serialise(receipt), 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) => message_correct::serialise(replace),
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;
@ -16,22 +18,27 @@ pub enum Receipt {
Received(String), Received(String),
} }
pub fn parse_receipt(root: &Element) -> Result<Receipt, Error> { impl<'a> TryFrom<&'a Element> for Receipt {
for _ in root.children() { type Error = Error;
fn try_from(elem: &'a Element) -> Result<Receipt, Error> {
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in receipt element.")); return Err(Error::ParseError("Unknown child in receipt element."));
} }
if root.is("request", ns::RECEIPTS) { if elem.is("request", ns::RECEIPTS) {
Ok(Receipt::Request) Ok(Receipt::Request)
} else if root.is("received", ns::RECEIPTS) { } else if elem.is("received", ns::RECEIPTS) {
let id = root.attr("id").unwrap_or("").to_owned(); let id = elem.attr("id").unwrap_or("").to_owned();
Ok(Receipt::Received(id)) Ok(Receipt::Received(id))
} else { } else {
Err(Error::ParseError("This is not a receipt element.")) Err(Error::ParseError("This is not a receipt element."))
} }
} }
}
pub fn serialise(receipt: &Receipt) -> Element { impl<'a> Into<Element> for &'a Receipt {
match *receipt { fn into(self) -> Element {
match *self {
Receipt::Request => Element::builder("request") Receipt::Request => Element::builder("request")
.ns(ns::RECEIPTS) .ns(ns::RECEIPTS)
.build(), .build(),
@ -41,34 +48,32 @@ pub fn serialise(receipt: &Receipt) -> Element {
.build(), .build(),
} }
} }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use minidom::Element; use super::*;
//use error::Error;
use receipts;
use ns;
#[test] #[test]
fn test_simple() { fn test_simple() {
let elem: Element = "<request xmlns='urn:xmpp:receipts'/>".parse().unwrap(); let elem: Element = "<request xmlns='urn:xmpp:receipts'/>".parse().unwrap();
receipts::parse_receipt(&elem).unwrap(); Receipt::try_from(&elem).unwrap();
let elem: Element = "<received xmlns='urn:xmpp:receipts'/>".parse().unwrap(); let elem: Element = "<received xmlns='urn:xmpp:receipts'/>".parse().unwrap();
receipts::parse_receipt(&elem).unwrap(); Receipt::try_from(&elem).unwrap();
let elem: Element = "<received xmlns='urn:xmpp:receipts' id='coucou'/>".parse().unwrap(); let elem: Element = "<received xmlns='urn:xmpp:receipts' id='coucou'/>".parse().unwrap();
receipts::parse_receipt(&elem).unwrap(); Receipt::try_from(&elem).unwrap();
} }
#[test] #[test]
fn test_serialise() { fn test_serialise() {
let receipt = receipts::Receipt::Request; let receipt = Receipt::Request;
let elem = receipts::serialise(&receipt); let elem: Element = (&receipt).into();
assert!(elem.is("request", ns::RECEIPTS)); assert!(elem.is("request", ns::RECEIPTS));
let receipt = receipts::Receipt::Received("coucou".to_owned()); let receipt = Receipt::Received("coucou".to_owned());
let elem = receipts::serialise(&receipt); let elem: Element = (&receipt).into();
assert!(elem.is("received", ns::RECEIPTS)); assert!(elem.is("received", ns::RECEIPTS));
assert_eq!(elem.attr("id"), Some("coucou")); assert_eq!(elem.attr("id"), Some("coucou"));
} }