1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use failure::{format_err, Error};
use roxmltree::Node;
use try_from::{TryFrom, TryInto};

pub mod assertion;
pub mod encryption;
pub mod response;
pub mod signature;

pub(crate) fn try_child<'a, 'd: 'a>(
    node: Node<'a, 'd>,
    element_name: &str,
) -> Result<Node<'a, 'd>, Error> {
    node.children()
        .find(|c| c.tag_name().name() == element_name)
        .ok_or_else(|| {
            format_err!(
                "{} element not found within {} at {}",
                element_name,
                node.tag_name().name(),
                node.node_pos()
            )
        })
}

pub(crate) fn maybe_child<'a, 'd: 'a, T>(
    node: Node<'a, 'd>,
    element_name: &str,
) -> Result<Option<T>, Error>
where
    T: TryFrom<Node<'a, 'd>, Err = Error>,
{
    node.children()
        .find(|c| c.tag_name().name() == element_name)
        .map(|c| c.try_into())
        .transpose()
}

pub(crate) fn try_attribute<'a, 'd: 'a>(
    node: Node<'a, 'd>,
    attribute_name: &str,
) -> Result<String, Error> {
    node.attribute(attribute_name)
        .map(|a| a.into())
        .ok_or_else(|| {
            format_err!(
                "{} attribute not found within {} at {}",
                attribute_name,
                node.tag_name().name(),
                node.node_pos()
            )
        })
}