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 toggle(line: &ConfigLine, name: &str) -> Option<bool> {
58 let written = |statement: &str| active_children(line).any(|c| c.text == statement || c.arg(statement).is_some());
59 if written(name) {
60 Some(true)
61 } else if written(&format!("no-{name}")) {
62 Some(false)
63 } else {
64 None
65 }
66}
67
68pub(crate) fn named_children<'a>(
70 line: &'a ConfigLine,
71 keyword: &'a str,
72) -> impl Iterator<Item = (&'a str, &'a ConfigLine)> {
73 active_children(line).filter_map(move |c| Some((unquote(c.arg(keyword)?), c)))
75}
76
77pub(crate) fn child_block<'a>(line: &'a ConfigLine, name: &str) -> Option<&'a ConfigLine> {
79 active_children(line).find(|c| c.text == name)
80}
81
82pub(crate) fn top_level<'a>(config: &'a Config, name: &str) -> Option<&'a ConfigLine> {
84 config.blocks.iter().map(|b| b.get_root_line()).find(|l| l.text == name)
85}
86
87pub(crate) fn list_values(arg: &str) -> Vec<String> {
89 let inner = arg.trim().trim_start_matches('[').trim_end_matches(']');
90 let mut values = Vec::new();
91 let mut current = String::new();
92 let mut quoted = false;
93 for ch in inner.chars() {
94 match ch {
95 '"' => quoted = !quoted,
96 c if c.is_whitespace() && !quoted => {
97 if !current.is_empty() {
98 values.push(std::mem::take(&mut current));
99 }
100 }
101 c => current.push(c),
102 }
103 }
104 if !current.is_empty() {
105 values.push(current);
106 }
107 values
108}
109
110pub(crate) fn split_name(arg: &str) -> (&str, &str) {
112 if let Some((name, rest)) = arg.strip_prefix('"').and_then(|quoted| quoted.split_once('"')) {
113 return (name, rest.trim());
114 }
115 arg.split_once(char::is_whitespace)
116 .map_or((arg, ""), |(name, rest)| (name, rest.trim()))
117}
118
119pub(crate) fn split_keyword(statement: &str) -> (&str, &str) {
121 statement
122 .split_once(char::is_whitespace)
123 .map_or((statement, ""), |(keyword, arg)| (keyword, arg.trim()))
124}
125
126pub(crate) fn statements<'a>(line: &'a ConfigLine, keyword: &str) -> Vec<&'a str> {
128 let mut found = Vec::new();
129 for child in active_children(line) {
130 if child.text == keyword {
131 found.extend(active_children(child).map(|s| s.text.as_str()));
132 } else if let Some(arg) = child.arg(keyword) {
133 found.push(arg);
134 }
135 }
136 found
137}
138
139pub(crate) fn statement_words<'a>(parent: &'a ConfigLine, keyword: &str) -> Option<Vec<&'a str>> {
141 let statement = active_children(parent).find(|c| c.text == keyword || c.arg(keyword).is_some())?;
142 Some(
143 statement
144 .text
145 .split_whitespace()
146 .skip(1)
147 .chain(active_children(statement).flat_map(|c| c.text.split_whitespace()))
148 .collect(),
149 )
150}
151
152pub(crate) fn value_after(words: &[&str], name: &str) -> Option<String> {
154 let position = words.iter().position(|w| *w == name)?;
155 words.get(position + 1).map(|w| w.to_string())
156}
157
158pub(crate) fn first_word(arg: &str) -> Option<&str> {
160 arg.split_whitespace().next()
161}