1pub mod device;
16pub mod firewall;
17pub mod interfaces;
18pub mod policy_options;
19pub mod protocols;
20pub mod routing_instances;
21pub mod routing_options;
22pub mod system;
23
24pub use device::JunosDevice;
25
26use asterust::config::{Config, ConfigLine};
27
28pub(crate) fn unquote(value: &str) -> &str {
30 value.trim().trim_matches('"')
31}
32
33pub(crate) fn active_children(line: &ConfigLine) -> impl Iterator<Item = &ConfigLine> {
35 line.children.iter().filter(|c| !c.text.starts_with("inactive:"))
36}
37
38pub(crate) fn arg_of<'a>(line: &'a ConfigLine, keyword: &str) -> Option<&'a str> {
40 active_children(line).find_map(|c| c.arg(keyword)).map(unquote)
41}
42
43pub(crate) fn args_of<'a>(line: &'a ConfigLine, keyword: &str) -> Vec<&'a str> {
45 active_children(line)
46 .filter_map(|c| c.arg(keyword))
47 .map(unquote)
48 .collect()
49}
50
51pub(crate) fn has_leaf(line: &ConfigLine, name: &str) -> bool {
53 active_children(line).any(|c| c.text == name)
54}
55
56pub(crate) fn named_children<'a>(
58 line: &'a ConfigLine,
59 keyword: &'a str,
60) -> impl Iterator<Item = (&'a str, &'a ConfigLine)> {
61 active_children(line).filter_map(move |c| Some((unquote(c.arg(keyword)?), c)))
63}
64
65pub(crate) fn child_block<'a>(line: &'a ConfigLine, name: &str) -> Option<&'a ConfigLine> {
67 active_children(line).find(|c| c.text == name)
68}
69
70pub(crate) fn top_level<'a>(config: &'a Config, name: &str) -> Option<&'a ConfigLine> {
72 config.blocks.iter().map(|b| b.get_root_line()).find(|l| l.text == name)
73}
74
75pub(crate) fn list_values(arg: &str) -> Vec<String> {
77 let inner = arg.trim().trim_start_matches('[').trim_end_matches(']');
78 let mut values = Vec::new();
79 let mut current = String::new();
80 let mut quoted = false;
81 for ch in inner.chars() {
82 match ch {
83 '"' => quoted = !quoted,
84 c if c.is_whitespace() && !quoted => {
85 if !current.is_empty() {
86 values.push(std::mem::take(&mut current));
87 }
88 }
89 c => current.push(c),
90 }
91 }
92 if !current.is_empty() {
93 values.push(current);
94 }
95 values
96}
97
98pub(crate) fn split_name(arg: &str) -> (&str, &str) {
100 if let Some((name, rest)) = arg.strip_prefix('"').and_then(|quoted| quoted.split_once('"')) {
101 return (name, rest.trim());
102 }
103 arg.split_once(char::is_whitespace)
104 .map_or((arg, ""), |(name, rest)| (name, rest.trim()))
105}
106
107pub(crate) fn split_keyword(statement: &str) -> (&str, &str) {
109 statement
110 .split_once(char::is_whitespace)
111 .map_or((statement, ""), |(keyword, arg)| (keyword, arg.trim()))
112}
113
114pub(crate) fn statements<'a>(line: &'a ConfigLine, keyword: &str) -> Vec<&'a str> {
116 let mut found = Vec::new();
117 for child in active_children(line) {
118 if child.text == keyword {
119 found.extend(active_children(child).map(|s| s.text.as_str()));
120 } else if let Some(arg) = child.arg(keyword) {
121 found.push(arg);
122 }
123 }
124 found
125}
126
127pub(crate) fn statement_words<'a>(parent: &'a ConfigLine, keyword: &str) -> Option<Vec<&'a str>> {
129 let statement = active_children(parent).find(|c| c.text == keyword || c.arg(keyword).is_some())?;
130 Some(
131 statement
132 .text
133 .split_whitespace()
134 .skip(1)
135 .chain(active_children(statement).flat_map(|c| c.text.split_whitespace()))
136 .collect(),
137 )
138}
139
140pub(crate) fn value_after(words: &[&str], name: &str) -> Option<String> {
142 let position = words.iter().position(|w| *w == name)?;
143 words.get(position + 1).map(|w| w.to_string())
144}
145
146pub(crate) fn first_word(arg: &str) -> Option<&str> {
148 arg.split_whitespace().next()
149}