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 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),
|
||||||
|
|
|
@ -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,59 +18,62 @@ 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;
|
||||||
return Err(Error::ParseError("Unknown child in receipt element."));
|
|
||||||
}
|
fn try_from(elem: &'a Element) -> Result<Receipt, Error> {
|
||||||
if root.is("request", ns::RECEIPTS) {
|
for _ in elem.children() {
|
||||||
Ok(Receipt::Request)
|
return Err(Error::ParseError("Unknown child in receipt element."));
|
||||||
} else if root.is("received", ns::RECEIPTS) {
|
}
|
||||||
let id = root.attr("id").unwrap_or("").to_owned();
|
if elem.is("request", ns::RECEIPTS) {
|
||||||
Ok(Receipt::Received(id))
|
Ok(Receipt::Request)
|
||||||
} else {
|
} else if elem.is("received", ns::RECEIPTS) {
|
||||||
Err(Error::ParseError("This is not a receipt element."))
|
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 {
|
impl<'a> Into<Element> for &'a Receipt {
|
||||||
match *receipt {
|
fn into(self) -> Element {
|
||||||
Receipt::Request => Element::builder("request")
|
match *self {
|
||||||
.ns(ns::RECEIPTS)
|
Receipt::Request => Element::builder("request")
|
||||||
.build(),
|
.ns(ns::RECEIPTS)
|
||||||
Receipt::Received(ref id) => Element::builder("received")
|
.build(),
|
||||||
.ns(ns::RECEIPTS)
|
Receipt::Received(ref id) => Element::builder("received")
|
||||||
.attr("id", id.clone())
|
.ns(ns::RECEIPTS)
|
||||||
.build(),
|
.attr("id", id.clone())
|
||||||
|
.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"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue