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
//! `three-color-policer`
//! `firewall { three-color-policer <name> { ... } }`.
//!
//! # See also
//!
//! - [three-color-policer (Configuring)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/three-color-policer-edit-firewall.html)

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

/// The `single-rate` or `two-rate` settings of a three-color policer.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ThreeColorRate {
    /// `Some(true)` for `color-aware`, `Some(false)` for `color-blind`.
    pub color_aware: Option<bool>,
    pub committed_information_rate: Option<String>,
    pub committed_burst_size: Option<String>,
    /// Only `single-rate`.
    pub excess_burst_size: Option<String>,
    /// Only `two-rate`.
    pub peak_information_rate: Option<String>,
    /// Only `two-rate`.
    pub peak_burst_size: Option<String>,
}

/// A `three-color-policer <name>`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ThreeColorPolicer {
    pub name: String,
    /// The statements of `action`, e.g. `"loss-priority high then discard"`.
    pub actions: Vec<String>,
    pub filter_specific: bool,
    pub logical_interface_policer: bool,
    pub physical_interface_policer: bool,
    pub shared_bandwidth_policer: bool,
    pub single_rate: Option<ThreeColorRate>,
    pub two_rate: Option<ThreeColorRate>,
}

pub(super) fn parse_three_color_policer(name: &str, line: &ConfigLine) -> ThreeColorPolicer {
    ThreeColorPolicer {
        name: name.to_string(),
        actions: child_block(line, "action")
            .map(|action| active_children(action).map(|c| c.text.clone()).collect())
            .unwrap_or_default(),
        filter_specific: has_leaf(line, "filter-specific"),
        logical_interface_policer: has_leaf(line, "logical-interface-policer"),
        physical_interface_policer: has_leaf(line, "physical-interface-policer"),
        shared_bandwidth_policer: has_leaf(line, "shared-bandwidth-policer"),
        single_rate: child_block(line, "single-rate").map(parse_rate),
        two_rate: child_block(line, "two-rate").map(parse_rate),
    }
}

fn parse_rate(rate: &ConfigLine) -> ThreeColorRate {
    let field = |keyword: &str| arg_of(rate, keyword).map(str::to_string);
    ThreeColorRate {
        color_aware: if has_leaf(rate, "color-aware") {
            Some(true)
        } else if has_leaf(rate, "color-blind") {
            Some(false)
        } else {
            None
        },
        committed_information_rate: field("committed-information-rate"),
        committed_burst_size: field("committed-burst-size"),
        excess_burst_size: field("excess-burst-size"),
        peak_information_rate: field("peak-information-rate"),
        peak_burst_size: field("peak-burst-size"),
    }
}