attention: Replace parse_* and serialise with TryFrom<Element> and Into<Element>.
This commit is contained in:
parent
c0b7c9da88
commit
765e8c3333
3 changed files with 34 additions and 20 deletions
|
|
@ -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;
|
||||||
|
|
@ -13,38 +15,45 @@ use ns;
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Attention;
|
pub struct Attention;
|
||||||
|
|
||||||
pub fn parse_attention(root: &Element) -> Result<Attention, Error> {
|
impl<'a> TryFrom<&'a Element> for Attention {
|
||||||
if !root.is("attention", ns::ATTENTION) {
|
type Error = Error;
|
||||||
return Err(Error::ParseError("This is not an attention element."));
|
|
||||||
|
fn try_from(elem: &'a Element) -> Result<Attention, Error> {
|
||||||
|
if !elem.is("attention", ns::ATTENTION) {
|
||||||
|
return Err(Error::ParseError("This is not an attention element."));
|
||||||
|
}
|
||||||
|
for _ in elem.children() {
|
||||||
|
return Err(Error::ParseError("Unknown child in attention element."));
|
||||||
|
}
|
||||||
|
Ok(Attention)
|
||||||
}
|
}
|
||||||
for _ in root.children() {
|
|
||||||
return Err(Error::ParseError("Unknown child in attention element."));
|
|
||||||
}
|
|
||||||
Ok(Attention)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialise(_: &Attention) -> Element {
|
impl<'a> Into<Element> for &'a Attention {
|
||||||
Element::builder("attention")
|
fn into(self) -> Element {
|
||||||
.ns(ns::ATTENTION)
|
Element::builder("attention")
|
||||||
.build()
|
.ns(ns::ATTENTION)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::convert::TryFrom;
|
||||||
use minidom::Element;
|
use minidom::Element;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use attention;
|
use super::Attention;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
let elem: Element = "<attention xmlns='urn:xmpp:attention:0'/>".parse().unwrap();
|
let elem: Element = "<attention xmlns='urn:xmpp:attention:0'/>".parse().unwrap();
|
||||||
attention::parse_attention(&elem).unwrap();
|
Attention::try_from(&elem).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_child() {
|
fn test_invalid_child() {
|
||||||
let elem: Element = "<attention xmlns='urn:xmpp:attention:0'><coucou/></attention>".parse().unwrap();
|
let elem: Element = "<attention xmlns='urn:xmpp:attention:0'><coucou/></attention>".parse().unwrap();
|
||||||
let error = attention::parse_attention(&elem).unwrap_err();
|
let error = Attention::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
|
@ -55,8 +64,10 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialise() {
|
fn test_serialise() {
|
||||||
let elem: Element = "<attention xmlns='urn:xmpp:attention:0'/>".parse().unwrap();
|
let elem: Element = "<attention xmlns='urn:xmpp:attention:0'/>".parse().unwrap();
|
||||||
let attention = attention::Attention;
|
let attention = Attention;
|
||||||
let elem2 = attention::serialise(&attention);
|
let elem2: Element = (&attention).into();
|
||||||
|
let elem3: Element = (&attention).into();
|
||||||
assert_eq!(elem, elem2);
|
assert_eq!(elem, elem2);
|
||||||
|
assert_eq!(elem2, elem3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,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/.
|
||||||
|
|
||||||
|
#![feature(try_from)]
|
||||||
|
|
||||||
extern crate minidom;
|
extern crate minidom;
|
||||||
extern crate jid;
|
extern crate jid;
|
||||||
extern crate base64;
|
extern crate base64;
|
||||||
|
|
|
||||||
|
|
@ -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 minidom::{Element, IntoElements, IntoAttributeValue};
|
use minidom::{Element, IntoElements, IntoAttributeValue};
|
||||||
|
|
@ -20,7 +21,7 @@ use stanza_error;
|
||||||
use chatstates;
|
use chatstates;
|
||||||
use receipts;
|
use receipts;
|
||||||
use delay;
|
use delay;
|
||||||
use attention;
|
use attention::Attention;
|
||||||
use message_correct;
|
use message_correct;
|
||||||
use eme;
|
use eme;
|
||||||
|
|
||||||
|
|
@ -32,7 +33,7 @@ pub enum MessagePayload {
|
||||||
ChatState(chatstates::ChatState),
|
ChatState(chatstates::ChatState),
|
||||||
Receipt(receipts::Receipt),
|
Receipt(receipts::Receipt),
|
||||||
Delay(delay::Delay),
|
Delay(delay::Delay),
|
||||||
Attention(attention::Attention),
|
Attention(Attention),
|
||||||
MessageCorrect(message_correct::Replace),
|
MessageCorrect(message_correct::Replace),
|
||||||
ExplicitMessageEncryption(eme::ExplicitMessageEncryption),
|
ExplicitMessageEncryption(eme::ExplicitMessageEncryption),
|
||||||
}
|
}
|
||||||
|
|
@ -121,7 +122,7 @@ pub fn parse_message(root: &Element) -> Result<Message, Error> {
|
||||||
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))
|
||||||
} else if let Ok(attention) = attention::parse_attention(elem) {
|
} else if let Ok(attention) = Attention::try_from(elem) {
|
||||||
Some(MessagePayload::Attention(attention))
|
Some(MessagePayload::Attention(attention))
|
||||||
} else if let Ok(replace) = message_correct::parse_replace(elem) {
|
} else if let Ok(replace) = message_correct::parse_replace(elem) {
|
||||||
Some(MessagePayload::MessageCorrect(replace))
|
Some(MessagePayload::MessageCorrect(replace))
|
||||||
|
|
@ -148,7 +149,7 @@ pub fn serialise_payload(payload: &MessagePayload) -> Element {
|
||||||
match *payload {
|
match *payload {
|
||||||
MessagePayload::Body(ref body) => body::serialise(body),
|
MessagePayload::Body(ref body) => body::serialise(body),
|
||||||
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::serialise(attention),
|
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) => receipts::serialise(receipt),
|
||||||
MessagePayload::Delay(ref delay) => delay::serialise(delay),
|
MessagePayload::Delay(ref delay) => delay::serialise(delay),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue