mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
delay: Switch to Into/TryFrom.
This commit is contained in:
parent
e451520187
commit
0f58e650b7
4 changed files with 55 additions and 54 deletions
58
src/delay.rs
58
src/delay.rs
|
@ -4,7 +4,9 @@
|
||||||
// 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 minidom::{Element, IntoElements, ElementEmitter};
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
use minidom::Element;
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use jid::Jid;
|
use jid::Jid;
|
||||||
|
@ -18,16 +20,19 @@ pub struct Delay {
|
||||||
pub data: Option<String>,
|
pub data: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_delay(root: &Element) -> Result<Delay, Error> {
|
impl<'a> TryFrom<&'a Element> for Delay {
|
||||||
if !root.is("delay", ns::DELAY) {
|
type Error = Error;
|
||||||
|
|
||||||
|
fn try_from(elem: &'a Element) -> Result<Delay, Error> {
|
||||||
|
if !elem.is("delay", ns::DELAY) {
|
||||||
return Err(Error::ParseError("This is not a delay element."));
|
return Err(Error::ParseError("This is not a delay element."));
|
||||||
}
|
}
|
||||||
for _ in root.children() {
|
for _ in elem.children() {
|
||||||
return Err(Error::ParseError("Unknown child in delay element."));
|
return Err(Error::ParseError("Unknown child in delay element."));
|
||||||
}
|
}
|
||||||
let from = root.attr("from").and_then(|value| value.parse().ok());
|
let from = elem.attr("from").and_then(|value| value.parse().ok());
|
||||||
let stamp = root.attr("stamp").ok_or(Error::ParseError("Mandatory argument 'stamp' not present in delay element."))?.to_owned();
|
let stamp = elem.attr("stamp").ok_or(Error::ParseError("Mandatory argument 'stamp' not present in delay element."))?.to_owned();
|
||||||
let data = match root.text().as_ref() {
|
let data = match elem.text().as_ref() {
|
||||||
"" => None,
|
"" => None,
|
||||||
text => Some(text.to_owned()),
|
text => Some(text.to_owned()),
|
||||||
};
|
};
|
||||||
|
@ -37,35 +42,28 @@ pub fn parse_delay(root: &Element) -> Result<Delay, Error> {
|
||||||
data: data,
|
data: data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialise(delay: &Delay) -> Element {
|
|
||||||
Element::builder("delay")
|
|
||||||
.ns(ns::DELAY)
|
|
||||||
.attr("from", delay.from.clone().and_then(|value| Some(String::from(value))))
|
|
||||||
.attr("stamp", delay.stamp.clone())
|
|
||||||
.append(delay.data.clone())
|
|
||||||
.build()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoElements for Delay {
|
impl<'a> Into<Element> for &'a Delay {
|
||||||
fn into_elements(self, emitter: &mut ElementEmitter) {
|
fn into(self) -> Element {
|
||||||
let elem = serialise(&self);
|
Element::builder("delay")
|
||||||
emitter.append_child(elem)
|
.ns(ns::DELAY)
|
||||||
|
.attr("from", self.from.clone().and_then(|value| Some(String::from(value))))
|
||||||
|
.attr("stamp", self.stamp.clone())
|
||||||
|
.append(self.data.clone())
|
||||||
|
.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use minidom::Element;
|
use super::*;
|
||||||
use error::Error;
|
|
||||||
use jid::Jid;
|
|
||||||
use delay;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
let elem: Element = "<delay xmlns='urn:xmpp:delay' from='capulet.com' stamp='2002-09-10T23:08:25Z'/>".parse().unwrap();
|
let elem: Element = "<delay xmlns='urn:xmpp:delay' from='capulet.com' stamp='2002-09-10T23:08:25Z'/>".parse().unwrap();
|
||||||
let delay = delay::parse_delay(&elem).unwrap();
|
let delay = Delay::try_from(&elem).unwrap();
|
||||||
assert_eq!(delay.from, Some(Jid::from_str("capulet.com").unwrap()));
|
assert_eq!(delay.from, Some(Jid::from_str("capulet.com").unwrap()));
|
||||||
assert_eq!(delay.stamp, "2002-09-10T23:08:25Z");
|
assert_eq!(delay.stamp, "2002-09-10T23:08:25Z");
|
||||||
assert_eq!(delay.data, None);
|
assert_eq!(delay.data, None);
|
||||||
|
@ -74,7 +72,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unknown() {
|
fn test_unknown() {
|
||||||
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
|
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
|
||||||
let error = delay::parse_delay(&elem).unwrap_err();
|
let error = Delay::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -85,7 +83,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_child() {
|
fn test_invalid_child() {
|
||||||
let elem: Element = "<delay xmlns='urn:xmpp:delay'><coucou/></delay>".parse().unwrap();
|
let elem: Element = "<delay xmlns='urn:xmpp:delay'><coucou/></delay>".parse().unwrap();
|
||||||
let error = delay::parse_delay(&elem).unwrap_err();
|
let error = Delay::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -96,24 +94,24 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialise() {
|
fn test_serialise() {
|
||||||
let elem: Element = "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25Z'/>".parse().unwrap();
|
let elem: Element = "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25Z'/>".parse().unwrap();
|
||||||
let delay = delay::Delay {
|
let delay = Delay {
|
||||||
from: None,
|
from: None,
|
||||||
stamp: "2002-09-10T23:08:25Z".to_owned(),
|
stamp: "2002-09-10T23:08:25Z".to_owned(),
|
||||||
data: None,
|
data: None,
|
||||||
};
|
};
|
||||||
let elem2 = delay::serialise(&delay);
|
let elem2 = (&delay).into();
|
||||||
assert_eq!(elem, elem2);
|
assert_eq!(elem, elem2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialise_data() {
|
fn test_serialise_data() {
|
||||||
let elem: Element = "<delay xmlns='urn:xmpp:delay' from='juliet@example.org' stamp='2002-09-10T23:08:25Z'>Reason</delay>".parse().unwrap();
|
let elem: Element = "<delay xmlns='urn:xmpp:delay' from='juliet@example.org' stamp='2002-09-10T23:08:25Z'>Reason</delay>".parse().unwrap();
|
||||||
let delay = delay::Delay {
|
let delay = Delay {
|
||||||
from: Some(Jid::from_str("juliet@example.org").unwrap()),
|
from: Some(Jid::from_str("juliet@example.org").unwrap()),
|
||||||
stamp: "2002-09-10T23:08:25Z".to_owned(),
|
stamp: "2002-09-10T23:08:25Z".to_owned(),
|
||||||
data: Some(String::from("Reason")),
|
data: Some(String::from("Reason")),
|
||||||
};
|
};
|
||||||
let elem2 = delay::serialise(&delay);
|
let elem2 = (&delay).into();
|
||||||
assert_eq!(elem, elem2);
|
assert_eq!(elem, elem2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,18 +4,20 @@
|
||||||
// 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;
|
||||||
|
|
||||||
use delay;
|
use delay::Delay;
|
||||||
use message;
|
use message;
|
||||||
|
|
||||||
use ns;
|
use ns;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Forwarded {
|
pub struct Forwarded {
|
||||||
pub delay: Option<delay::Delay>,
|
pub delay: Option<Delay>,
|
||||||
// XXX: really? Option?
|
// XXX: really? Option?
|
||||||
pub stanza: Option<message::Message>,
|
pub stanza: Option<message::Message>,
|
||||||
}
|
}
|
||||||
|
@ -28,7 +30,7 @@ pub fn parse_forwarded(root: &Element) -> Result<Forwarded, Error> {
|
||||||
let mut stanza = None;
|
let mut stanza = None;
|
||||||
for child in root.children() {
|
for child in root.children() {
|
||||||
if child.is("delay", ns::DELAY) {
|
if child.is("delay", ns::DELAY) {
|
||||||
delay = Some(delay::parse_delay(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::parse_message(child)?);
|
||||||
// TODO: also handle the five other possibilities.
|
// TODO: also handle the five other possibilities.
|
||||||
|
@ -45,7 +47,7 @@ pub fn parse_forwarded(root: &Element) -> Result<Forwarded, Error> {
|
||||||
pub fn serialise(forwarded: &Forwarded) -> Element {
|
pub fn serialise(forwarded: &Forwarded) -> Element {
|
||||||
Element::builder("forwarded")
|
Element::builder("forwarded")
|
||||||
.ns(ns::FORWARD)
|
.ns(ns::FORWARD)
|
||||||
.append(forwarded.delay.clone())
|
.append(match forwarded.delay { Some(ref delay) => { let elem: Element = delay.into(); Some(elem) }, None => None })
|
||||||
.append(forwarded.stanza.clone())
|
.append(forwarded.stanza.clone())
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ use body;
|
||||||
use stanza_error;
|
use stanza_error;
|
||||||
use chatstates::ChatState;
|
use chatstates::ChatState;
|
||||||
use receipts::Receipt;
|
use receipts::Receipt;
|
||||||
use delay;
|
use delay::Delay;
|
||||||
use attention::Attention;
|
use attention::Attention;
|
||||||
use message_correct::Replace;
|
use message_correct::Replace;
|
||||||
use eme;
|
use eme;
|
||||||
|
@ -31,7 +31,7 @@ pub enum MessagePayload {
|
||||||
StanzaError(stanza_error::StanzaError),
|
StanzaError(stanza_error::StanzaError),
|
||||||
ChatState(ChatState),
|
ChatState(ChatState),
|
||||||
Receipt(Receipt),
|
Receipt(Receipt),
|
||||||
Delay(delay::Delay),
|
Delay(Delay),
|
||||||
Attention(Attention),
|
Attention(Attention),
|
||||||
MessageCorrect(Replace),
|
MessageCorrect(Replace),
|
||||||
ExplicitMessageEncryption(eme::ExplicitMessageEncryption),
|
ExplicitMessageEncryption(eme::ExplicitMessageEncryption),
|
||||||
|
@ -119,7 +119,7 @@ pub fn parse_message(root: &Element) -> Result<Message, Error> {
|
||||||
Some(MessagePayload::ChatState(chatstate))
|
Some(MessagePayload::ChatState(chatstate))
|
||||||
} else if let Ok(receipt) = Receipt::try_from(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::try_from(elem) {
|
||||||
Some(MessagePayload::Delay(delay))
|
Some(MessagePayload::Delay(delay))
|
||||||
} else if let Ok(attention) = Attention::try_from(elem) {
|
} else if let Ok(attention) = Attention::try_from(elem) {
|
||||||
Some(MessagePayload::Attention(attention))
|
Some(MessagePayload::Attention(attention))
|
||||||
|
@ -151,7 +151,7 @@ pub fn serialise_payload(payload: &MessagePayload) -> Element {
|
||||||
MessagePayload::Attention(ref attention) => attention.into(),
|
MessagePayload::Attention(ref attention) => attention.into(),
|
||||||
MessagePayload::ChatState(ref chatstate) => chatstate.into(),
|
MessagePayload::ChatState(ref chatstate) => chatstate.into(),
|
||||||
MessagePayload::Receipt(ref receipt) => receipt.into(),
|
MessagePayload::Receipt(ref receipt) => receipt.into(),
|
||||||
MessagePayload::Delay(ref delay) => delay::serialise(delay),
|
MessagePayload::Delay(ref delay) => delay.into(),
|
||||||
MessagePayload::MessageCorrect(ref replace) => replace.into(),
|
MessagePayload::MessageCorrect(ref replace) => replace.into(),
|
||||||
MessagePayload::ExplicitMessageEncryption(ref eme) => eme::serialise(eme),
|
MessagePayload::ExplicitMessageEncryption(ref eme) => eme::serialise(eme),
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// 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 std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
@ -16,7 +17,7 @@ use error::Error;
|
||||||
use ns;
|
use ns;
|
||||||
|
|
||||||
use stanza_error;
|
use stanza_error;
|
||||||
use delay;
|
use delay::Delay;
|
||||||
use ecaps2;
|
use ecaps2;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
@ -51,7 +52,7 @@ pub enum PresencePayload {
|
||||||
Status(Status),
|
Status(Status),
|
||||||
Priority(Priority),
|
Priority(Priority),
|
||||||
StanzaError(stanza_error::StanzaError),
|
StanzaError(stanza_error::StanzaError),
|
||||||
Delay(delay::Delay),
|
Delay(Delay),
|
||||||
ECaps2(ecaps2::ECaps2),
|
ECaps2(ecaps2::ECaps2),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,7 +181,7 @@ pub fn parse_presence(root: &Element) -> Result<Presence, Error> {
|
||||||
} else {
|
} else {
|
||||||
let payload = if let Ok(stanza_error) = stanza_error::parse_stanza_error(elem) {
|
let payload = if let Ok(stanza_error) = stanza_error::parse_stanza_error(elem) {
|
||||||
Some(PresencePayload::StanzaError(stanza_error))
|
Some(PresencePayload::StanzaError(stanza_error))
|
||||||
} else if let Ok(delay) = delay::parse_delay(elem) {
|
} else if let Ok(delay) = Delay::try_from(elem) {
|
||||||
Some(PresencePayload::Delay(delay))
|
Some(PresencePayload::Delay(delay))
|
||||||
} else if let Ok(ecaps2) = ecaps2::parse_ecaps2(elem) {
|
} else if let Ok(ecaps2) = ecaps2::parse_ecaps2(elem) {
|
||||||
Some(PresencePayload::ECaps2(ecaps2))
|
Some(PresencePayload::ECaps2(ecaps2))
|
||||||
|
@ -226,7 +227,7 @@ pub fn serialise_payload(payload: &PresencePayload) -> Element {
|
||||||
.build()
|
.build()
|
||||||
},
|
},
|
||||||
PresencePayload::StanzaError(ref stanza_error) => stanza_error::serialise(stanza_error),
|
PresencePayload::StanzaError(ref stanza_error) => stanza_error::serialise(stanza_error),
|
||||||
PresencePayload::Delay(ref delay) => delay::serialise(delay),
|
PresencePayload::Delay(ref delay) => delay.into(),
|
||||||
PresencePayload::ECaps2(ref ecaps2) => ecaps2::serialise(ecaps2),
|
PresencePayload::ECaps2(ref ecaps2) => ecaps2::serialise(ecaps2),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue