asterust-junos 0.5.1

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! `defaults`
//! `policy-options { defaults { ... } }` and `policy-options { policy-statement <name> { defaults { ... } } }`.
//!
//! # See also
//!
//! - [defaults (Policy Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/defaults-edit-policy-options.html)

use crate::{active_children, split_keyword, statements};
use asterust::config::ConfigLine;

/// The `defaults` of `policy-options` or of one policy statement.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PolicyDefaults {
    /// `"optimize"` or `"no-optimize"`.
    pub asregex_optimize: Option<String>,
    /// `"walkup"` or `"no-walkup"`.
    pub route_filter: Option<String>,
}

/// The `defaults` of `line`, or `None` if it has none.
pub(super) fn parse_defaults(line: &ConfigLine) -> Option<PolicyDefaults> {
    if !active_children(line).any(|c| c.text == "defaults" || c.arg("defaults").is_some()) {
        return None;
    }
    let mut defaults = PolicyDefaults::default();
    for statement in statements(line, "defaults") {
        let (keyword, value) = split_keyword(statement);
        let value = Some(value.to_string()).filter(|v| !v.is_empty());
        match keyword {
            "asregex-optimize" => defaults.asregex_optimize = value,
            "route-filter" => defaults.route_filter = value,
            _ => {}
        }
    }
    Some(defaults)
}

#[cfg(test)]
mod tests {
    use crate::policy_options::{policy_options, PolicyDefaults};
    use asterust::config::juniper;

    // Adapted from the Routing Policies, Firewall Filters, and Traffic Policers User Guide, Understanding the Route
    // Filter Walkup Policy Option: the `defaults` of the `RouteFilter-A` policy; see the README "Sources".
    #[test]
    fn reads_the_guide_walkup_default() {
        let config = juniper::parse_braces(
            "policy-options {\n    policy-statement RouteFilter-A {\n        defaults {\n            route-filter walkup;\n        }\n        term RouteFilter-1 {\n            from {\n                route-filter 10.0.0.0/16 prefix-length-range /22-/24;\n            }\n            then accept;\n        }\n    }\n}\n",
        );
        let options = policy_options(&config).unwrap();
        let defaults = options.policy_statements[0].defaults.as_ref().unwrap();
        assert_eq!(defaults.route_filter.as_deref(), Some("walkup"));
        assert_eq!(defaults.asregex_optimize, None);
        assert_eq!(options.defaults, None);
    }

    // Not from documentation: the global form and `asregex-optimize` of the defaults CLI Reference page.
    #[test]
    fn reads_the_global_defaults() {
        let config = juniper::parse_braces(
            "policy-options {\n    defaults {\n        asregex-optimize no-optimize;\n        route-filter no-walkup;\n    }\n}\n",
        );
        assert_eq!(
            policy_options(&config).unwrap().defaults,
            Some(PolicyDefaults {
                asregex_optimize: Some("no-optimize".to_string()),
                route_filter: Some("no-walkup".to_string()),
            })
        );
    }
}