use crate::{active_children, arg_of, has_leaf, value_after};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SourceRoutingPath {
pub name: String,
pub srv6: bool,
pub to: Option<String>,
pub from: Option<String>,
pub color: Option<String>,
pub binding_sid: Option<String>,
pub metric: Option<String>,
pub no_ingress: bool,
pub sr_preference: Option<String>,
pub te_group_id: Option<String>,
pub lsp_external_controller: Option<String>,
pub primary: Vec<PathSegmentList>,
pub secondary: Vec<PathSegmentList>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PathSegmentList {
pub name: String,
pub weight: Option<String>,
pub return_path: Option<String>,
pub compute: Option<String>,
}
pub(super) fn parse_source_routing_path(name: &str, path: &ConfigLine) -> SourceRoutingPath {
let field = |keyword: &str| arg_of(path, keyword).map(str::to_string);
SourceRoutingPath {
name: name.to_string(),
srv6: has_leaf(path, "srv6"),
to: field("to"),
from: field("from"),
color: field("color"),
binding_sid: field("binding-sid"),
metric: field("metric"),
no_ingress: has_leaf(path, "no-ingress"),
sr_preference: field("sr-preference"),
te_group_id: field("te-group-id"),
lsp_external_controller: field("lsp-external-controller"),
primary: parse_segment_lists(path, "primary"),
secondary: parse_segment_lists(path, "secondary"),
}
}
fn parse_segment_lists(path: &ConfigLine, keyword: &str) -> Vec<PathSegmentList> {
let mut lists = Vec::new();
for statement in active_children(path).filter(|c| c.text == keyword || c.arg(keyword).is_some()) {
match statement.arg(keyword) {
Some(inline) => lists.push(parse_entry(inline, statement)),
None => lists.extend(active_children(statement).map(|entry| parse_entry(&entry.text, entry))),
}
}
lists
}
fn parse_entry(text: &str, node: &ConfigLine) -> PathSegmentList {
let mut words = text.split_whitespace();
let name = words.next().unwrap_or_default().to_string();
let words: Vec<&str> = words
.chain(active_children(node).flat_map(|child| child.text.split_whitespace()))
.collect();
PathSegmentList {
name,
weight: value_after(&words, "weight"),
return_path: value_after(&words, "return-path"),
compute: value_after(&words, "compute"),
}
}