pub mod bgp;
pub mod device;
pub mod firewall;
pub mod interface;
pub mod isis;
pub mod ospf;
pub mod policy_options;
pub mod routing_instance;
pub mod routing_options;
pub mod system;
pub use device::JunosDevice;
use asterust::config::{Config, ConfigLine};
pub(crate) fn unquote(value: &str) -> &str {
value.trim().trim_matches('"')
}
pub(crate) fn active_children(line: &ConfigLine) -> impl Iterator<Item = &ConfigLine> {
line.children.iter().filter(|c| !c.text.starts_with("inactive:"))
}
pub(crate) fn arg_of<'a>(line: &'a ConfigLine, keyword: &str) -> Option<&'a str> {
active_children(line).find_map(|c| c.arg(keyword)).map(unquote)
}
pub(crate) fn args_of<'a>(line: &'a ConfigLine, keyword: &str) -> Vec<&'a str> {
active_children(line)
.filter_map(|c| c.arg(keyword))
.map(unquote)
.collect()
}
pub(crate) fn has_leaf(line: &ConfigLine, name: &str) -> bool {
active_children(line).any(|c| c.text == name)
}
pub(crate) fn named_children<'a>(
line: &'a ConfigLine,
keyword: &'a str,
) -> impl Iterator<Item = (&'a str, &'a ConfigLine)> {
active_children(line).filter_map(move |c| Some((unquote(c.arg(keyword)?), c)))
}
pub(crate) fn child_block<'a>(line: &'a ConfigLine, name: &str) -> Option<&'a ConfigLine> {
active_children(line).find(|c| c.text == name)
}
pub(crate) fn top_level<'a>(config: &'a Config, name: &str) -> Option<&'a ConfigLine> {
config.blocks.iter().map(|b| b.get_root_line()).find(|l| l.text == name)
}
pub(crate) fn list_values(arg: &str) -> Vec<String> {
let inner = arg.trim().trim_start_matches('[').trim_end_matches(']');
let mut values = Vec::new();
let mut current = String::new();
let mut quoted = false;
for ch in inner.chars() {
match ch {
'"' => quoted = !quoted,
c if c.is_whitespace() && !quoted => {
if !current.is_empty() {
values.push(std::mem::take(&mut current));
}
}
c => current.push(c),
}
}
if !current.is_empty() {
values.push(current);
}
values
}
pub(crate) fn split_name(arg: &str) -> (&str, &str) {
if let Some((name, rest)) = arg.strip_prefix('"').and_then(|quoted| quoted.split_once('"')) {
return (name, rest.trim());
}
arg.split_once(char::is_whitespace)
.map_or((arg, ""), |(name, rest)| (name, rest.trim()))
}
pub(crate) fn split_keyword(statement: &str) -> (&str, &str) {
statement
.split_once(char::is_whitespace)
.map_or((statement, ""), |(keyword, arg)| (keyword, arg.trim()))
}
pub(crate) fn statements<'a>(line: &'a ConfigLine, keyword: &str) -> Vec<&'a str> {
let mut found = Vec::new();
for child in active_children(line) {
if child.text == keyword {
found.extend(active_children(child).map(|s| s.text.as_str()));
} else if let Some(arg) = child.arg(keyword) {
found.push(arg);
}
}
found
}
pub(crate) fn statement_words<'a>(parent: &'a ConfigLine, keyword: &str) -> Option<Vec<&'a str>> {
let statement = active_children(parent).find(|c| c.text == keyword || c.arg(keyword).is_some())?;
Some(
statement
.text
.split_whitespace()
.skip(1)
.chain(active_children(statement).flat_map(|c| c.text.split_whitespace()))
.collect(),
)
}
pub(crate) fn first_word(arg: &str) -> Option<&str> {
arg.split_whitespace().next()
}