asterust-junos 0.5.10

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

use crate::{arg_of, args_of, list_values};
use asterust::config::ConfigLine;

/// The options of `system services telnet`; every field is empty when only `telnet;` is written.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TelnetService {
    /// Methods tried in order: `password`, `radius`, `tacplus`.
    pub authentication_order: Vec<String>,
    pub connection_limit: Option<String>,
    pub rate_limit: Option<String>,
}

pub(super) fn parse_telnet(telnet: &ConfigLine) -> TelnetService {
    TelnetService {
        authentication_order: args_of(telnet, "authentication-order")
            .into_iter()
            .flat_map(list_values)
            .collect(),
        connection_limit: arg_of(telnet, "connection-limit").map(str::to_string),
        rate_limit: arg_of(telnet, "rate-limit").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_telnet_options() {
        let telnet = system(&juniper::parse_braces(
            "system {\n    services {\n        telnet {\n            authentication-order [ tacplus password ];\n            connection-limit 10;\n            rate-limit 5;\n        }\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap()
        .telnet_service
        .unwrap();
        assert_eq!(telnet.authentication_order, ["tacplus", "password"]);
        assert_eq!(telnet.connection_limit.as_deref(), Some("10"));
        assert_eq!(telnet.rate_limit.as_deref(), Some("5"));
    }

    #[test]
    fn bare_telnet_has_no_options() {
        let services = system(&juniper::parse_braces(
            "system {\n    services {\n        telnet;\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap();
        assert!(services.telnet);
        assert_eq!(services.telnet_service, Some(Default::default()));
    }
}