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
//! `overrides`
//! `system { services { dhcp-local-server { [dhcpv6] [group <name> [interface <name>]] overrides { ... } } } }`.
//!
//! # See also
//!
//! - [overrides (DHCP Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/overrides-edit-system-services.html)
//! - [overrides (System Services DHCP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-edit-overrides-dhcp-local-server.html)
//! - [delay-offer (DHCPv4)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/delay-offer.html)
//! - [delay-advertise (DHCPv6)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/delay-advertise.html)
//! - [client-discover-match (DHCP Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/client-discover-match-edit-system-services.html)
//! - [client-negotiation-match (DHCPv6 Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/client-negotiation-match-edit-system-services.html)
//! - [delegated-pool (DHCP Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/delegated-pool-edit-system-services.html)
//! - [delete-binding-on-renegotiation](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/delete-binding-on-renegotiation-edit-dhcp.html)
//! - [dual-stack (DHCP Local Server Overrides)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dual-stack-edit-system-services-dhcp-local-server-overrides.html)
//! - [include-option-82 (DHCP Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/include-option-82-edit-system-services.html)
//! - [interface-client-limit (DHCP Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-client-limit-edit-system-services.html)
//! - [multi-address-embedded-option-response (DHCP Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/multi-address-embedded-option-response-edit-system-services.html)
//! - [process-inform](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/process-inform-edit-system-services-overrides.html)
//! - [rapid-commit (DHCPv6 Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rapid-commit-edit-system-services.html)
//! - [always-process-option-request-option](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/always-process-option-request-option-edit-system-services-dhcp-local-server-dhcpv6-group-overrides.html)

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

/// An `ascii` or `hexadecimal` match of a `delay-offer` or `delay-advertise` rule.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DhcpMatch {
    /// `equals`, `not-equals` or `starts-with`.
    pub condition: String,
    /// `ascii` or `hexadecimal`.
    pub encoding: String,
    pub value: String,
}

/// A `delay-offer` or `delay-advertise` block.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DhcpDelay {
    /// The option the delay is based on, e.g. `option-82` or `option-37`.
    pub based_on: Option<String>,
    pub matches: Vec<DhcpMatch>,
    /// Seconds.
    pub delay_time: Option<String>,
}

/// The `overrides` block of a DHCP local server, group or interface.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct DhcpOverrides {
    pub always_add_option_dns_server: bool,
    pub always_process_option_request_option: bool,
    pub asymmetric_lease_time: Option<String>,
    pub asymmetric_prefix_lease_time: Option<String>,
    /// `option60-and-option82` or `incoming-interface`.
    pub client_discover_match: Option<String>,
    pub client_negotiation_match: Option<String>,
    pub delay_advertise: Option<DhcpDelay>,
    pub delay_offer: Option<DhcpDelay>,
    pub delegated_pool: Option<String>,
    pub delete_binding_on_renegotiation: bool,
    pub dual_stack: Option<String>,
    pub ia_na_pool: Option<String>,
    /// `include-option-82` is configured.
    pub include_option_82: bool,
    pub include_option_82_forcerenew: bool,
    pub include_option_82_nak: bool,
    pub interface_client_limit: Option<String>,
    pub multi_address_embedded_option_response: bool,
    /// From `process-inform { pool }`.
    pub process_inform_pool: Option<String>,
    pub protocol_attributes: Option<String>,
    pub rapid_commit: bool,
}

fn parse_delay(delay: &ConfigLine) -> DhcpDelay {
    let based_on = named_children(delay, "based-on").next();
    let mut matches = Vec::new();
    if let Some((_, block)) = based_on {
        for condition in ["equals", "not-equals", "starts-with"] {
            let Some(rules) = child_block(block, condition) else {
                continue;
            };
            for rule in active_children(rules) {
                if let Some((encoding, value)) = rule.text.split_once(char::is_whitespace) {
                    matches.push(DhcpMatch {
                        condition: condition.to_string(),
                        encoding: encoding.to_string(),
                        value: value.trim().trim_matches('"').to_string(),
                    });
                }
            }
        }
    }
    DhcpDelay {
        based_on: based_on.map(|(option, _)| option.to_string()),
        matches,
        delay_time: arg_of(delay, "delay-time").map(str::to_string),
    }
}

pub(super) fn parse_overrides(overrides: &ConfigLine) -> DhcpOverrides {
    let field = |keyword: &str| arg_of(overrides, keyword).map(str::to_string);
    let option_82 = child_block(overrides, "include-option-82");
    DhcpOverrides {
        always_add_option_dns_server: has_leaf(overrides, "always-add-option-dns-server"),
        always_process_option_request_option: has_leaf(overrides, "always-process-option-request-option"),
        asymmetric_lease_time: field("asymmetric-lease-time"),
        asymmetric_prefix_lease_time: field("asymmetric-prefix-lease-time"),
        client_discover_match: field("client-discover-match"),
        client_negotiation_match: field("client-negotiation-match"),
        delay_advertise: child_block(overrides, "delay-advertise").map(parse_delay),
        delay_offer: child_block(overrides, "delay-offer").map(parse_delay),
        delegated_pool: field("delegated-pool"),
        delete_binding_on_renegotiation: has_leaf(overrides, "delete-binding-on-renegotiation"),
        dual_stack: field("dual-stack"),
        ia_na_pool: field("ia-na-pool"),
        include_option_82: option_82.is_some(),
        include_option_82_forcerenew: option_82.is_some_and(|o| has_leaf(o, "forcerenew")),
        include_option_82_nak: option_82.is_some_and(|o| has_leaf(o, "nak")),
        interface_client_limit: field("interface-client-limit"),
        multi_address_embedded_option_response: has_leaf(overrides, "multi-address-embedded-option-response"),
        process_inform_pool: child_block(overrides, "process-inform")
            .and_then(|inform| arg_of(inform, "pool"))
            .map(str::to_string),
        protocol_attributes: field("protocol-attributes"),
        rapid_commit: has_leaf(overrides, "rapid-commit"),
    }
}