Add a status parser.
This commit is contained in:
parent
948b54d073
commit
93343f3a00
2 changed files with 84 additions and 1 deletions
|
|
@ -21,8 +21,11 @@ pub mod ns;
|
||||||
pub mod message;
|
pub mod message;
|
||||||
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
|
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
|
||||||
pub mod presence;
|
pub mod presence;
|
||||||
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
|
|
||||||
|
/// RFC 6121: Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence
|
||||||
pub mod body;
|
pub mod body;
|
||||||
|
/// RFC 6121: Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence
|
||||||
|
pub mod status;
|
||||||
|
|
||||||
/// XEP-0004: Data Forms
|
/// XEP-0004: Data Forms
|
||||||
pub mod data_forms;
|
pub mod data_forms;
|
||||||
|
|
|
||||||
80
src/status.rs
Normal file
80
src/status.rs
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
use minidom::Element;
|
||||||
|
|
||||||
|
use error::Error;
|
||||||
|
|
||||||
|
use ns;
|
||||||
|
|
||||||
|
pub type Status = String;
|
||||||
|
|
||||||
|
pub fn parse_status(root: &Element) -> Result<Status, Error> {
|
||||||
|
// TODO: also support components and servers.
|
||||||
|
if !root.is("status", ns::JABBER_CLIENT) {
|
||||||
|
return Err(Error::ParseError("This is not a status element."));
|
||||||
|
}
|
||||||
|
for _ in root.children() {
|
||||||
|
return Err(Error::ParseError("Unknown child in status element."));
|
||||||
|
}
|
||||||
|
Ok(root.text())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn serialise(status: &Status) -> Element {
|
||||||
|
Element::builder("status")
|
||||||
|
.ns(ns::JABBER_CLIENT)
|
||||||
|
.append(status.to_owned())
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use minidom::Element;
|
||||||
|
use error::Error;
|
||||||
|
use status;
|
||||||
|
use ns;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_simple() {
|
||||||
|
let elem: Element = "<status xmlns='jabber:client'/>".parse().unwrap();
|
||||||
|
status::parse_status(&elem).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_invalid() {
|
||||||
|
let elem: Element = "<status xmlns='jabber:server'/>".parse().unwrap();
|
||||||
|
let error = status::parse_status(&elem).unwrap_err();
|
||||||
|
let message = match error {
|
||||||
|
Error::ParseError(string) => string,
|
||||||
|
_ => panic!(),
|
||||||
|
};
|
||||||
|
assert_eq!(message, "This is not a status element.");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_invalid_child() {
|
||||||
|
let elem: Element = "<status xmlns='jabber:client'><coucou/></status>".parse().unwrap();
|
||||||
|
let error = status::parse_status(&elem).unwrap_err();
|
||||||
|
let message = match error {
|
||||||
|
Error::ParseError(string) => string,
|
||||||
|
_ => panic!(),
|
||||||
|
};
|
||||||
|
assert_eq!(message, "Unknown child in status element.");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn test_invalid_attribute() {
|
||||||
|
let elem: Element = "<status xmlns='jabber:client' coucou=''/>".parse().unwrap();
|
||||||
|
let error = status::parse_status(&elem).unwrap_err();
|
||||||
|
let message = match error {
|
||||||
|
Error::ParseError(string) => string,
|
||||||
|
_ => panic!(),
|
||||||
|
};
|
||||||
|
assert_eq!(message, "Unknown attribute in status element.");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_serialise() {
|
||||||
|
let status = status::Status::from("Hello world!");
|
||||||
|
let elem = status::serialise(&status);
|
||||||
|
assert!(elem.is("status", ns::JABBER_CLIENT));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue