asterust-junos 0.5.6

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
//! `prefix-action`
//! `firewall { family inet { prefix-action <name> { ... } } }`.
//!
//! # See also
//!
//! - [prefix-action (Configuring)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/prefix-action-edit-firewall.html)
//! - [prefix-action (Firewall Filter Action)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/prefix-action-edit-firewall-family-filter-term-then.html)

use crate::{arg_of, has_leaf, named_children};
use asterust::config::ConfigLine;

/// A `prefix-action <name>`, which polices or counts per source or destination prefix.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PrefixAction {
    /// The `family` it is under, `"inet"`.
    pub family: String,
    pub name: String,
    pub count: bool,
    pub destination_prefix_length: Option<String>,
    pub filter_specific: bool,
    pub policer: Option<String>,
    pub source_prefix_length: Option<String>,
    pub subnet_prefix_length: Option<String>,
}

/// The `prefix-action` entries under each `family` of `firewall`.
pub(super) fn prefix_actions(firewall: &ConfigLine) -> Vec<PrefixAction> {
    named_children(firewall, "family")
        .flat_map(|(family, family_line)| {
            named_children(family_line, "prefix-action")
                .map(move |(name, line)| parse_prefix_action(family, name, line))
        })
        .collect()
}

fn parse_prefix_action(family: &str, name: &str, line: &ConfigLine) -> PrefixAction {
    let field = |keyword: &str| arg_of(line, keyword).map(str::to_string);
    PrefixAction {
        family: family.to_string(),
        name: name.to_string(),
        count: has_leaf(line, "count"),
        destination_prefix_length: field("destination-prefix-length"),
        filter_specific: has_leaf(line, "filter-specific"),
        policer: field("policer"),
        source_prefix_length: field("source-prefix-length"),
        subnet_prefix_length: field("subnet-prefix-length"),
    }
}