asterust-junos 0.5.5

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `mac-list` and `rib-list`
//! `policy-options { mac-list <name> { ... } }` and `policy-options { rib-list <name> { ... } }`.
//!
//! # See also
//!
//! - [mac-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mac-list-edit-policy-options.html)
//! - [rib-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rib-list-edit-policy-options.html)

use crate::{active_children, args_of};
use asterust::config::ConfigLine;

/// A `mac-list <name>`, which a `from mac-filter-list` condition refers to.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MacList {
    pub name: String,
    pub mac_addresses: Vec<String>,
}

/// A `rib-list <name>`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RibList {
    pub name: String,
    /// The `rib-list-item` names.
    pub items: Vec<String>,
}

pub(super) fn parse_mac_list(name: &str, line: &ConfigLine) -> MacList {
    MacList {
        name: name.to_string(),
        // Addresses are the only statements without arguments; `apply-groups` and the like have a space.
        mac_addresses: active_children(line)
            .filter(|c| !c.text.contains(' '))
            .map(|c| c.text.clone())
            .collect(),
    }
}

pub(super) fn parse_rib_list(name: &str, line: &ConfigLine) -> RibList {
    RibList {
        name: name.to_string(),
        items: args_of(line, "rib-list-item").into_iter().map(str::to_string).collect(),
    }
}

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

    // Not from documentation: written from the mac-list and rib-list CLI Reference pages. See the README "Sources".
    #[test]
    fn reads_mac_and_rib_lists() {
        let config = juniper::parse_braces(
            "policy-options {\n    mac-list MACS {\n        00:11:22:33:44:55;\n        00:11:22:33:44:66;\n    }\n    rib-list RIBS {\n        rib-list-item inet.0;\n        rib-list-item vrf-a.inet.0;\n    }\n}\n",
        );
        let options = policy_options(&config).unwrap();
        assert_eq!(options.mac_lists[0].name, "MACS");
        assert_eq!(
            options.mac_lists[0].mac_addresses,
            ["00:11:22:33:44:55", "00:11:22:33:44:66"]
        );
        assert_eq!(options.rib_lists[0].name, "RIBS");
        assert_eq!(options.rib_lists[0].items, ["inet.0", "vrf-a.inet.0"]);
    }
}