mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
message: Switch to Into/TryFrom.
This commit is contained in:
parent
fffaca316f
commit
f971cbd5c9
2 changed files with 96 additions and 98 deletions
|
@ -11,7 +11,7 @@ use minidom::Element;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
|
||||||
use delay::Delay;
|
use delay::Delay;
|
||||||
use message;
|
use message::Message;
|
||||||
|
|
||||||
use ns;
|
use ns;
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ use ns;
|
||||||
pub struct Forwarded {
|
pub struct Forwarded {
|
||||||
pub delay: Option<Delay>,
|
pub delay: Option<Delay>,
|
||||||
// XXX: really? Option?
|
// XXX: really? Option?
|
||||||
pub stanza: Option<message::Message>,
|
pub stanza: Option<Message>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TryFrom<&'a Element> for Forwarded {
|
impl<'a> TryFrom<&'a Element> for Forwarded {
|
||||||
|
@ -35,7 +35,7 @@ impl<'a> TryFrom<&'a Element> for Forwarded {
|
||||||
if child.is("delay", ns::DELAY) {
|
if child.is("delay", ns::DELAY) {
|
||||||
delay = Some(Delay::try_from(child)?);
|
delay = Some(Delay::try_from(child)?);
|
||||||
} else if child.is("message", ns::JABBER_CLIENT) {
|
} else if child.is("message", ns::JABBER_CLIENT) {
|
||||||
stanza = Some(message::parse_message(child)?);
|
stanza = Some(Message::try_from(child)?);
|
||||||
// TODO: also handle the five other possibilities.
|
// TODO: also handle the five other possibilities.
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::ParseError("Unknown child in forwarded element."));
|
return Err(Error::ParseError("Unknown child in forwarded element."));
|
||||||
|
@ -53,7 +53,7 @@ impl<'a> Into<Element> for &'a Forwarded {
|
||||||
Element::builder("forwarded")
|
Element::builder("forwarded")
|
||||||
.ns(ns::FORWARD)
|
.ns(ns::FORWARD)
|
||||||
.append(match self.delay { Some(ref delay) => { let elem: Element = delay.into(); Some(elem) }, None => None })
|
.append(match self.delay { Some(ref delay) => { let elem: Element = delay.into(); Some(elem) }, None => None })
|
||||||
.append(self.stanza.clone())
|
.append(match self.stanza { Some(ref stanza) => { let elem: Element = stanza.into(); Some(elem) }, None => None })
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use minidom::{Element, IntoElements, IntoAttributeValue, ElementEmitter};
|
use minidom::{Element, IntoAttributeValue};
|
||||||
|
|
||||||
use jid::Jid;
|
use jid::Jid;
|
||||||
|
|
||||||
|
@ -95,7 +95,10 @@ pub struct Message {
|
||||||
pub payloads: Vec<MessagePayloadType>,
|
pub payloads: Vec<MessagePayloadType>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_message(root: &Element) -> Result<Message, Error> {
|
impl<'a> TryFrom<&'a Element> for Message {
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn try_from(root: &'a Element) -> Result<Message, Error> {
|
||||||
if !root.is("message", ns::JABBER_CLIENT) {
|
if !root.is("message", ns::JABBER_CLIENT) {
|
||||||
return Err(Error::ParseError("This is not a message element."));
|
return Err(Error::ParseError("This is not a message element."));
|
||||||
}
|
}
|
||||||
|
@ -143,9 +146,11 @@ pub fn parse_message(root: &Element) -> Result<Message, Error> {
|
||||||
payloads: payloads,
|
payloads: payloads,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn serialise_payload(payload: &MessagePayload) -> Element {
|
impl<'a> Into<Element> for &'a MessagePayload {
|
||||||
match *payload {
|
fn into(self) -> Element {
|
||||||
|
match *self {
|
||||||
MessagePayload::Body(ref body) => body::serialise(body),
|
MessagePayload::Body(ref body) => body::serialise(body),
|
||||||
MessagePayload::StanzaError(ref stanza_error) => stanza_error.into(),
|
MessagePayload::StanzaError(ref stanza_error) => stanza_error.into(),
|
||||||
MessagePayload::Attention(ref attention) => attention.into(),
|
MessagePayload::Attention(ref attention) => attention.into(),
|
||||||
|
@ -156,91 +161,84 @@ pub fn serialise_payload(payload: &MessagePayload) -> Element {
|
||||||
MessagePayload::ExplicitMessageEncryption(ref eme) => eme.into(),
|
MessagePayload::ExplicitMessageEncryption(ref eme) => eme.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn serialise(message: &Message) -> Element {
|
impl<'a> Into<Element> for &'a Message {
|
||||||
|
fn into(self) -> Element {
|
||||||
let mut stanza = Element::builder("message")
|
let mut stanza = Element::builder("message")
|
||||||
.ns(ns::JABBER_CLIENT)
|
.ns(ns::JABBER_CLIENT)
|
||||||
.attr("from", message.from.clone().and_then(|value| Some(String::from(value))))
|
.attr("from", self.from.clone().and_then(|value| Some(String::from(value))))
|
||||||
.attr("to", message.to.clone().and_then(|value| Some(String::from(value))))
|
.attr("to", self.to.clone().and_then(|value| Some(String::from(value))))
|
||||||
.attr("id", message.id.clone())
|
.attr("id", self.id.clone())
|
||||||
.attr("type", message.type_.clone())
|
.attr("type", self.type_.clone())
|
||||||
.build();
|
.build();
|
||||||
for child in message.payloads.clone() {
|
for child in self.payloads.clone() {
|
||||||
let elem = match child {
|
let elem = match child {
|
||||||
MessagePayloadType::XML(elem) => elem,
|
MessagePayloadType::XML(elem) => elem,
|
||||||
MessagePayloadType::Parsed(payload) => serialise_payload(&payload),
|
MessagePayloadType::Parsed(payload) => (&payload).into(),
|
||||||
};
|
};
|
||||||
stanza.append_child(elem);
|
stanza.append_child(elem);
|
||||||
}
|
}
|
||||||
stanza
|
stanza
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoElements for Message {
|
|
||||||
fn into_elements(self, emitter: &mut ElementEmitter) {
|
|
||||||
let elem = serialise(&self);
|
|
||||||
emitter.append_child(elem);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::str::FromStr;
|
use super::*;
|
||||||
use minidom::Element;
|
|
||||||
use jid::Jid;
|
|
||||||
use message;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap();
|
let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap();
|
||||||
let message = message::parse_message(&elem).unwrap();
|
let message = Message::try_from(&elem).unwrap();
|
||||||
assert_eq!(message.from, None);
|
assert_eq!(message.from, None);
|
||||||
assert_eq!(message.to, None);
|
assert_eq!(message.to, None);
|
||||||
assert_eq!(message.id, None);
|
assert_eq!(message.id, None);
|
||||||
assert_eq!(message.type_, message::MessageType::Normal);
|
assert_eq!(message.type_, MessageType::Normal);
|
||||||
assert!(message.payloads.is_empty());
|
assert!(message.payloads.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialise() {
|
fn test_serialise() {
|
||||||
let elem: Element = "<message xmlns='jabber:client' type='normal'/>".parse().unwrap();
|
let elem: Element = "<message xmlns='jabber:client' type='normal'/>".parse().unwrap();
|
||||||
let message = message::Message {
|
let message = Message {
|
||||||
from: None,
|
from: None,
|
||||||
to: None,
|
to: None,
|
||||||
id: None,
|
id: None,
|
||||||
type_: message::MessageType::Normal,
|
type_: MessageType::Normal,
|
||||||
payloads: vec!(),
|
payloads: vec!(),
|
||||||
};
|
};
|
||||||
let elem2 = message::serialise(&message);
|
let elem2 = (&message).into();
|
||||||
assert_eq!(elem, elem2);
|
assert_eq!(elem, elem2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_body() {
|
fn test_body() {
|
||||||
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
||||||
message::parse_message(&elem).unwrap();
|
Message::try_from(&elem).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialise_body() {
|
fn test_serialise_body() {
|
||||||
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
||||||
let message = message::Message {
|
let message = Message {
|
||||||
from: None,
|
from: None,
|
||||||
to: Some(Jid::from_str("coucou@example.org").unwrap()),
|
to: Some(Jid::from_str("coucou@example.org").unwrap()),
|
||||||
id: None,
|
id: None,
|
||||||
type_: message::MessageType::Chat,
|
type_: MessageType::Chat,
|
||||||
payloads: vec!(
|
payloads: vec!(
|
||||||
message::MessagePayloadType::Parsed(message::MessagePayload::Body("Hello world!".to_owned())),
|
MessagePayloadType::Parsed(MessagePayload::Body("Hello world!".to_owned())),
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
let elem2 = message::serialise(&message);
|
let elem2 = (&message).into();
|
||||||
assert_eq!(elem, elem2);
|
assert_eq!(elem, elem2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_attention() {
|
fn test_attention() {
|
||||||
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><attention xmlns='urn:xmpp:attention:0'/></message>".parse().unwrap();
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><attention xmlns='urn:xmpp:attention:0'/></message>".parse().unwrap();
|
||||||
let message = message::parse_message(&elem).unwrap();
|
let message = Message::try_from(&elem).unwrap();
|
||||||
let elem2 = message::serialise(&message);
|
let elem2 = (&message).into();
|
||||||
assert_eq!(elem, elem2);
|
assert_eq!(elem, elem2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue