1pub mod device;
34pub mod firewall;
35pub mod interfaces;
36pub mod policy_options;
37pub mod protocols;
38pub mod routing_instances;
39pub mod routing_options;
40pub mod system;
41
42pub use device::JunosDevice;
43
44use asterust::config::{Config, ConfigLine};
45
46pub(crate) fn unquote(value: &str) -> &str {
48 value.trim().trim_matches('"')
49}
50
51pub(crate) fn active_children(line: &ConfigLine) -> impl Iterator<Item = &ConfigLine> {
53 line.children.iter().filter(|c| !c.text.starts_with("inactive:"))
54}
55
56pub(crate) fn arg_of<'a>(line: &'a ConfigLine, keyword: &str) -> Option<&'a str> {
58 active_children(line).find_map(|c| c.arg(keyword)).map(unquote)
59}
60
61pub(crate) fn args_of<'a>(line: &'a ConfigLine, keyword: &str) -> Vec<&'a str> {
63 active_children(line)
64 .filter_map(|c| c.arg(keyword))
65 .map(unquote)
66 .collect()
67}
68
69pub(crate) fn has_leaf(line: &ConfigLine, name: &str) -> bool {
71 active_children(line).any(|c| c.text == name)
72}
73
74pub(crate) fn toggle(line: &ConfigLine, name: &str) -> Option<bool> {
77 let written = |statement: &str| active_children(line).any(|c| c.text == statement || c.arg(statement).is_some());
78 if written(name) {
79 Some(true)
80 } else if written(&format!("no-{name}")) {
81 Some(false)
82 } else {
83 None
84 }
85}
86
87pub(crate) fn named_children<'a>(
89 line: &'a ConfigLine,
90 keyword: &'a str,
91) -> impl Iterator<Item = (&'a str, &'a ConfigLine)> {
92 active_children(line).filter_map(move |c| Some((unquote(c.arg(keyword)?), c)))
94}
95
96pub(crate) fn child_block<'a>(line: &'a ConfigLine, name: &str) -> Option<&'a ConfigLine> {
98 active_children(line).find(|c| c.text == name)
99}
100
101pub(crate) fn top_level<'a>(config: &'a Config, name: &str) -> Option<&'a ConfigLine> {
103 config.blocks.iter().map(|b| b.get_root_line()).find(|l| l.text == name)
104}
105
106pub(crate) fn list_values(arg: &str) -> Vec<String> {
108 let inner = arg.trim().trim_start_matches('[').trim_end_matches(']');
109 let mut values = Vec::new();
110 let mut current = String::new();
111 let mut quoted = false;
112 for ch in inner.chars() {
113 match ch {
114 '"' => quoted = !quoted,
115 c if c.is_whitespace() && !quoted => {
116 if !current.is_empty() {
117 values.push(std::mem::take(&mut current));
118 }
119 }
120 c => current.push(c),
121 }
122 }
123 if !current.is_empty() {
124 values.push(current);
125 }
126 values
127}
128
129pub(crate) fn split_name(arg: &str) -> (&str, &str) {
131 if let Some((name, rest)) = arg.strip_prefix('"').and_then(|quoted| quoted.split_once('"')) {
132 return (name, rest.trim());
133 }
134 arg.split_once(char::is_whitespace)
135 .map_or((arg, ""), |(name, rest)| (name, rest.trim()))
136}
137
138pub(crate) fn split_keyword(statement: &str) -> (&str, &str) {
140 statement
141 .split_once(char::is_whitespace)
142 .map_or((statement, ""), |(keyword, arg)| (keyword, arg.trim()))
143}
144
145pub(crate) fn statements<'a>(line: &'a ConfigLine, keyword: &str) -> Vec<&'a str> {
147 let mut found = Vec::new();
148 for child in active_children(line) {
149 if child.text == keyword {
150 found.extend(active_children(child).map(|s| s.text.as_str()));
151 } else if let Some(arg) = child.arg(keyword) {
152 found.push(arg);
153 }
154 }
155 found
156}
157
158pub(crate) fn statement_words<'a>(parent: &'a ConfigLine, keyword: &str) -> Option<Vec<&'a str>> {
161 let statement = active_children(parent).find(|c| c.text == keyword || c.arg(keyword).is_some())?;
162 Some(
163 statement
164 .text
165 .split_whitespace()
166 .skip(1)
167 .chain(active_children(statement).flat_map(|c| c.text.split_whitespace()))
168 .collect(),
169 )
170}
171
172pub(crate) fn value_after(words: &[&str], name: &str) -> Option<String> {
174 let position = words.iter().position(|w| *w == name)?;
175 words.get(position + 1).map(|w| w.to_string())
176}
177
178pub(crate) fn first_word(arg: &str) -> Option<&str> {
180 arg.split_whitespace().next()
181}