refactor: improve the XML parsing

This commit is contained in:
Kevin Yue
2024-07-07 18:15:12 +08:00
parent 2a0880bfee
commit 68227b64a2
4 changed files with 106 additions and 86 deletions

View File

@@ -1,6 +1,17 @@
use roxmltree::Document;
use roxmltree::Node;
pub(crate) fn get_child_text(doc: &Document, name: &str) -> Option<String> {
let node = doc.descendants().find(|n| n.has_tag_name(name))?;
node.text().map(|s| s.to_string())
pub(crate) trait NodeExt<'a> {
fn find_child(&self, name: &str) -> Option<Node<'a, 'a>>;
fn child_text(&self, name: &str) -> Option<&'a str>;
}
impl<'a> NodeExt<'a> for Node<'a, 'a> {
fn find_child(&self, name: &str) -> Option<Node<'a, 'a>> {
self.children().find(|n| n.has_tag_name(name))
}
fn child_text(&self, name: &str) -> Option<&'a str> {
let node = self.find_child(name)?;
node.text()
}
}