From 151635f5fb433fa3aaea4a3614907b928c3fb254 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 6 May 2017 20:07:03 +0100 Subject: [PATCH] receipts: Switch to Into/TryFrom. --- src/message.rs | 8 +++--- src/receipts.rs | 67 ++++++++++++++++++++++++++----------------------- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/message.rs b/src/message.rs index 6fcc681..4800145 100644 --- a/src/message.rs +++ b/src/message.rs @@ -18,7 +18,7 @@ use ns; use body; use stanza_error; use chatstates; -use receipts; +use receipts::Receipt; use delay; use attention::Attention; use message_correct; @@ -30,7 +30,7 @@ pub enum MessagePayload { Body(body::Body), StanzaError(stanza_error::StanzaError), ChatState(chatstates::ChatState), - Receipt(receipts::Receipt), + Receipt(Receipt), Delay(delay::Delay), Attention(Attention), MessageCorrect(message_correct::Replace), @@ -117,7 +117,7 @@ pub fn parse_message(root: &Element) -> Result { Some(MessagePayload::StanzaError(stanza_error)) } else if let Ok(chatstate) = chatstates::parse_chatstate(elem) { 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)) } else if let Ok(delay) = delay::parse_delay(elem) { 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::Attention(ref attention) => attention.into(), 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::MessageCorrect(ref replace) => message_correct::serialise(replace), MessagePayload::ExplicitMessageEncryption(ref eme) => eme::serialise(eme), diff --git a/src/receipts.rs b/src/receipts.rs index 3dc3b78..27392fb 100644 --- a/src/receipts.rs +++ b/src/receipts.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,59 +18,62 @@ pub enum Receipt { Received(String), } -pub fn parse_receipt(root: &Element) -> Result { - for _ in root.children() { - return Err(Error::ParseError("Unknown child in receipt element.")); - } - if root.is("request", ns::RECEIPTS) { - Ok(Receipt::Request) - } else if root.is("received", ns::RECEIPTS) { - let id = root.attr("id").unwrap_or("").to_owned(); - Ok(Receipt::Received(id)) - } else { - Err(Error::ParseError("This is not a receipt element.")) +impl<'a> TryFrom<&'a Element> for Receipt { + type Error = Error; + + fn try_from(elem: &'a Element) -> Result { + for _ in elem.children() { + return Err(Error::ParseError("Unknown child in receipt element.")); + } + if elem.is("request", ns::RECEIPTS) { + Ok(Receipt::Request) + } else if elem.is("received", ns::RECEIPTS) { + let id = elem.attr("id").unwrap_or("").to_owned(); + Ok(Receipt::Received(id)) + } else { + Err(Error::ParseError("This is not a receipt element.")) + } } } -pub fn serialise(receipt: &Receipt) -> Element { - match *receipt { - Receipt::Request => Element::builder("request") - .ns(ns::RECEIPTS) - .build(), - Receipt::Received(ref id) => Element::builder("received") - .ns(ns::RECEIPTS) - .attr("id", id.clone()) - .build(), +impl<'a> Into for &'a Receipt { + fn into(self) -> Element { + match *self { + Receipt::Request => Element::builder("request") + .ns(ns::RECEIPTS) + .build(), + Receipt::Received(ref id) => Element::builder("received") + .ns(ns::RECEIPTS) + .attr("id", id.clone()) + .build(), + } } } #[cfg(test)] mod tests { - use minidom::Element; - //use error::Error; - use receipts; - use ns; + use super::*; #[test] fn test_simple() { let elem: Element = "".parse().unwrap(); - receipts::parse_receipt(&elem).unwrap(); + Receipt::try_from(&elem).unwrap(); let elem: Element = "".parse().unwrap(); - receipts::parse_receipt(&elem).unwrap(); + Receipt::try_from(&elem).unwrap(); let elem: Element = "".parse().unwrap(); - receipts::parse_receipt(&elem).unwrap(); + Receipt::try_from(&elem).unwrap(); } #[test] fn test_serialise() { - let receipt = receipts::Receipt::Request; - let elem = receipts::serialise(&receipt); + let receipt = Receipt::Request; + let elem: Element = (&receipt).into(); assert!(elem.is("request", ns::RECEIPTS)); - let receipt = receipts::Receipt::Received("coucou".to_owned()); - let elem = receipts::serialise(&receipt); + let receipt = Receipt::Received("coucou".to_owned()); + let elem: Element = (&receipt).into(); assert!(elem.is("received", ns::RECEIPTS)); assert_eq!(elem.attr("id"), Some("coucou")); }