mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
move NamespaceSet to namespaces mod
This commit is contained in:
parent
7d2699e08e
commit
0148790a01
3 changed files with 108 additions and 99 deletions
101
src/element.rs
101
src/element.rs
|
@ -4,7 +4,6 @@ use std::io:: Write;
|
|||
use std::collections::{btree_map, BTreeMap};
|
||||
|
||||
use std::str;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use std::borrow::Cow;
|
||||
|
||||
|
@ -20,6 +19,7 @@ use std::str::FromStr;
|
|||
use std::slice;
|
||||
|
||||
use convert::{IntoElements, IntoAttributeValue, ElementEmitter};
|
||||
use namespaces::NamespaceSet;
|
||||
|
||||
/// Escape XML text
|
||||
pub fn write_escaped<W: Write>(writer: &mut W, input: &str) -> Result<()> {
|
||||
|
@ -97,103 +97,6 @@ impl Node {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
struct NamespaceSet {
|
||||
parent: RefCell<Option<Rc<NamespaceSet>>>,
|
||||
namespaces: BTreeMap<Option<String>, String>,
|
||||
}
|
||||
|
||||
impl Default for NamespaceSet {
|
||||
fn default() -> Self {
|
||||
NamespaceSet {
|
||||
parent: RefCell::new(None),
|
||||
namespaces: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NamespaceSet {
|
||||
fn get(&self, prefix: &Option<String>) -> Option<String> {
|
||||
match self.namespaces.get(prefix) {
|
||||
Some(ns) => Some(ns.clone()),
|
||||
None => match *self.parent.borrow() {
|
||||
None => None,
|
||||
Some(ref parent) => parent.get(prefix)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn has<NS: AsRef<str>>(&self, prefix: &Option<String>, wanted_ns: NS) -> bool {
|
||||
match self.namespaces.get(prefix) {
|
||||
Some(ns) =>
|
||||
ns == wanted_ns.as_ref(),
|
||||
None => match *self.parent.borrow() {
|
||||
None =>
|
||||
false,
|
||||
Some(ref parent) =>
|
||||
parent.has(prefix, wanted_ns),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn set_parent(&self, parent: Rc<NamespaceSet>) {
|
||||
let mut parent_ns = self.parent.borrow_mut();
|
||||
let new_set = parent;
|
||||
*parent_ns = Some(new_set);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl From<BTreeMap<Option<String>, String>> for NamespaceSet {
|
||||
fn from(namespaces: BTreeMap<Option<String>, String>) -> Self {
|
||||
NamespaceSet {
|
||||
parent: RefCell::new(None),
|
||||
namespaces: namespaces,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Option<String>> for NamespaceSet {
|
||||
fn from(namespace: Option<String>) -> Self {
|
||||
match namespace {
|
||||
None => Self::default(),
|
||||
Some(namespace) => Self::from(namespace),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for NamespaceSet {
|
||||
fn from(namespace: String) -> Self {
|
||||
let mut namespaces = BTreeMap::new();
|
||||
namespaces.insert(None, namespace);
|
||||
|
||||
NamespaceSet {
|
||||
parent: RefCell::new(None),
|
||||
namespaces: namespaces,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(Option<String>, String)> for NamespaceSet {
|
||||
fn from(prefix_namespace: (Option<String>, String)) -> Self {
|
||||
let (prefix, namespace) = prefix_namespace;
|
||||
let mut namespaces = BTreeMap::new();
|
||||
namespaces.insert(prefix, namespace);
|
||||
|
||||
NamespaceSet {
|
||||
parent: RefCell::new(None),
|
||||
namespaces: namespaces,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(String, String)> for NamespaceSet {
|
||||
fn from(prefix_namespace: (String, String)) -> Self {
|
||||
let (prefix, namespace) = prefix_namespace;
|
||||
Self::from((Some(prefix), namespace))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
/// A struct representing a DOM Element.
|
||||
pub struct Element {
|
||||
|
@ -437,7 +340,7 @@ impl Element {
|
|||
};
|
||||
write!(writer, "<{}", name)?;
|
||||
|
||||
for (prefix, ns) in &self.namespaces.namespaces {
|
||||
for (prefix, ns) in self.namespaces.declared_ns() {
|
||||
match prefix {
|
||||
&None => {
|
||||
write!(writer, " xmlns=\"")?;
|
||||
|
|
|
@ -70,6 +70,7 @@ extern crate quick_xml;
|
|||
pub mod error;
|
||||
pub mod element;
|
||||
pub mod convert;
|
||||
mod namespaces;
|
||||
|
||||
#[cfg(test)] mod tests;
|
||||
|
||||
|
|
105
src/namespaces.rs
Normal file
105
src/namespaces.rs
Normal file
|
@ -0,0 +1,105 @@
|
|||
use std::collections::BTreeMap;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct NamespaceSet {
|
||||
parent: RefCell<Option<Rc<NamespaceSet>>>,
|
||||
namespaces: BTreeMap<Option<String>, String>,
|
||||
}
|
||||
|
||||
impl Default for NamespaceSet {
|
||||
fn default() -> Self {
|
||||
NamespaceSet {
|
||||
parent: RefCell::new(None),
|
||||
namespaces: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NamespaceSet {
|
||||
pub fn declared_ns(&self) -> &BTreeMap<Option<String>, String> {
|
||||
&self.namespaces
|
||||
}
|
||||
|
||||
pub fn get(&self, prefix: &Option<String>) -> Option<String> {
|
||||
match self.namespaces.get(prefix) {
|
||||
Some(ns) => Some(ns.clone()),
|
||||
None => match *self.parent.borrow() {
|
||||
None => None,
|
||||
Some(ref parent) => parent.get(prefix)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has<NS: AsRef<str>>(&self, prefix: &Option<String>, wanted_ns: NS) -> bool {
|
||||
match self.namespaces.get(prefix) {
|
||||
Some(ns) =>
|
||||
ns == wanted_ns.as_ref(),
|
||||
None => match *self.parent.borrow() {
|
||||
None =>
|
||||
false,
|
||||
Some(ref parent) =>
|
||||
parent.has(prefix, wanted_ns),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_parent(&self, parent: Rc<NamespaceSet>) {
|
||||
let mut parent_ns = self.parent.borrow_mut();
|
||||
let new_set = parent;
|
||||
*parent_ns = Some(new_set);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl From<BTreeMap<Option<String>, String>> for NamespaceSet {
|
||||
fn from(namespaces: BTreeMap<Option<String>, String>) -> Self {
|
||||
NamespaceSet {
|
||||
parent: RefCell::new(None),
|
||||
namespaces: namespaces,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Option<String>> for NamespaceSet {
|
||||
fn from(namespace: Option<String>) -> Self {
|
||||
match namespace {
|
||||
None => Self::default(),
|
||||
Some(namespace) => Self::from(namespace),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for NamespaceSet {
|
||||
fn from(namespace: String) -> Self {
|
||||
let mut namespaces = BTreeMap::new();
|
||||
namespaces.insert(None, namespace);
|
||||
|
||||
NamespaceSet {
|
||||
parent: RefCell::new(None),
|
||||
namespaces: namespaces,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(Option<String>, String)> for NamespaceSet {
|
||||
fn from(prefix_namespace: (Option<String>, String)) -> Self {
|
||||
let (prefix, namespace) = prefix_namespace;
|
||||
let mut namespaces = BTreeMap::new();
|
||||
namespaces.insert(prefix, namespace);
|
||||
|
||||
NamespaceSet {
|
||||
parent: RefCell::new(None),
|
||||
namespaces: namespaces,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(String, String)> for NamespaceSet {
|
||||
fn from(prefix_namespace: (String, String)) -> Self {
|
||||
let (prefix, namespace) = prefix_namespace;
|
||||
Self::from((Some(prefix), namespace))
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue