mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2025-05-20 07:26:58 -04:00
refactor: improve the XML parsing
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user