use crate::{active_children, arg_of, args_of, has_leaf, list_values, split_name, toggle};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct MplsAdminGroupConstraints {
pub exclude: Vec<String>,
pub include_all: Vec<String>,
pub include_any: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MplsLspPath {
pub name: String,
pub adaptive: bool,
pub admin_group: MplsAdminGroupConstraints,
pub bandwidth: Option<String>,
pub class_of_service: Option<String>,
pub hop_limit: Option<String>,
pub no_cspf: bool,
pub no_decrement_ttl: bool,
pub optimize_timer: Option<String>,
pub preference: Option<String>,
pub priority: Option<String>,
pub record: Option<bool>,
pub select: Option<String>,
pub standby: bool,
}
pub(super) fn parse_admin_group(line: &ConfigLine) -> MplsAdminGroupConstraints {
let Some(block) = active_children(line).find(|c| c.text == "admin-group") else {
return MplsAdminGroupConstraints::default();
};
let list = |keyword: &str| -> Vec<String> { args_of(block, keyword).into_iter().flat_map(list_values).collect() };
MplsAdminGroupConstraints {
exclude: list("exclude"),
include_all: list("include-all"),
include_any: list("include-any"),
}
}
pub(super) fn parse_paths(lsp: &ConfigLine, keyword: &str) -> Vec<MplsLspPath> {
crate::named_children(lsp, keyword)
.map(|(arg, path)| parse_path(arg, path))
.collect()
}
fn parse_path(arg: &str, path: &ConfigLine) -> MplsLspPath {
let (name, rest) = split_name(arg);
let field = |keyword: &str| arg_of(path, keyword).map(str::to_string);
MplsLspPath {
name: name.to_string(),
adaptive: has_leaf(path, "adaptive"),
admin_group: parse_admin_group(path),
bandwidth: field("bandwidth"),
class_of_service: field("class-of-service"),
hop_limit: field("hop-limit"),
no_cspf: has_leaf(path, "no-cspf"),
no_decrement_ttl: has_leaf(path, "no-decrement-ttl"),
optimize_timer: field("optimize-timer"),
preference: field("preference"),
priority: field("priority"),
record: toggle(path, "record"),
select: field("select"),
standby: has_leaf(path, "standby") || rest.split_whitespace().any(|word| word == "standby"),
}
}