asterust-junos 0.5.0

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! `server`
//! `services { monitoring { twamp { server { ... } } } }`.

use super::text;
use crate::services::rpm::twamp::{parse_client_lists, TwampClientList};
use crate::{child_block, has_leaf};
use asterust::config::ConfigLine;

/// The `twamp server` of Junos OS Evolved: either a `managed` or a `light` server.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampServer {
    /// `managed;` on its own is a server with no options.
    pub managed: Option<TwampManagedServer>,
    /// `light;` on its own is a server with no options.
    pub light: Option<TwampLightServer>,
}

/// The `managed` server.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampManagedServer {
    pub client_limit: Option<String>,
    pub client_lists: Vec<TwampClientList>,
    pub control_inactivity_timeout: Option<String>,
    pub control_maximum_duration: Option<String>,
    pub control_per_client_limit: Option<String>,
    /// `"inline-timestamp"`, `"none"` or `"pfe-timestamp"`; the default is `"pfe-timestamp"`.
    pub offload_type: Option<String>,
    pub port: Option<String>,
    pub stateful_sequence: bool,
    pub test_per_client_limit: Option<String>,
}

/// The `light` server.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampLightServer {
    /// `"inline-timestamp"`, `"none"` or `"pfe-timestamp"`; the default is `"pfe-timestamp"`.
    pub offload_type: Option<String>,
    pub port: Option<String>,
    /// The size after `stateful-sequence cache-size`.
    pub stateful_sequence_cache_size: Option<String>,
}

pub(super) fn parse_server(line: &ConfigLine) -> TwampServer {
    TwampServer {
        managed: child_block(line, "managed").map(parse_managed),
        light: child_block(line, "light").map(parse_light),
    }
}

fn parse_managed(line: &ConfigLine) -> TwampManagedServer {
    TwampManagedServer {
        client_limit: text(line, "client-limit"),
        client_lists: parse_client_lists(line),
        control_inactivity_timeout: text(line, "control-inactivity-timeout"),
        control_maximum_duration: text(line, "control-maximum-duration"),
        control_per_client_limit: text(line, "control-per-client-limit"),
        offload_type: text(line, "offload-type"),
        port: text(line, "port"),
        stateful_sequence: has_leaf(line, "stateful-sequence"),
        test_per_client_limit: text(line, "test-per-client-limit"),
    }
}

fn parse_light(line: &ConfigLine) -> TwampLightServer {
    TwampLightServer {
        offload_type: text(line, "offload-type"),
        port: text(line, "port"),
        stateful_sequence_cache_size: text(line, "stateful-sequence")
            .and_then(|s| s.strip_prefix("cache-size").map(|size| size.trim().to_string())),
    }
}