OnlyChildElementExt

Trait OnlyChildElementExt 

Source
pub trait OnlyChildElementExt {
    // Required methods
    fn try_find_only_child<'a, P>(
        &'a self,
        predicate: P,
    ) -> Result<&'a Self, Error>
       where P: Fn(&'a Self) -> bool;
    fn try_only_child<'a>(&'a self, child_name: &str) -> Result<&'a Self, Error>;

    // Provided methods
    fn find_only_child<'a, P>(&'a self, predicate: P) -> Option<&'a Self>
       where P: Fn(&'a Self) -> bool { ... }
    fn only_child<'a>(&'a self, child_name: &str) -> Option<&'a Self> { ... }
}
Expand description

Get the one and only child of an element.

If no children or more than two children are found, it is considered an error.

Required Methods§

Source

fn try_find_only_child<'a, P>(&'a self, predicate: P) -> Result<&'a Self, Error>
where P: Fn(&'a Self) -> bool,

Try to get the unique child of an element.

To select this element, a predicate is specified taking the element as an input and returning a boolean.

The function returns a Result with an error if there is none NoChildrenFound or more than one MultipleChildrenFound selected elements by the predicate above.

Source

fn try_only_child<'a>(&'a self, child_name: &str) -> Result<&'a Self, Error>

Try to get an unique child from its name and return a Result.

Returns an Error if the child can’t be found (NoChildren) or if the child is not unique (MultipleChildren)

Provided Methods§

Source

fn find_only_child<'a, P>(&'a self, predicate: P) -> Option<&'a Self>
where P: Fn(&'a Self) -> bool,

Get the unique child of an element.

To select this element, a predicate is specified taking the element as an input and returning a boolean.

The function returns an Option with the child if there is one and only one child corresponding to the predicate.

Source

fn only_child<'a>(&'a self, child_name: &str) -> Option<&'a Self>

Get a unique child from its name and return an Option.

Returns None if the child can’t be found or if the child is not unique.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl OnlyChildElementExt for Element

Source§

fn try_find_only_child<'a, P>(&'a self, predicate: P) -> Result<&'a Self, Error>
where P: Fn(&'a Self) -> bool,

Implementation of OnlyChildElementExt for Element gives you the ability to select one and only one child of an XML tag depending on a predicate. If none or more than two children are found with the predicate, an error is returned.

use minidom::Element;
use minidom_ext::OnlyChildElementExt;

let xml: &'static str = r#"<root>
        <child type="ugly" />
        <child />
    </root>"#;
let root: Element = xml.parse().unwrap();
let child = root
    .try_find_only_child(|e| {
        e.name() == "child" && e.attr("type").map(|id| id == "ugly").unwrap_or(false)
    })
    .unwrap();
assert_eq!("child", child.name());
Source§

fn try_only_child<'a>(&'a self, child_name: &str) -> Result<&'a Self, Error>

Implementation of OnlyChildElementExt for Element gives you the ability to select one and only one child of an XML tag depending on its name. If none or more than two are found with the name, an error is returned.

use minidom::Element;
use minidom_ext::OnlyChildElementExt;

let xml: &'static str = r#"<root>
        <child />
    </root>"#;
let root: Element = xml.parse().unwrap();
let child = root
    .try_only_child("child")
    .unwrap();
assert_eq!("child", child.name());

Implementors§