use crate::{arg_of, child_block, has_leaf, named_children};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AutoExportScope {
pub disable: bool,
pub rib_group: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AutoExportFamily {
pub name: String,
pub disable: bool,
pub unicast: Option<AutoExportScope>,
pub multicast: Option<AutoExportScope>,
pub flow: Option<AutoExportScope>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AutoExport {
pub disable: bool,
pub families: Vec<AutoExportFamily>,
}
pub(super) fn parse_auto_export(auto_export: &ConfigLine) -> AutoExport {
AutoExport {
disable: has_leaf(auto_export, "disable"),
families: named_children(auto_export, "family")
.map(|(name, family)| {
let scope = |keyword: &str| {
child_block(family, keyword).map(|scope| AutoExportScope {
disable: has_leaf(scope, "disable"),
rib_group: arg_of(scope, "rib-group").map(str::to_string),
})
};
AutoExportFamily {
name: name.to_string(),
disable: has_leaf(family, "disable"),
unicast: scope("unicast"),
multicast: scope("multicast"),
flow: scope("flow"),
}
})
.collect(),
}
}
#[cfg(test)]
mod tests {
use crate::routing_options::routing_options;
use asterust::config::juniper;
#[test]
fn extracts_auto_export_families() {
let options = routing_options(&juniper::parse_braces(
r#"
routing-options {
auto-export {
family inet {
unicast {
rib-group EXPORT-GROUP;
}
multicast {
disable;
}
}
family inet6 {
disable;
}
}
}
"#,
))
.unwrap();
let auto_export = options.auto_export.unwrap();
assert!(!auto_export.disable);
assert_eq!(auto_export.families.len(), 2);
let inet = &auto_export.families[0];
assert_eq!(inet.name, "inet");
assert!(!inet.disable);
assert_eq!(
inet.unicast.as_ref().unwrap().rib_group.as_deref(),
Some("EXPORT-GROUP")
);
assert!(inet.multicast.as_ref().unwrap().disable);
assert!(inet.flow.is_none());
assert!(auto_export.families[1].disable);
}
#[test]
fn bare_auto_export_enables_with_defaults() {
let options = routing_options(&juniper::parse_braces("routing-options {\n auto-export;\n}\n")).unwrap();
let auto_export = options.auto_export.unwrap();
assert!(!auto_export.disable);
assert!(auto_export.families.is_empty());
}
#[test]
fn no_auto_export_is_none() {
let options = routing_options(&juniper::parse_braces(
"routing-options {\n router-id 192.0.2.1;\n}\n",
))
.unwrap();
assert!(options.auto_export.is_none());
}
}