asterust_junos/protocols/source_packet_routing/
segment_list.rs1use crate::{active_children, has_leaf, value_after};
10use asterust::config::ConfigLine;
11
12const OPTIONS: [&str; 5] = ["srv6", "compute", "dynamic", "inherit-label-nexthops", "auto-translate"];
14
15#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct SegmentList {
19 pub name: String,
20 pub srv6: bool,
22 pub compute: bool,
23 pub dynamic: bool,
24 pub inherit_label_nexthops: bool,
25 pub auto_translate: bool,
27 pub hops: Vec<SegmentHop>,
28}
29
30#[derive(Clone, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub struct SegmentHop {
34 pub name: String,
35 pub strict: bool,
36 pub loose: bool,
37 pub ip_address: Option<String>,
38 pub label: Option<String>,
39 pub label_type: Option<String>,
41 pub srv6_sid: Option<String>,
42}
43
44pub(super) fn parse_segment_list(name: &str, list: &ConfigLine) -> SegmentList {
45 SegmentList {
46 name: name.to_string(),
47 srv6: has_leaf(list, "srv6"),
48 compute: has_leaf(list, "compute"),
49 dynamic: statement_present(list, "dynamic"),
50 inherit_label_nexthops: has_leaf(list, "inherit-label-nexthops"),
51 auto_translate: statement_present(list, "auto-translate"),
52 hops: active_children(list)
53 .filter(|child| {
54 child
55 .text
56 .split_whitespace()
57 .next()
58 .is_some_and(|first| !OPTIONS.contains(&first))
59 })
60 .map(parse_hop)
61 .collect(),
62 }
63}
64
65fn statement_present(list: &ConfigLine, keyword: &str) -> bool {
66 active_children(list).any(|child| child.text == keyword || child.arg(keyword).is_some())
67}
68
69fn parse_hop(hop: &ConfigLine) -> SegmentHop {
70 let mut words = hop.text.split_whitespace();
71 let name = words.next().unwrap_or_default().to_string();
72 let words: Vec<&str> = words
73 .chain(active_children(hop).flat_map(|child| child.text.split_whitespace()))
74 .collect();
75 SegmentHop {
76 name,
77 strict: words.contains(&"strict"),
78 loose: words.contains(&"loose"),
79 ip_address: value_after(&words, "ip-address"),
80 label: value_after(&words, "label"),
81 label_type: value_after(&words, "label-type"),
82 srv6_sid: value_after(&words, "srv6-sid"),
83 }
84}