Skip to main content

asterust_junos/system/services/
limits.rs

1//! `finger` and `tftp-server`
2//! `system { services { finger { ... } } }` and `system { services { tftp-server { ... } } }`.
3//!
4//! # See also
5//!
6//! - [finger](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/finger-edit-system.html)
7//! - [tftp-server](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-services-tftp-server.html)
8
9use crate::arg_of;
10use asterust::config::ConfigLine;
11
12/// The `connection-limit` and `rate-limit` of a service such as `finger` or `tftp-server`.
13#[derive(Clone, Debug, Default, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct ConnectionLimits {
16    pub connection_limit: Option<String>,
17    /// Connection attempts per minute.
18    pub rate_limit: Option<String>,
19}
20
21pub(super) fn parse_limits(service: &ConfigLine) -> ConnectionLimits {
22    ConnectionLimits {
23        connection_limit: arg_of(service, "connection-limit").map(str::to_string),
24        rate_limit: arg_of(service, "rate-limit").map(str::to_string),
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use crate::system::system;
31    use asterust::config::juniper;
32
33    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
34    #[test]
35    fn extracts_finger_and_tftp_server_limits() {
36        let services = system(&juniper::parse_braces(
37            "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",
38        ))
39        .unwrap()
40        .services
41        .unwrap();
42        let finger = services.finger.unwrap();
43        assert_eq!(finger.connection_limit.as_deref(), Some("50"));
44        assert_eq!(finger.rate_limit.as_deref(), Some("10"));
45        let tftp = services.tftp_server.unwrap();
46        assert_eq!(tftp.connection_limit.as_deref(), Some("20"));
47        assert_eq!(tftp.rate_limit, None);
48    }
49
50    #[test]
51    fn no_finger_or_tftp_server_is_none() {
52        let services = system(&juniper::parse_braces(
53            "system {\n    services {\n        telnet;\n    }\n}\n",
54        ))
55        .unwrap()
56        .services
57        .unwrap();
58        assert!(services.finger.is_none());
59        assert!(services.tftp_server.is_none());
60    }
61}