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
//! `pppoe-underlying-options` and `family pppoe`
//! `interfaces { <name> { unit <n> { pppoe-underlying-options { ... } } } }`.
//!
//! # See also
//!
//! - [pppoe-underlying-options (Static and Dynamic Subscribers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pppoe-underlying-options-edit-interfaces-pppoe.html)
//! - [max-sessions (Dynamic PPPoE)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/max-sessions-edit-interfaces-pppoe-underlying-options.html)
//! - [max-sessions-vsa-ignore](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/max-sessions-vsa-ignore-edit-pppoe-underlying-options.html)
//! - [dynamic-profile (Dynamic PPPoE)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dynamic-profile-edit-interfaces-pppoe-underlying-options.html)
//! - [service-name-table](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/service-name-table-edit-interfaces.html)
//! - [duplicate-protection (Dynamic PPPoE)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/duplicate-protection-edit-interfaces-pppoe-underlying.html)
//! - [direct-connect](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/direct-connect-edit-interfaces-pppoe.html)
//! - [short-cycle-protection](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/short-cycle-protection-edit-pppoe-underlying-options.html)

use crate::{arg_of, child_block, has_leaf, list_values, statement_words, value_after};
use asterust::config::ConfigLine;

/// The `duplicate-protection` of the PPPoE underlying options.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DuplicateProtection {
    /// `include { relay-session-id; }`.
    pub include_relay_session_id: bool,
}

/// The `short-cycle-protection` of the PPPoE underlying options.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ShortCycleProtection {
    /// Seconds.
    pub lockout_time_min: Option<String>,
    /// Seconds.
    pub lockout_time_max: Option<String>,
    /// The `filter [ ... ]` values, e.g. `aci`.
    pub filter: Vec<String>,
}

/// The `pppoe-underlying-options` of a unit, which `family pppoe` shares.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PppoeUnderlyingOptions {
    pub access_concentrator: Option<String>,
    pub dynamic_profile: Option<String>,
    pub direct_connect: bool,
    pub duplicate_protection: Option<DuplicateProtection>,
    pub max_sessions: Option<String>,
    pub max_sessions_vsa_ignore: bool,
    pub service_name_table: Option<String>,
    pub short_cycle_protection: Option<ShortCycleProtection>,
}

pub(super) fn parse_pppoe_underlying_options(options: &ConfigLine) -> PppoeUnderlyingOptions {
    let value = |keyword| arg_of(options, keyword).map(str::to_string);
    PppoeUnderlyingOptions {
        access_concentrator: value("access-concentrator"),
        dynamic_profile: value("dynamic-profile"),
        direct_connect: has_leaf(options, "direct-connect"),
        duplicate_protection: crate::active_children(options)
            .find(|c| c.text == "duplicate-protection" || c.text.starts_with("duplicate-protection "))
            .map(|protection| DuplicateProtection {
                include_relay_session_id: child_block(protection, "include")
                    .is_some_and(|include| has_leaf(include, "relay-session-id")),
            }),
        max_sessions: value("max-sessions"),
        max_sessions_vsa_ignore: has_leaf(options, "max-sessions-vsa-ignore"),
        service_name_table: value("service-name-table"),
        short_cycle_protection: statement_words(options, "short-cycle-protection").map(|words| ShortCycleProtection {
            lockout_time_min: value_after(&words, "lockout-time-min"),
            lockout_time_max: value_after(&words, "lockout-time-max"),
            filter: words
                .iter()
                .position(|w| *w == "filter")
                .map(|i| list_values(&words[i + 1..].join(" ")))
                .unwrap_or_default(),
        }),
    }
}