Import ns itself, and remove the _NS suffix on all namespaces.

This commit is contained in:
Emmanuel Gil Peyrot 2017-04-20 23:16:12 +01:00
parent 411e421732
commit 7288c2c74f
10 changed files with 47 additions and 47 deletions

View file

@ -2,8 +2,7 @@ use minidom::Element;
use error::Error; use error::Error;
// TODO: also support components and servers. use ns;
use ns::JABBER_CLIENT_NS;
#[derive(Debug)] #[derive(Debug)]
pub struct Body { pub struct Body {
@ -11,7 +10,8 @@ pub struct Body {
} }
pub fn parse_body(root: &Element) -> Result<Body, Error> { pub fn parse_body(root: &Element) -> Result<Body, Error> {
if !root.is("body", JABBER_CLIENT_NS) { // TODO: also support components and servers.
if !root.is("body", ns::JABBER_CLIENT) {
return Err(Error::ParseError("This is not a body element.")); return Err(Error::ParseError("This is not a body element."));
} }
for _ in root.children() { for _ in root.children() {

View file

@ -2,7 +2,7 @@ use minidom::Element;
use error::Error; use error::Error;
use ns::CHATSTATES_NS; use ns;
#[derive(Debug)] #[derive(Debug)]
pub enum ChatState { pub enum ChatState {
@ -17,15 +17,15 @@ pub fn parse_chatstate(root: &Element) -> Result<ChatState, Error> {
for _ in root.children() { for _ in root.children() {
return Err(Error::ParseError("Unknown child in chatstate element.")); return Err(Error::ParseError("Unknown child in chatstate element."));
} }
if root.is("active", CHATSTATES_NS) { if root.is("active", ns::CHATSTATES) {
Ok(ChatState::Active) Ok(ChatState::Active)
} else if root.is("composing", CHATSTATES_NS) { } else if root.is("composing", ns::CHATSTATES) {
Ok(ChatState::Composing) Ok(ChatState::Composing)
} else if root.is("gone", CHATSTATES_NS) { } else if root.is("gone", ns::CHATSTATES) {
Ok(ChatState::Gone) Ok(ChatState::Gone)
} else if root.is("inactive", CHATSTATES_NS) { } else if root.is("inactive", ns::CHATSTATES) {
Ok(ChatState::Inactive) Ok(ChatState::Inactive)
} else if root.is("paused", CHATSTATES_NS) { } else if root.is("paused", ns::CHATSTATES) {
Ok(ChatState::Paused) Ok(ChatState::Paused)
} else { } else {
Err(Error::ParseError("This is not a chatstate element.")) Err(Error::ParseError("This is not a chatstate element."))

View file

@ -5,7 +5,7 @@ use std::str::FromStr;
use minidom::Element; use minidom::Element;
use error::Error; use error::Error;
use ns::{DATA_FORMS_NS, MEDIA_ELEMENT_NS}; use ns;
use media_element::{MediaElement, parse_media_element}; use media_element::{MediaElement, parse_media_element};
@ -52,7 +52,7 @@ pub struct DataForm {
} }
pub fn parse_data_form(root: &Element) -> Result<DataForm, Error> { pub fn parse_data_form(root: &Element) -> Result<DataForm, Error> {
if !root.is("x", DATA_FORMS_NS) { if !root.is("x", ns::DATA_FORMS) {
return Err(Error::ParseError("This is not a data form element.")); return Err(Error::ParseError("This is not a data form element."));
} }
@ -63,16 +63,16 @@ pub fn parse_data_form(root: &Element) -> Result<DataForm, Error> {
let mut fields = vec!(); let mut fields = vec!();
let mut form_type = None; let mut form_type = None;
for field in root.children() { for field in root.children() {
if field.is("field", DATA_FORMS_NS) { if field.is("field", ns::DATA_FORMS) {
let var = field.attr("var").ok_or(Error::ParseError("Field must have a 'var' attribute."))?; let var = field.attr("var").ok_or(Error::ParseError("Field must have a 'var' attribute."))?;
let field_type = field.attr("type").unwrap_or("text-single"); let field_type = field.attr("type").unwrap_or("text-single");
let label = field.attr("label").and_then(|label| label.parse().ok()); let label = field.attr("label").and_then(|label| label.parse().ok());
let mut values = vec!(); let mut values = vec!();
let mut media = vec!(); let mut media = vec!();
for element in field.children() { for element in field.children() {
if element.is("value", DATA_FORMS_NS) { if element.is("value", ns::DATA_FORMS) {
values.push(element.text()); values.push(element.text());
} else if element.is("media", MEDIA_ELEMENT_NS) { } else if element.is("media", ns::MEDIA_ELEMENT) {
match parse_media_element(element) { match parse_media_element(element) {
Ok(media_element) => media.push(media_element), Ok(media_element) => media.push(media_element),
Err(_) => (), // TODO: is it really nice to swallow this error? Err(_) => (), // TODO: is it really nice to swallow this error?

View file

@ -3,7 +3,7 @@ extern crate minidom;
use minidom::Element; use minidom::Element;
use error::Error; use error::Error;
use ns::{DISCO_INFO_NS, DATA_FORMS_NS}; use ns;
use data_forms::{DataForm, DataFormType, parse_data_form}; use data_forms::{DataForm, DataFormType, parse_data_form};
@ -29,7 +29,7 @@ pub struct Disco {
} }
pub fn parse_disco(root: &Element) -> Result<Disco, Error> { pub fn parse_disco(root: &Element) -> Result<Disco, Error> {
if !root.is("query", DISCO_INFO_NS) { if !root.is("query", ns::DISCO_INFO) {
return Err(Error::ParseError("This is not a disco#info element.")); return Err(Error::ParseError("This is not a disco#info element."));
} }
@ -41,13 +41,13 @@ pub fn parse_disco(root: &Element) -> Result<Disco, Error> {
.and_then(|node| node.parse().ok()); .and_then(|node| node.parse().ok());
for child in root.children() { for child in root.children() {
if child.is("feature", DISCO_INFO_NS) { if child.is("feature", ns::DISCO_INFO) {
let feature = child.attr("var") let feature = child.attr("var")
.ok_or(Error::ParseError("Feature must have a 'var' attribute."))?; .ok_or(Error::ParseError("Feature must have a 'var' attribute."))?;
features.push(Feature { features.push(Feature {
var: feature.to_owned(), var: feature.to_owned(),
}); });
} else if child.is("identity", DISCO_INFO_NS) { } else if child.is("identity", ns::DISCO_INFO) {
let category = child.attr("category") let category = child.attr("category")
.ok_or(Error::ParseError("Identity must have a 'category' attribute."))?; .ok_or(Error::ParseError("Identity must have a 'category' attribute."))?;
if category == "" { if category == "" {
@ -70,7 +70,7 @@ pub fn parse_disco(root: &Element) -> Result<Disco, Error> {
xml_lang: xml_lang.to_owned(), xml_lang: xml_lang.to_owned(),
name: name, name: name,
}); });
} else if child.is("x", DATA_FORMS_NS) { } else if child.is("x", ns::DATA_FORMS) {
let data_form = parse_data_form(child)?; let data_form = parse_data_form(child)?;
match data_form.type_ { match data_form.type_ {
DataFormType::Result_ => (), DataFormType::Result_ => (),
@ -93,7 +93,7 @@ pub fn parse_disco(root: &Element) -> Result<Disco, Error> {
if features.is_empty() { if features.is_empty() {
return Err(Error::ParseError("There must be at least one feature in disco#info.")); return Err(Error::ParseError("There must be at least one feature in disco#info."));
} }
if !features.contains(&Feature { var: DISCO_INFO_NS.to_owned() }) { if !features.contains(&Feature { var: ns::DISCO_INFO.to_owned() }) {
return Err(Error::ParseError("disco#info feature not present in disco#info.")); return Err(Error::ParseError("disco#info feature not present in disco#info."));
} }
*/ */
@ -108,12 +108,12 @@ pub fn parse_disco(root: &Element) -> Result<Disco, Error> {
pub fn serialise_disco(disco: &Disco) -> Element { pub fn serialise_disco(disco: &Disco) -> Element {
let mut root = Element::builder("query") let mut root = Element::builder("query")
.ns(DISCO_INFO_NS) .ns(ns::DISCO_INFO)
.attr("node", disco.node.clone()) .attr("node", disco.node.clone())
.build(); .build();
for identity in &disco.identities { for identity in &disco.identities {
let identity_element = Element::builder("identity") let identity_element = Element::builder("identity")
.ns(DISCO_INFO_NS) .ns(ns::DISCO_INFO)
.attr("category", identity.category.clone()) .attr("category", identity.category.clone())
.attr("type", identity.type_.clone()) .attr("type", identity.type_.clone())
.attr("xml:lang", identity.xml_lang.clone()) .attr("xml:lang", identity.xml_lang.clone())
@ -123,7 +123,7 @@ pub fn serialise_disco(disco: &Disco) -> Element {
} }
for feature in &disco.features { for feature in &disco.features {
let feature_element = Element::builder("feature") let feature_element = Element::builder("feature")
.ns(DISCO_INFO_NS) .ns(ns::DISCO_INFO)
.attr("var", feature.var.clone()) .attr("var", feature.var.clone())
.build(); .build();
root.append_child(feature_element); root.append_child(feature_element);

View file

@ -4,7 +4,7 @@ use minidom::Element;
use error::Error; use error::Error;
use ns::IBB_NS; use ns;
#[derive(Debug)] #[derive(Debug)]
pub enum Stanza { pub enum Stanza {
@ -49,7 +49,7 @@ fn required_attr<T: FromStr>(root: &Element, attr: &str, err: Error) -> Result<T
} }
pub fn parse_ibb(root: &Element) -> Result<IBB, Error> { pub fn parse_ibb(root: &Element) -> Result<IBB, Error> {
if root.is("open", IBB_NS) { if root.is("open", ns::IBB) {
let block_size = required_attr(root, "block-size", Error::ParseError("Required attribute 'block-size' missing in open element."))?; let block_size = required_attr(root, "block-size", Error::ParseError("Required attribute 'block-size' missing in open element."))?;
let sid = required_attr(root, "sid", Error::ParseError("Required attribute 'sid' missing in open element."))?; let sid = required_attr(root, "sid", Error::ParseError("Required attribute 'sid' missing in open element."))?;
let stanza = root.attr("stanza") let stanza = root.attr("stanza")

View file

@ -5,7 +5,7 @@ use std::str::FromStr;
use minidom::Element; use minidom::Element;
use error::Error; use error::Error;
use ns::JINGLE_NS; use ns;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Action { pub enum Action {
@ -209,7 +209,7 @@ pub struct Jingle {
} }
pub fn parse_jingle(root: &Element) -> Result<Jingle, Error> { pub fn parse_jingle(root: &Element) -> Result<Jingle, Error> {
if !root.is("jingle", JINGLE_NS) { if !root.is("jingle", ns::JINGLE) {
return Err(Error::ParseError("This is not a Jingle element.")); return Err(Error::ParseError("This is not a Jingle element."));
} }
@ -227,7 +227,7 @@ pub fn parse_jingle(root: &Element) -> Result<Jingle, Error> {
let mut reason_element = None; let mut reason_element = None;
for child in root.children() { for child in root.children() {
if child.is("content", JINGLE_NS) { if child.is("content", ns::JINGLE) {
let creator = child.attr("creator") let creator = child.attr("creator")
.ok_or(Error::ParseError("Content must have a 'creator' attribute."))? .ok_or(Error::ParseError("Content must have a 'creator' attribute."))?
.parse()?; .parse()?;
@ -276,14 +276,14 @@ pub fn parse_jingle(root: &Element) -> Result<Jingle, Error> {
transport: transport, transport: transport,
security: security.to_owned(), security: security.to_owned(),
}); });
} else if child.is("reason", JINGLE_NS) { } else if child.is("reason", ns::JINGLE) {
if reason_element.is_some() { if reason_element.is_some() {
return Err(Error::ParseError("Jingle must not have more than one reason.")); return Err(Error::ParseError("Jingle must not have more than one reason."));
} }
let mut reason = None; let mut reason = None;
let mut text = None; let mut text = None;
for stuff in child.children() { for stuff in child.children() {
if stuff.ns() != Some(JINGLE_NS) { if stuff.ns() != Some(ns::JINGLE) {
return Err(Error::ParseError("Reason contains a foreign element.")); return Err(Error::ParseError("Reason contains a foreign element."));
} }
let name = stuff.name(); let name = stuff.name();

View file

@ -2,7 +2,7 @@ use minidom::Element;
use error::Error; use error::Error;
use ns::MEDIA_ELEMENT_NS; use ns;
#[derive(Debug)] #[derive(Debug)]
pub struct URI { pub struct URI {
@ -18,7 +18,7 @@ pub struct MediaElement {
} }
pub fn parse_media_element(root: &Element) -> Result<MediaElement, Error> { pub fn parse_media_element(root: &Element) -> Result<MediaElement, Error> {
if !root.is("media", MEDIA_ELEMENT_NS) { if !root.is("media", ns::MEDIA_ELEMENT) {
return Err(Error::ParseError("This is not a media element.")); return Err(Error::ParseError("This is not a media element."));
} }
@ -26,7 +26,7 @@ pub fn parse_media_element(root: &Element) -> Result<MediaElement, Error> {
let height = root.attr("height").and_then(|height| height.parse().ok()); let height = root.attr("height").and_then(|height| height.parse().ok());
let mut uris = vec!(); let mut uris = vec!();
for uri in root.children() { for uri in root.children() {
if uri.is("uri", MEDIA_ELEMENT_NS) { if uri.is("uri", ns::MEDIA_ELEMENT) {
let type_ = uri.attr("type").ok_or(Error::ParseError("Attribute type on uri is mandatory."))?; let type_ = uri.attr("type").ok_or(Error::ParseError("Attribute type on uri is mandatory."))?;
let text = uri.text().trim().to_owned(); let text = uri.text().trim().to_owned();
if text == "" { if text == "" {

View file

@ -1,9 +1,9 @@
pub const JABBER_CLIENT_NS: &'static str = "jabber:client"; pub const JABBER_CLIENT: &'static str = "jabber:client";
pub const DISCO_INFO_NS: &'static str = "http://jabber.org/protocol/disco#info"; pub const DISCO_INFO: &'static str = "http://jabber.org/protocol/disco#info";
pub const DATA_FORMS_NS: &'static str = "jabber:x:data"; pub const DATA_FORMS: &'static str = "jabber:x:data";
pub const MEDIA_ELEMENT_NS: &'static str = "urn:xmpp:media-element"; pub const MEDIA_ELEMENT: &'static str = "urn:xmpp:media-element";
pub const JINGLE_NS: &'static str = "urn:xmpp:jingle:1"; pub const JINGLE: &'static str = "urn:xmpp:jingle:1";
pub const PING_NS: &'static str = "urn:xmpp:ping"; pub const PING: &'static str = "urn:xmpp:ping";
pub const CHATSTATES_NS: &'static str = "http://jabber.org/protocol/chatstates"; pub const CHATSTATES: &'static str = "http://jabber.org/protocol/chatstates";
pub const IBB_NS: &'static str = "http://jabber.org/protocol/ibb"; pub const IBB: &'static str = "http://jabber.org/protocol/ibb";
pub const RECEIPTS_NS: &'static str = "urn:xmpp:receipts"; pub const RECEIPTS: &'static str = "urn:xmpp:receipts";

View file

@ -2,14 +2,14 @@ use minidom::Element;
use error::Error; use error::Error;
use ns::PING_NS; use ns;
#[derive(Debug)] #[derive(Debug)]
pub struct Ping { pub struct Ping {
} }
pub fn parse_ping(root: &Element) -> Result<Ping, Error> { pub fn parse_ping(root: &Element) -> Result<Ping, Error> {
if !root.is("ping", PING_NS) { if !root.is("ping", ns::PING) {
return Err(Error::ParseError("This is not a ping element.")); return Err(Error::ParseError("This is not a ping element."));
} }

View file

@ -2,7 +2,7 @@ use minidom::Element;
use error::Error; use error::Error;
use ns::RECEIPTS_NS; use ns;
#[derive(Debug)] #[derive(Debug)]
pub enum Receipt { pub enum Receipt {
@ -14,9 +14,9 @@ pub fn parse_receipt(root: &Element) -> Result<Receipt, Error> {
for _ in root.children() { for _ in root.children() {
return Err(Error::ParseError("Unknown child in receipt element.")); return Err(Error::ParseError("Unknown child in receipt element."));
} }
if root.is("request", RECEIPTS_NS) { if root.is("request", ns::RECEIPTS) {
Ok(Receipt::Request) Ok(Receipt::Request)
} else if root.is("received", RECEIPTS_NS) { } else if root.is("received", ns::RECEIPTS) {
let id = root.attr("id").unwrap_or("").to_owned(); let id = root.attr("id").unwrap_or("").to_owned();
Ok(Receipt::Received(id)) Ok(Receipt::Received(id))
} else { } else {