mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
receipts: Switch to Into/TryFrom.
This commit is contained in:
parent
f99c667eab
commit
151635f5fb
2 changed files with 40 additions and 35 deletions
|
@ -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<Message, Error> {
|
|||
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),
|
||||
|
|
|
@ -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<Receipt, Error> {
|
||||
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<Receipt, Error> {
|
||||
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<Element> 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 = "<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();
|
||||
receipts::parse_receipt(&elem).unwrap();
|
||||
Receipt::try_from(&elem).unwrap();
|
||||
|
||||
let elem: Element = "<received xmlns='urn:xmpp:receipts' id='coucou'/>".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"));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue