asterust-junos 0.5.3

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
//! `as-list`
//! `policy-options { as-list <name> members [ ... ]; }`.
//!
//! # See also
//!
//! - [as-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/as-list-edit-policy-options.html)

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

/// An `as-list <name>`, a list of AS numbers or ranges that `as-path-neighbors`, `as-path-origins` and
/// `as-path-transits` conditions refer to.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AsList {
    pub name: String,
    /// The `members`, e.g. `"65000"` or `"1-65535"`.
    pub members: Vec<String>,
}

/// An `as-list` written either on one line (`as-list X members 1-65535;`) or as a block.
pub(super) fn parse_as_list(line: &ConfigLine) -> Option<AsList> {
    let (name, rest) = split_name(line.arg("as-list")?);
    let (keyword, members) = split_keyword(rest);
    let mut list = AsList {
        name: name.to_string(),
        members: if keyword == "members" {
            list_values(members)
        } else {
            Vec::new()
        },
    };
    for child in active_children(line) {
        if let Some(members) = child.arg("members") {
            list.members.extend(list_values(members));
        }
    }
    Some(list)
}

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

    // Adapted from the BGP User Guide, Configuring BGP Auto-Discovered Neighbors: the `as-list` of the `show
    // policy-options` output; see the README "Sources".
    #[test]
    fn reads_the_guide_as_list() {
        let config = juniper::parse_braces(
            "policy-options {\n    policy-statement DIRECT-RTS {\n        from protocol direct;\n        then accept;\n    }\n    as-list a-list members 1-65535;\n}\n",
        );
        let lists = policy_options(&config).unwrap().as_lists;
        assert_eq!(lists.len(), 1);
        assert_eq!(lists[0].name, "a-list");
        assert_eq!(lists[0].members, ["1-65535"]);
    }

    // Not from documentation: the block form and the member list of the as-list CLI Reference page.
    #[test]
    fn reads_the_block_form() {
        let config = juniper::parse_braces(
            "policy-options {\n    as-list PEERS {\n        members [ 65001 65002 ];\n    }\n}\n",
        );
        let lists = policy_options(&config).unwrap().as_lists;
        assert_eq!(lists[0].members, ["65001", "65002"]);
    }
}