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

use crate::arg_of;
use asterust::config::ConfigLine;

/// The `connection-limit` and `rate-limit` of a service such as `finger` or `tftp-server`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ConnectionLimits {
    pub connection_limit: Option<String>,
    /// Connection attempts per minute.
    pub rate_limit: Option<String>,
}

pub(super) fn parse_limits(service: &ConfigLine) -> ConnectionLimits {
    ConnectionLimits {
        connection_limit: arg_of(service, "connection-limit").map(str::to_string),
        rate_limit: arg_of(service, "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 pages. See the README "Sources".
    #[test]
    fn extracts_finger_and_tftp_server_limits() {
        let services = system(&juniper::parse_braces(
            "system {\n    services {\n        finger {\n            connection-limit 50;\n            rate-limit 10;\n        }\n        tftp-server {\n            connection-limit 20;\n        }\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap();
        let finger = services.finger.unwrap();
        assert_eq!(finger.connection_limit.as_deref(), Some("50"));
        assert_eq!(finger.rate_limit.as_deref(), Some("10"));
        let tftp = services.tftp_server.unwrap();
        assert_eq!(tftp.connection_limit.as_deref(), Some("20"));
        assert_eq!(tftp.rate_limit, None);
    }

    #[test]
    fn no_finger_or_tftp_server_is_none() {
        let services = system(&juniper::parse_braces(
            "system {\n    services {\n        telnet;\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap();
        assert!(services.finger.is_none());
        assert!(services.tftp_server.is_none());
    }
}