use crate::errors::XmlError;
pub struct AsyncResult {
id: String,
}
impl AsyncResult {
pub fn from_xml(xml: &str) -> Result<Self, XmlError> {
let doc = roxmltree::Document::parse(xml)?;
let body = find_element(doc.root_element(), "Body")?;
let retrieve_response = find_element(body, "retrieveResponse")?;
let result = find_element(retrieve_response, "result")?;
Ok(Self {
id: parse_text_child(result, "id")?,
})
}
pub fn id(&self) -> &str {
&self.id
}
}
fn find_element<'a, 'input>(
root: roxmltree::Node<'a, 'input>,
local_name: &'static str,
) -> Result<roxmltree::Node<'a, 'input>, XmlError> {
root.descendants()
.find(|node| node.is_element() && node.tag_name().name() == local_name)
.ok_or(XmlError::MissingElement(local_name))
}
fn parse_text_child<'a, 'input>(
root: roxmltree::Node<'a, 'input>,
local_name: &'static str,
) -> Result<String, XmlError> {
let node = find_element(root, local_name)?;
node.text()
.map(ToOwned::to_owned)
.ok_or(XmlError::MissingText(local_name))
}