Trait minidom_ext::OnlyChildElementExt[][src]

pub trait OnlyChildElementExt {
    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>; 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

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.

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

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.

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.

Implementations on Foreign Types

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());

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