asterust-junos 0.5.12

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
//! `demux-options`, `demux-source`, `demux-destination`, `advisory-options` and `targeted-distribution`
//! `interfaces { <name> { unit <n> { ... } } }`.
//!
//! # See also
//!
//! - [demux-options (Static Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-options-edit-interfaces.html)
//! - [demux-source (Underlying Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-source-edit-interfaces-unit.html)
//! - [demux-destination (Underlying Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-destination-edit-interfaces-unit.html)
//! - [demux-source (Demux Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-source-edit-interfaces-family.html)
//! - [demux-destination (Demux Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-destination-edit-interfaces-family.html)
//! - [advisory-options (Traffic Shaping)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/advisory-options-edit-interfaces-unit.html)
//! - [downstream-rate (Traffic Shaping)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/downstream-rate-edit-interfaces-unit.html)
//! - [upstream-rate (Traffic Shaping)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/upstream-rate-edit-interfaces-unit.html)
//! - [targeted-distribution](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/targeted-distribution-edit-interfaces.html)

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

/// The `demux-options` of a unit.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DemuxOptions {
    pub underlying_interface: Option<String>,
}

/// The `advisory-options` of a unit: the rates used for traffic shaping.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AdvisoryOptions {
    pub downstream_rate: Option<String>,
    pub upstream_rate: Option<String>,
}

pub(super) fn parse_demux_options(options: &ConfigLine) -> DemuxOptions {
    DemuxOptions {
        underlying_interface: arg_of(options, "underlying-interface").map(str::to_string),
    }
}

pub(super) fn parse_advisory_options(options: &ConfigLine) -> AdvisoryOptions {
    AdvisoryOptions {
        downstream_rate: arg_of(options, "downstream-rate").map(str::to_string),
        upstream_rate: arg_of(options, "upstream-rate").map(str::to_string),
    }
}

/// The prefixes of a family's `demux-source { ... }` or `demux-destination { ... }`; also accepts `<keyword> <prefix>;`.
pub(super) fn demux_prefixes(family: &ConfigLine, keyword: &str) -> Vec<String> {
    active_children(family)
        .flat_map(|child| {
            if child.text == keyword {
                active_children(child).map(|prefix| prefix.text.clone()).collect()
            } else {
                child.arg(keyword).map(str::to_string).into_iter().collect::<Vec<_>>()
            }
        })
        .collect()
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_demux_options_and_advisory_options() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    ge-0/0/0 {
        unit 0 {
            demux-options {
                underlying-interface ge-0/0/1;
            }
            demux-source inet;
            demux-source inet6;
            demux-destination inet;
            advisory-options {
                downstream-rate 10m;
                upstream-rate 1m;
            }
            targeted-distribution scale;
            family inet {
                demux-source {
                    192.0.2.0/24;
                    198.51.100.0/24;
                }
                demux-destination {
                    203.0.113.0/24;
                }
            }
            family inet6 {
                demux-source 2001:db8::/32;
            }
        }
        unit 1 {
            description plain;
        }
    }
}
"#,
        ));
        let units = &ifaces[0].units;
        assert_eq!(
            units[0].demux_options.as_ref().unwrap().underlying_interface.as_deref(),
            Some("ge-0/0/1")
        );
        assert_eq!(units[0].demux_source, ["inet", "inet6"]);
        assert_eq!(units[0].demux_destination, ["inet"]);
        let advisory = units[0].advisory_options.as_ref().unwrap();
        assert_eq!(advisory.downstream_rate.as_deref(), Some("10m"));
        assert_eq!(advisory.upstream_rate.as_deref(), Some("1m"));
        assert_eq!(units[0].targeted_distribution.as_deref(), Some("scale"));
        let inet = &units[0].family_settings[0];
        assert_eq!(inet.demux_source, ["192.0.2.0/24", "198.51.100.0/24"]);
        assert_eq!(inet.demux_destination, ["203.0.113.0/24"]);
        assert_eq!(units[0].family_settings[1].demux_source, ["2001:db8::/32"]);
        assert!(units[0].family_settings[1].demux_destination.is_empty());

        assert!(units[1].demux_options.is_none() && units[1].advisory_options.is_none());
        assert!(units[1].demux_source.is_empty() && units[1].targeted_distribution.is_none());
    }
}