mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
Split Node
off into its own module: node.rs
This commit is contained in:
parent
c5c8dee20a
commit
f0dd03d633
4 changed files with 207 additions and 194 deletions
|
@ -1,7 +1,5 @@
|
|||
//! A module which exports a few traits for converting types to elements and attributes.
|
||||
|
||||
use element::{Element, ElementBuilder};
|
||||
|
||||
/// A trait for types which can be converted to an attribute value.
|
||||
pub trait IntoAttributeValue {
|
||||
/// Turns this into an attribute string, or None if it shouldn't be added.
|
||||
|
|
193
src/element.rs
193
src/element.rs
|
@ -11,7 +11,7 @@ use error::{Error, Result};
|
|||
|
||||
use quick_xml::Reader as EventReader;
|
||||
use quick_xml::Writer as EventWriter;
|
||||
use quick_xml::events::{Event, BytesStart, BytesEnd, BytesText, BytesDecl};
|
||||
use quick_xml::events::{Event, BytesStart, BytesEnd, BytesDecl};
|
||||
|
||||
use std::io::BufRead;
|
||||
|
||||
|
@ -21,6 +21,7 @@ use std::slice;
|
|||
|
||||
use convert::IntoAttributeValue;
|
||||
use namespace_set::NamespaceSet;
|
||||
use node::Node;
|
||||
|
||||
/// helper function to escape a `&[u8]` and replace all
|
||||
/// xml special characters (<, >, &, ', ") with their corresponding
|
||||
|
@ -68,196 +69,6 @@ pub fn escape(raw: &[u8]) -> Cow<[u8]> {
|
|||
}
|
||||
}
|
||||
|
||||
/// A node in an element tree.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Node {
|
||||
/// An `Element`.
|
||||
Element(Element),
|
||||
/// A text node.
|
||||
Text(String),
|
||||
/// A comment node.
|
||||
Comment(String),
|
||||
}
|
||||
|
||||
impl Node {
|
||||
/// Turns this into a reference to an `Element` if this is an element node.
|
||||
/// Else this returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Node;
|
||||
///
|
||||
/// let elm = Node::Element("<meow />".parse().unwrap());
|
||||
/// let txt = Node::Text("meow".to_owned());
|
||||
///
|
||||
/// assert_eq!(elm.as_element().unwrap().name(), "meow");
|
||||
/// assert_eq!(txt.as_element(), None);
|
||||
/// ```
|
||||
pub fn as_element(&self) -> Option<&Element> {
|
||||
match *self {
|
||||
Node::Element(ref e) => Some(e),
|
||||
Node::Text(_) => None,
|
||||
Node::Comment(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns this into a mutable reference of an `Element` if this is an element node.
|
||||
/// Else this returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Node;
|
||||
///
|
||||
/// let mut elm = Node::Element("<meow />".parse().unwrap());
|
||||
/// let mut txt = Node::Text("meow".to_owned());
|
||||
///
|
||||
/// assert_eq!(elm.as_element_mut().unwrap().name(), "meow");
|
||||
/// assert_eq!(txt.as_element_mut(), None);
|
||||
/// ```
|
||||
pub fn as_element_mut(&mut self) -> Option<&mut Element> {
|
||||
match *self {
|
||||
Node::Element(ref mut e) => Some(e),
|
||||
Node::Text(_) => None,
|
||||
Node::Comment(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns this into an `Element`, consuming self, if this is an element node.
|
||||
/// Else this returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Node;
|
||||
///
|
||||
/// let elm = Node::Element("<meow />".parse().unwrap());
|
||||
/// let txt = Node::Text("meow".to_owned());
|
||||
///
|
||||
/// assert_eq!(elm.into_element().unwrap().name(), "meow");
|
||||
/// assert_eq!(txt.into_element(), None);
|
||||
/// ```
|
||||
pub fn into_element(self) -> Option<Element> {
|
||||
match self {
|
||||
Node::Element(e) => Some(e),
|
||||
Node::Text(_) => None,
|
||||
Node::Comment(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns this into an `&str` if this is a text node.
|
||||
/// Else this returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Node;
|
||||
///
|
||||
/// let elm = Node::Element("<meow />".parse().unwrap());
|
||||
/// let txt = Node::Text("meow".to_owned());
|
||||
///
|
||||
/// assert_eq!(elm.as_text(), None);
|
||||
/// assert_eq!(txt.as_text().unwrap(), "meow");
|
||||
/// ```
|
||||
pub fn as_text(&self) -> Option<&str> {
|
||||
match *self {
|
||||
Node::Element(_) => None,
|
||||
Node::Text(ref s) => Some(s),
|
||||
Node::Comment(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns this into an `&mut String` if this is a text node.
|
||||
/// Else this returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Node;
|
||||
///
|
||||
/// let mut elm = Node::Element("<meow />".parse().unwrap());
|
||||
/// let mut txt = Node::Text("meow".to_owned());
|
||||
///
|
||||
/// assert_eq!(elm.as_text_mut(), None);
|
||||
/// {
|
||||
/// let text_mut = txt.as_text_mut().unwrap();
|
||||
/// assert_eq!(text_mut, "meow");
|
||||
/// text_mut.push_str("zies");
|
||||
/// assert_eq!(text_mut, "meowzies");
|
||||
/// }
|
||||
/// assert_eq!(txt.as_text().unwrap(), "meowzies");
|
||||
/// ```
|
||||
pub fn as_text_mut(&mut self) -> Option<&mut String> {
|
||||
match *self {
|
||||
Node::Element(_) => None,
|
||||
Node::Text(ref mut s) => Some(s),
|
||||
Node::Comment(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns this into an `String`, consuming self, if this is a text node.
|
||||
/// Else this returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Node;
|
||||
///
|
||||
/// let elm = Node::Element("<meow />".parse().unwrap());
|
||||
/// let txt = Node::Text("meow".to_owned());
|
||||
///
|
||||
/// assert_eq!(elm.into_text(), None);
|
||||
/// assert_eq!(txt.into_text().unwrap(), "meow");
|
||||
/// ```
|
||||
pub fn into_text(self) -> Option<String> {
|
||||
match self {
|
||||
Node::Element(_) => None,
|
||||
Node::Text(s) => Some(s),
|
||||
Node::Comment(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn write_to_inner<W: Write>(&self, writer: &mut EventWriter<W>) -> Result<()>{
|
||||
match *self {
|
||||
Node::Element(ref elmt) => elmt.write_to_inner(writer)?,
|
||||
Node::Text(ref s) => {
|
||||
writer.write_event(Event::Text(BytesText::from_plain_str(s)))?;
|
||||
()
|
||||
},
|
||||
Node::Comment(ref s) => {
|
||||
writer.write_event(Event::Comment(BytesText::from_plain_str(s)))?;
|
||||
()
|
||||
},
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Element> for Node {
|
||||
fn from(elm: Element) -> Node {
|
||||
Node::Element(elm)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Node {
|
||||
fn from(s: String) -> Node {
|
||||
Node::Text(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for Node {
|
||||
fn from(s: &'a str) -> Node {
|
||||
Node::Text(s.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ElementBuilder> for Node {
|
||||
fn from(builder: ElementBuilder) -> Node {
|
||||
Node::Element(builder.build())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
/// A struct representing a DOM Element.
|
||||
|
|
|
@ -71,10 +71,12 @@ extern crate failure;
|
|||
pub mod error;
|
||||
pub mod element;
|
||||
pub mod convert;
|
||||
pub mod node;
|
||||
mod namespace_set;
|
||||
|
||||
#[cfg(test)] mod tests;
|
||||
|
||||
pub use error::{Error, Result};
|
||||
pub use element::{Element, Node, Children, ChildrenMut, ElementBuilder};
|
||||
pub use element::{Element, Children, ChildrenMut, ElementBuilder};
|
||||
pub use node::Node;
|
||||
pub use convert::IntoAttributeValue;
|
||||
|
|
202
src/node.rs
Normal file
202
src/node.rs
Normal file
|
@ -0,0 +1,202 @@
|
|||
//! Provides the `Node` struct, which represents a node in the DOM.
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
use quick_xml::Writer as EventWriter;
|
||||
use quick_xml::events::{Event, BytesText};
|
||||
|
||||
use error::Result;
|
||||
|
||||
use element::{Element, ElementBuilder};
|
||||
|
||||
/// A node in an element tree.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Node {
|
||||
/// An `Element`.
|
||||
Element(Element),
|
||||
/// A text node.
|
||||
Text(String),
|
||||
/// A comment node.
|
||||
Comment(String),
|
||||
}
|
||||
|
||||
impl Node {
|
||||
/// Turns this into a reference to an `Element` if this is an element node.
|
||||
/// Else this returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Node;
|
||||
///
|
||||
/// let elm = Node::Element("<meow />".parse().unwrap());
|
||||
/// let txt = Node::Text("meow".to_owned());
|
||||
///
|
||||
/// assert_eq!(elm.as_element().unwrap().name(), "meow");
|
||||
/// assert_eq!(txt.as_element(), None);
|
||||
/// ```
|
||||
pub fn as_element(&self) -> Option<&Element> {
|
||||
match *self {
|
||||
Node::Element(ref e) => Some(e),
|
||||
Node::Text(_) => None,
|
||||
Node::Comment(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns this into a mutable reference of an `Element` if this is an element node.
|
||||
/// Else this returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Node;
|
||||
///
|
||||
/// let mut elm = Node::Element("<meow />".parse().unwrap());
|
||||
/// let mut txt = Node::Text("meow".to_owned());
|
||||
///
|
||||
/// assert_eq!(elm.as_element_mut().unwrap().name(), "meow");
|
||||
/// assert_eq!(txt.as_element_mut(), None);
|
||||
/// ```
|
||||
pub fn as_element_mut(&mut self) -> Option<&mut Element> {
|
||||
match *self {
|
||||
Node::Element(ref mut e) => Some(e),
|
||||
Node::Text(_) => None,
|
||||
Node::Comment(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns this into an `Element`, consuming self, if this is an element node.
|
||||
/// Else this returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Node;
|
||||
///
|
||||
/// let elm = Node::Element("<meow />".parse().unwrap());
|
||||
/// let txt = Node::Text("meow".to_owned());
|
||||
///
|
||||
/// assert_eq!(elm.into_element().unwrap().name(), "meow");
|
||||
/// assert_eq!(txt.into_element(), None);
|
||||
/// ```
|
||||
pub fn into_element(self) -> Option<Element> {
|
||||
match self {
|
||||
Node::Element(e) => Some(e),
|
||||
Node::Text(_) => None,
|
||||
Node::Comment(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns this into an `&str` if this is a text node.
|
||||
/// Else this returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Node;
|
||||
///
|
||||
/// let elm = Node::Element("<meow />".parse().unwrap());
|
||||
/// let txt = Node::Text("meow".to_owned());
|
||||
///
|
||||
/// assert_eq!(elm.as_text(), None);
|
||||
/// assert_eq!(txt.as_text().unwrap(), "meow");
|
||||
/// ```
|
||||
pub fn as_text(&self) -> Option<&str> {
|
||||
match *self {
|
||||
Node::Element(_) => None,
|
||||
Node::Text(ref s) => Some(s),
|
||||
Node::Comment(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns this into an `&mut String` if this is a text node.
|
||||
/// Else this returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Node;
|
||||
///
|
||||
/// let mut elm = Node::Element("<meow />".parse().unwrap());
|
||||
/// let mut txt = Node::Text("meow".to_owned());
|
||||
///
|
||||
/// assert_eq!(elm.as_text_mut(), None);
|
||||
/// {
|
||||
/// let text_mut = txt.as_text_mut().unwrap();
|
||||
/// assert_eq!(text_mut, "meow");
|
||||
/// text_mut.push_str("zies");
|
||||
/// assert_eq!(text_mut, "meowzies");
|
||||
/// }
|
||||
/// assert_eq!(txt.as_text().unwrap(), "meowzies");
|
||||
/// ```
|
||||
pub fn as_text_mut(&mut self) -> Option<&mut String> {
|
||||
match *self {
|
||||
Node::Element(_) => None,
|
||||
Node::Text(ref mut s) => Some(s),
|
||||
Node::Comment(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns this into an `String`, consuming self, if this is a text node.
|
||||
/// Else this returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Node;
|
||||
///
|
||||
/// let elm = Node::Element("<meow />".parse().unwrap());
|
||||
/// let txt = Node::Text("meow".to_owned());
|
||||
///
|
||||
/// assert_eq!(elm.into_text(), None);
|
||||
/// assert_eq!(txt.into_text().unwrap(), "meow");
|
||||
/// ```
|
||||
pub fn into_text(self) -> Option<String> {
|
||||
match self {
|
||||
Node::Element(_) => None,
|
||||
Node::Text(s) => Some(s),
|
||||
Node::Comment(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub(crate) fn write_to_inner<W: Write>(&self, writer: &mut EventWriter<W>) -> Result<()>{
|
||||
match *self {
|
||||
Node::Element(ref elmt) => elmt.write_to_inner(writer)?,
|
||||
Node::Text(ref s) => {
|
||||
writer.write_event(Event::Text(BytesText::from_plain_str(s)))?;
|
||||
()
|
||||
},
|
||||
Node::Comment(ref s) => {
|
||||
writer.write_event(Event::Comment(BytesText::from_plain_str(s)))?;
|
||||
()
|
||||
},
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Element> for Node {
|
||||
fn from(elm: Element) -> Node {
|
||||
Node::Element(elm)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Node {
|
||||
fn from(s: String) -> Node {
|
||||
Node::Text(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for Node {
|
||||
fn from(s: &'a str) -> Node {
|
||||
Node::Text(s.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ElementBuilder> for Node {
|
||||
fn from(builder: ElementBuilder) -> Node {
|
||||
Node::Element(builder.build())
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue