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