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