RawNode

Struct RawNode 

Source
pub struct RawNode<'a>(pub Node<'a, 'a>);
Expand description

Captures subtrees from the source

This type must borrow from the source during serialization and therefore requires the use of the from_doc or from_node entry points. It will however recover only the source document or node lifetime and not the full input lifetime.

use roxmltree::Document;
use serde::Deserialize;
use serde_roxmltree::{from_doc, RawNode};

#[derive(Deserialize)]
struct Record<'a> {
    #[serde(borrow)]
    subtree: RawNode<'a>,
}

let document = Document::parse(r#"<document><subtree><field attribute="bar">foo</field></subtree></document>"#)?;

let record = from_doc::<Record>(&document)?;
assert!(record.subtree.has_tag_name("subtree"));

Tuple Fields§

§0: Node<'a, 'a>

Methods from Deref<Target = Node<'a, 'a>>§

Source

pub fn node_type(&self) -> NodeType

Returns node’s type.

Source

pub fn is_root(&self) -> bool

Checks that node is a root node.

Source

pub fn is_element(&self) -> bool

Checks that node is an element node.

Source

pub fn is_pi(&self) -> bool

Checks that node is a processing instruction node.

Source

pub fn is_comment(&self) -> bool

Checks that node is a comment node.

Source

pub fn is_text(&self) -> bool

Checks that node is a text node.

Source

pub fn document(&self) -> &'a Document<'input>

Returns node’s document.

Source

pub fn tag_name(&self) -> ExpandedName<'a, 'input>

Returns node’s tag name.

Returns an empty name with no namespace if the current node is not an element.

§Examples
let doc = roxmltree::Document::parse("<e xmlns='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().tag_name().namespace(), Some("http://www.w3.org"));
assert_eq!(doc.root_element().tag_name().name(), "e");
Source

pub fn has_tag_name<'n, 'm, N>(&self, name: N) -> bool
where N: Into<ExpandedName<'n, 'm>>,

Checks that node has a specified tag name.

§Examples
let doc = roxmltree::Document::parse("<e xmlns='http://www.w3.org'/>").unwrap();

assert!(doc.root_element().has_tag_name("e"));
assert!(doc.root_element().has_tag_name(("http://www.w3.org", "e")));

assert!(!doc.root_element().has_tag_name("b"));
assert!(!doc.root_element().has_tag_name(("http://www.w4.org", "e")));
Source

pub fn default_namespace(&self) -> Option<&'a str>

Returns node’s default namespace URI.

§Examples
let doc = roxmltree::Document::parse("<e xmlns='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().default_namespace(), Some("http://www.w3.org"));
let doc = roxmltree::Document::parse("<e xmlns:n='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().default_namespace(), None);
Source

pub fn lookup_prefix(&self, uri: &str) -> Option<&'input str>

Returns a prefix for a given namespace URI.

§Examples
let doc = roxmltree::Document::parse("<e xmlns:n='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().lookup_prefix("http://www.w3.org"), Some("n"));
let doc = roxmltree::Document::parse("<e xmlns:n=''/>").unwrap();

assert_eq!(doc.root_element().lookup_prefix(""), Some("n"));
Source

pub fn lookup_namespace_uri(&self, prefix: Option<&str>) -> Option<&'a str>

Returns an URI for a given prefix.

§Examples
let doc = roxmltree::Document::parse("<e xmlns:n='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().lookup_namespace_uri(Some("n")), Some("http://www.w3.org"));
let doc = roxmltree::Document::parse("<e xmlns='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().lookup_namespace_uri(None), Some("http://www.w3.org"));
Source

pub fn attribute<'n, 'm, N>(&self, name: N) -> Option<&'a str>
where N: Into<ExpandedName<'n, 'm>>,

Returns element’s attribute value.

§Examples
let doc = roxmltree::Document::parse("<e a='b'/>").unwrap();

assert_eq!(doc.root_element().attribute("a"), Some("b"));
let doc = roxmltree::Document::parse(
    "<e xmlns:n='http://www.w3.org' a='b' n:a='c'/>"
).unwrap();

assert_eq!(doc.root_element().attribute("a"), Some("b"));
assert_eq!(doc.root_element().attribute(("http://www.w3.org", "a")), Some("c"));
Source

pub fn attribute_node<'n, 'm, N>( &self, name: N, ) -> Option<Attribute<'a, 'input>>
where N: Into<ExpandedName<'n, 'm>>,

Returns element’s attribute object.

The same as attribute(), but returns the Attribute itself instead of a value string.

Source

pub fn has_attribute<'n, 'm, N>(&self, name: N) -> bool
where N: Into<ExpandedName<'n, 'm>>,

Checks that element has a specified attribute.

§Examples
let doc = roxmltree::Document::parse(
    "<e xmlns:n='http://www.w3.org' a='b' n:a='c'/>"
).unwrap();

assert!(doc.root_element().has_attribute("a"));
assert!(doc.root_element().has_attribute(("http://www.w3.org", "a")));

assert!(!doc.root_element().has_attribute("b"));
assert!(!doc.root_element().has_attribute(("http://www.w4.org", "a")));
Source

pub fn attributes(&self) -> Attributes<'a, 'input>

Returns element’s attributes.

§Examples
let doc = roxmltree::Document::parse(
    "<e xmlns:n='http://www.w3.org' a='b' n:a='c'/>"
).unwrap();

assert_eq!(doc.root_element().attributes().len(), 2);
Source

pub fn namespaces(&self) -> NamespaceIter<'a, 'input>

Returns element’s namespaces.

§Examples
let doc = roxmltree::Document::parse(
    "<e xmlns:n='http://www.w3.org'/>"
).unwrap();

assert_eq!(doc.root_element().namespaces().len(), 1);
Source

pub fn text(&self) -> Option<&'a str>

Returns node’s text.

  • for an element will return a first text child
  • for a comment will return a self text
  • for a text node will return a self text
§Examples
let doc = roxmltree::Document::parse("\
<p>
    text
</p>
").unwrap();

assert_eq!(doc.root_element().text(),
           Some("\n    text\n"));
assert_eq!(doc.root_element().first_child().unwrap().text(),
           Some("\n    text\n"));
let doc = roxmltree::Document::parse("<!-- comment --><e/>").unwrap();

assert_eq!(doc.root().first_child().unwrap().text(), Some(" comment "));
Source

pub fn text_storage(&self) -> Option<&'a StringStorage<'input>>

Returns node’s text storage.

Useful when you need a more low-level access to an allocated string.

Source

pub fn tail(&self) -> Option<&'a str>

Returns element’s tail text.

§Examples
let doc = roxmltree::Document::parse("\
<root>
    text1
    <p/>
    text2
</root>
").unwrap();

let p = doc.descendants().find(|n| n.has_tag_name("p")).unwrap();
assert_eq!(p.tail(), Some("\n    text2\n"));
Source

pub fn tail_storage(&self) -> Option<&'a StringStorage<'input>>

Returns element’s tail text storage.

Useful when you need a more low-level access to an allocated string.

Source

pub fn pi(&self) -> Option<PI<'input>>

Returns node as Processing Instruction.

Source

pub fn parent(&self) -> Option<Node<'a, 'input>>

Returns the parent of this node.

Source

pub fn parent_element(&self) -> Option<Node<'a, 'input>>

Returns the parent element of this node.

Source

pub fn prev_sibling(&self) -> Option<Node<'a, 'input>>

Returns the previous sibling of this node.

Source

pub fn prev_sibling_element(&self) -> Option<Node<'a, 'input>>

Returns the previous sibling element of this node.

Source

pub fn next_sibling(&self) -> Option<Node<'a, 'input>>

Returns the next sibling of this node.

Source

pub fn next_sibling_element(&self) -> Option<Node<'a, 'input>>

Returns the next sibling element of this node.

Source

pub fn first_child(&self) -> Option<Node<'a, 'input>>

Returns the first child of this node.

Source

pub fn first_element_child(&self) -> Option<Node<'a, 'input>>

Returns the first element child of this node.

Source

pub fn last_child(&self) -> Option<Node<'a, 'input>>

Returns the last child of this node.

Source

pub fn last_element_child(&self) -> Option<Node<'a, 'input>>

Returns the last element child of this node.

Source

pub fn has_siblings(&self) -> bool

Returns true if this node has siblings.

Source

pub fn has_children(&self) -> bool

Returns true if this node has children.

Source

pub fn ancestors(&self) -> AxisIter<'a, 'input>

Returns an iterator over ancestor nodes starting at this node.

Source

pub fn prev_siblings(&self) -> AxisIter<'a, 'input>

Returns an iterator over previous sibling nodes starting at this node.

Source

pub fn next_siblings(&self) -> AxisIter<'a, 'input>

Returns an iterator over next sibling nodes starting at this node.

Source

pub fn first_children(&self) -> AxisIter<'a, 'input>

Returns an iterator over first children nodes starting at this node.

Source

pub fn last_children(&self) -> AxisIter<'a, 'input>

Returns an iterator over last children nodes starting at this node.

Source

pub fn children(&self) -> Children<'a, 'input>

Returns an iterator over children nodes.

Source

pub fn descendants(&self) -> Descendants<'a, 'input>

Returns an iterator over this node and its descendants.

Source

pub fn id(&self) -> NodeId

Returns node’s NodeId

Trait Implementations§

Source§

impl<'a> Clone for RawNode<'a>

Source§

fn clone(&self) -> RawNode<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for RawNode<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Deref for RawNode<'a>

Source§

type Target = Node<'a, 'a>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'de, 'a> Deserialize<'de> for RawNode<'a>
where 'de: 'a,

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a> Hash for RawNode<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> Ord for RawNode<'a>

Source§

fn cmp(&self, other: &RawNode<'a>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a> PartialEq for RawNode<'a>

Source§

fn eq(&self, other: &RawNode<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialOrd for RawNode<'a>

Source§

fn partial_cmp(&self, other: &RawNode<'a>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> Copy for RawNode<'a>

Source§

impl<'a> Eq for RawNode<'a>

Source§

impl<'a> StructuralPartialEq for RawNode<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for RawNode<'a>

§

impl<'a> RefUnwindSafe for RawNode<'a>

§

impl<'a> Send for RawNode<'a>

§

impl<'a> Sync for RawNode<'a>

§

impl<'a> Unpin for RawNode<'a>

§

impl<'a> UnwindSafe for RawNode<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,