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
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use minidom::Element;
|
||||
|
||||
use error::Error;
|
||||
|
|
@ -13,38 +15,45 @@ use ns;
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct Attention;
|
||||
|
||||
pub fn parse_attention(root: &Element) -> Result<Attention, Error> {
|
||||
if !root.is("attention", ns::ATTENTION) {
|
||||
return Err(Error::ParseError("This is not an attention element."));
|
||||
impl<'a> TryFrom<&'a Element> for Attention {
|
||||
type Error = Error;
|
||||
|
||||
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 {
|
||||
Element::builder("attention")
|
||||
.ns(ns::ATTENTION)
|
||||
.build()
|
||||
impl<'a> Into<Element> for &'a Attention {
|
||||
fn into(self) -> Element {
|
||||
Element::builder("attention")
|
||||
.ns(ns::ATTENTION)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::convert::TryFrom;
|
||||
use minidom::Element;
|
||||
use error::Error;
|
||||
use attention;
|
||||
use super::Attention;
|
||||
|
||||
#[test]
|
||||
fn test_simple() {
|
||||
let elem: Element = "<attention xmlns='urn:xmpp:attention:0'/>".parse().unwrap();
|
||||
attention::parse_attention(&elem).unwrap();
|
||||
Attention::try_from(&elem).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_child() {
|
||||
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 {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
@ -55,8 +64,10 @@ mod tests {
|
|||
#[test]
|
||||
fn test_serialise() {
|
||||
let elem: Element = "<attention xmlns='urn:xmpp:attention:0'/>".parse().unwrap();
|
||||
let attention = attention::Attention;
|
||||
let elem2 = attention::serialise(&attention);
|
||||
let attention = Attention;
|
||||
let elem2: Element = (&attention).into();
|
||||
let elem3: Element = (&attention).into();
|
||||
assert_eq!(elem, elem2);
|
||||
assert_eq!(elem2, elem3);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue