refactor: improve the XML parsing

This commit is contained in:
Kevin Yue
2024-07-08 13:04:27 +00:00
parent fb8fb21450
commit 5cb9432f21
6 changed files with 60 additions and 34 deletions

View File

@@ -1,7 +1,7 @@
use std::{process::ExitStatus, time::Duration};
use anyhow::bail;
use log::{info, warn};
use log::info;
use tauri::Window;
use tokio::process::Command;
@@ -33,7 +33,7 @@ pub fn raise_window(win: &Window) -> anyhow::Result<()> {
let title = win.title()?;
tokio::spawn(async move {
if let Err(err) = wmctrl_raise_window(&title).await {
warn!("Failed to raise window: {}", err);
info!("Window not raised: {}", err);
}
});
}

View File

@@ -1,17 +1,27 @@
use roxmltree::Node;
pub(crate) trait NodeExt<'a> {
fn find_descendant(&self, name: &str) -> Option<Node<'a, 'a>>;
fn descendant_text(&self, name: &str) -> Option<&'a str>;
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>> {
fn find_descendant(&self, name: &str) -> Option<Node<'a, 'a>> {
self.descendants().find(|n| n.has_tag_name(name))
}
fn descendant_text(&self, name: &str) -> Option<&'a str> {
self.find_descendant(name).and_then(|node| node.text())
}
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()
self.find_child(name).and_then(|node| node.text())
}
}