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

@@ -5,18 +5,18 @@ use crate::utils::xml::NodeExt;
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 internal_gateway_list = if use_internal {
info!("Using internal gateways");
node_gateways.find_child("internal").and_then(|n| n.find_child("list"))
pub(crate) fn parse_gateways(node: &Node, prefer_internal: bool) -> Option<Vec<Gateway>> {
let node_gateways = node.find_descendant("gateways")?;
let internal_gateway_list = if prefer_internal {
info!("Try to parse the internal gateways...");
node_gateways.find_descendant("internal").and_then(|n| n.find_child("list"))
} else {
None
};
let gateway_list = internal_gateway_list.or_else(|| {
info!("Using external gateways");
node_gateways.find_child("external").and_then(|n| n.find_child("list"))
info!("Try to parse the external gateways...");
node_gateways.find_descendant("external").and_then(|n| n.find_child("list"))
})?;
let gateways = gateway_list