asterust-junos 0.5.9

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
//! `radius-options`
//! `system { radius-options { ... } }`.
//!
//! # See also
//!
//! - [radius-options (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/radius-options-edit-system.html)

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

/// The `system radius-options` settings.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RadiusOptions {
    /// From `attributes nas-id`.
    pub nas_id: Option<String>,
    /// From `attributes nas-ip-address`.
    pub nas_ip_address: Option<String>,
    pub enhanced_accounting: bool,
    /// e.g. `mschap-v2`.
    pub password_protocol: Option<String>,
}

pub(super) fn parse_radius_options(options: &ConfigLine) -> RadiusOptions {
    let attributes = child_block(options, "attributes");
    RadiusOptions {
        nas_id: attributes.and_then(|a| arg_of(a, "nas-id")).map(str::to_string),
        nas_ip_address: attributes.and_then(|a| arg_of(a, "nas-ip-address")).map(str::to_string),
        enhanced_accounting: has_leaf(options, "enhanced-accounting"),
        password_protocol: arg_of(options, "password-protocol").map(str::to_string),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
    #[test]
    fn extracts_radius_options() {
        let options = system(&juniper::parse_braces(
            r#"
system {
    radius-options {
        attributes {
            nas-id pe1;
            nas-ip-address 192.0.2.1;
        }
        enhanced-accounting;
        password-protocol mschap-v2;
    }
}
"#,
        ))
        .unwrap()
        .radius_options
        .unwrap();
        assert_eq!(options.nas_id.as_deref(), Some("pe1"));
        assert_eq!(options.nas_ip_address.as_deref(), Some("192.0.2.1"));
        assert!(options.enhanced_accounting);
        assert_eq!(options.password_protocol.as_deref(), Some("mschap-v2"));
    }

    #[test]
    fn radius_options_default_to_unset() {
        let options = system(&juniper::parse_braces(
            "system {\n    radius-options {\n        enhanced-accounting;\n    }\n}\n",
        ))
        .unwrap()
        .radius_options
        .unwrap();
        assert_eq!(options.nas_id, None);
        assert_eq!(options.nas_ip_address, None);
        assert_eq!(options.password_protocol, None);
    }

    #[test]
    fn no_radius_options_is_none() {
        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
        assert!(system.radius_options.is_none());
    }
}