asterust_junos/routing_options/
route_attributes.rs1use crate::{active_children, list_values, split_keyword, unquote};
18use asterust::config::ConfigLine;
19
20pub(super) fn route_statements<'a>(inline: &'a str, line: &'a ConfigLine) -> Vec<&'a str> {
23 let mut statements = Vec::new();
24 if !inline.is_empty() {
25 statements.push(inline);
26 }
27 statements.extend(active_children(line).map(|child| child.text.as_str()));
28 statements
29}
30
31#[derive(Clone, Debug, Default, PartialEq, Eq)]
34#[non_exhaustive]
35pub struct RouteAttributes {
36 pub active: Option<bool>,
38 pub install: Option<bool>,
40 pub readvertise: Option<bool>,
41 pub resolve: Option<bool>,
42 pub retain: Option<bool>,
43 pub longest_match: Option<bool>,
44 pub preference: Option<String>,
46 pub preference2: Option<String>,
47 pub metric: Option<String>,
48 pub metric2: Option<String>,
49 pub tag: Option<String>,
50 pub tag2: Option<String>,
51 pub color: Option<String>,
52 pub color2: Option<String>,
53 pub community: Vec<String>,
55 pub description: Option<String>,
56 pub priority: Option<String>,
58 pub backup_pe_group: Option<String>,
59}
60
61pub(super) fn parse_route_attributes<'a>(statements: impl IntoIterator<Item = &'a str>) -> RouteAttributes {
64 let mut attributes = RouteAttributes::default();
65 for statement in statements {
66 let (keyword, arg) = split_keyword(statement);
67 let value = || Some(arg.to_string());
68 match keyword {
69 "active" => attributes.active = Some(true),
70 "passive" => attributes.active = Some(false),
71 "install" => attributes.install = Some(true),
72 "no-install" => attributes.install = Some(false),
73 "readvertise" => attributes.readvertise = Some(true),
74 "no-readvertise" => attributes.readvertise = Some(false),
75 "resolve" => attributes.resolve = Some(true),
76 "no-resolve" => attributes.resolve = Some(false),
77 "retain" => attributes.retain = Some(true),
78 "no-retain" => attributes.retain = Some(false),
79 "longest-match" => attributes.longest_match = Some(true),
80 "no-longest-match" => attributes.longest_match = Some(false),
81 "preference" => attributes.preference = value(),
82 "preference2" => attributes.preference2 = value(),
83 "metric" => attributes.metric = value(),
84 "metric2" => attributes.metric2 = value(),
85 "tag" => attributes.tag = value(),
86 "tag2" => attributes.tag2 = value(),
87 "color" => attributes.color = value(),
88 "color2" => attributes.color2 = value(),
89 "community" => attributes.community.extend(list_values(arg)),
90 "description" => attributes.description = Some(unquote(arg).to_string()),
91 "priority" => attributes.priority = value(),
92 "backup-pe-group" => attributes.backup_pe_group = value(),
93 _ => {}
94 }
95 }
96 attributes
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[test]
105 fn reads_flags_pairs_and_values() {
106 let attributes = parse_route_attributes([
107 "passive",
108 "no-install",
109 "readvertise",
110 "no-resolve",
111 "retain",
112 "no-longest-match",
113 "preference 5 type 2",
114 "metric 3",
115 "tag 7",
116 "color 100",
117 "community [ 65000:1 no-export ]",
118 "description \"to branch\"",
119 "priority high",
120 ]);
121 assert_eq!(attributes.active, Some(false));
122 assert_eq!(attributes.install, Some(false));
123 assert_eq!(attributes.readvertise, Some(true));
124 assert_eq!(attributes.resolve, Some(false));
125 assert_eq!(attributes.retain, Some(true));
126 assert_eq!(attributes.longest_match, Some(false));
127 assert_eq!(attributes.preference.as_deref(), Some("5 type 2"));
128 assert_eq!(attributes.metric.as_deref(), Some("3"));
129 assert_eq!(attributes.tag.as_deref(), Some("7"));
130 assert_eq!(attributes.color.as_deref(), Some("100"));
131 assert_eq!(attributes.community, ["65000:1", "no-export"]);
132 assert_eq!(attributes.description.as_deref(), Some("to branch"));
133 assert_eq!(attributes.priority.as_deref(), Some("high"));
134 }
135
136 #[test]
137 fn ignores_statements_that_are_not_attributes() {
138 let attributes = parse_route_attributes(["next-hop 192.0.2.1", "discard"]);
139 assert_eq!(attributes, RouteAttributes::default());
140 }
141}