refactor: improve gateway parsing

This commit is contained in:
Kevin Yue 2024-07-07 13:38:34 +00:00
parent 68227b64a2
commit 370a32f1b5
No known key found for this signature in database
GPG Key ID: 4D3A6EE977B15AC4
2 changed files with 12 additions and 6 deletions

View File

@ -1,3 +1,4 @@
use log::info;
use roxmltree::Node;
use crate::utils::xml::NodeExt;
@ -6,14 +7,19 @@ use super::{Gateway, PriorityRule};
pub(crate) fn parse_gateways(node: &Node, use_internal: bool) -> Option<Vec<Gateway>> {
let node_gateways = node.find_child("gateways")?;
let list_gateway = if use_internal {
node_gateways.find_child("internal")?.find_child("list")?
let internal_gateway_list = if use_internal {
info!("Using internal gateways");
node_gateways.find_child("internal").and_then(|n| n.find_child("list"))
} else {
node_gateways.find_child("external")?.find_child("list")?
None
};
let gateways = list_gateway
let gateway_list = internal_gateway_list.or_else(|| {
info!("Using external gateways");
node_gateways.find_child("external").and_then(|n| n.find_child("list"))
})?;
let gateways = gateway_list
.children()
.filter_map(|gateway_item| {
if !gateway_item.has_tag_name("entry") {

View File

@ -7,7 +7,7 @@ pub(crate) trait NodeExt<'a> {
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))
self.descendants().find(|n| n.has_tag_name(name))
}
fn child_text(&self, name: &str) -> Option<&'a str> {