asterust-junos 0.5.14

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
//! `accounting`, `targeted-broadcast`, `unnumbered-address` and `vlan-rewrite`
//! `interfaces { <name> { unit <n> { family <name> { ... } } } }`.
//!
//! # See also
//!
//! - [accounting (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accounting-edit-interfaces.html)
//! - [destination-class-usage](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/destination-class-usage-edit-interfaces.html)
//! - [source-class-usage](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/source-class-usage-edit-interfaces.html)
//! - [forward-only](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/forward-only-edit-interfaces.html)
//! - [forward-and-send-to-re](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/forward-and-send-to-re-edit-interfaces.html)
//! - [unnumbered-address (Ethernet)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unnumbered-address-edit-interfaces-ethernet.html)
//! - [unnumbered-address (PPP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unnumbered-address-edit-interfaces-ppp.html)
//! - [unnumbered-address (Demux)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unnumbered-address-edit-interfaces-ip-demux.html)
//! - [vlan-rewrite](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vlan-rewrite-edit-interfaces.html)

use crate::{active_children, args_of, child_block, has_leaf, statement_words, value_after};
use asterust::config::ConfigLine;

/// The `accounting` block of `family inet`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FamilyAccounting {
    pub destination_class_usage: bool,
    pub source_class_usage: bool,
    /// The directions of `source-class-usage`, e.g. `input` and `output`.
    pub source_class_usage_directions: Vec<String>,
}

/// The `targeted-broadcast` block of `family inet`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TargetedBroadcast {
    pub forward_only: bool,
    pub forward_and_send_to_re: bool,
}

/// The `unnumbered-address` of a family.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct UnnumberedAddress {
    /// The interface whose address is borrowed.
    pub interface: String,
    pub preferred_source_address: Option<String>,
    pub destination: Option<String>,
    pub destination_profile: Option<String>,
}

/// A `translate <from> <to>` of `vlan-rewrite`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct VlanTranslation {
    pub from: String,
    pub to: String,
}

pub(super) fn parse_accounting(accounting: &ConfigLine) -> FamilyAccounting {
    let source =
        active_children(accounting).find(|c| c.text == "source-class-usage" || c.arg("source-class-usage").is_some());
    FamilyAccounting {
        destination_class_usage: has_leaf(accounting, "destination-class-usage"),
        source_class_usage: source.is_some(),
        source_class_usage_directions: source
            .map(|line| {
                line.text
                    .split_whitespace()
                    .skip(1)
                    .map(str::to_string)
                    .chain(active_children(line).map(|direction| direction.text.clone()))
                    .collect()
            })
            .unwrap_or_default(),
    }
}

pub(super) fn parse_targeted_broadcast(broadcast: &ConfigLine) -> TargetedBroadcast {
    TargetedBroadcast {
        forward_only: has_leaf(broadcast, "forward-only"),
        forward_and_send_to_re: has_leaf(broadcast, "forward-and-send-to-re"),
    }
}

pub(super) fn parse_unnumbered_address(family: &ConfigLine) -> Option<UnnumberedAddress> {
    let words = statement_words(family, "unnumbered-address")?;
    Some(UnnumberedAddress {
        interface: words.first()?.to_string(),
        preferred_source_address: value_after(&words, "preferred-source-address"),
        destination: value_after(&words, "destination"),
        destination_profile: value_after(&words, "destination-profile"),
    })
}

pub(super) fn parse_vlan_rewrite(family: &ConfigLine) -> Vec<VlanTranslation> {
    child_block(family, "vlan-rewrite").map_or_else(Vec::new, |rewrite| {
        args_of(rewrite, "translate")
            .into_iter()
            .filter_map(|arg| {
                let mut ids = arg.split_whitespace();
                Some(VlanTranslation {
                    from: ids.next()?.to_string(),
                    to: ids.next()?.to_string(),
                })
            })
            .collect()
    })
}