Skip to main content

asterust_junos/system/services/
ftp.rs

1//! `ftp`
2//! `system { services { ftp { ... } } }`.
3//!
4//! # See also
5//!
6//! - [ftp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ftp-edit-system.html)
7
8use crate::{arg_of, args_of, has_leaf, list_values};
9use asterust::config::ConfigLine;
10
11/// The options of `system services ftp`; every field is empty when only `ftp;` is written.
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct FtpService {
15    /// Methods tried in order: `password`, `radius`, `tacplus`.
16    pub authentication_order: Vec<String>,
17    pub connection_limit: Option<String>,
18    pub rate_limit: Option<String>,
19    pub enable_anonymous_login: bool,
20}
21
22pub(super) fn parse_ftp(ftp: &ConfigLine) -> FtpService {
23    FtpService {
24        authentication_order: args_of(ftp, "authentication-order")
25            .into_iter()
26            .flat_map(list_values)
27            .collect(),
28        connection_limit: arg_of(ftp, "connection-limit").map(str::to_string),
29        rate_limit: arg_of(ftp, "rate-limit").map(str::to_string),
30        enable_anonymous_login: has_leaf(ftp, "enable-anonymous-login"),
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use crate::system::system;
37    use asterust::config::juniper;
38
39    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
40    #[test]
41    fn extracts_ftp_options() {
42        let ftp = system(&juniper::parse_braces(
43            "system {\n    services {\n        ftp {\n            authentication-order password;\n            connection-limit 20;\n            rate-limit 8;\n            enable-anonymous-login;\n        }\n    }\n}\n",
44        ))
45        .unwrap()
46        .services
47        .unwrap()
48        .ftp_service
49        .unwrap();
50        assert_eq!(ftp.authentication_order, ["password"]);
51        assert_eq!(ftp.connection_limit.as_deref(), Some("20"));
52        assert_eq!(ftp.rate_limit.as_deref(), Some("8"));
53        assert!(ftp.enable_anonymous_login);
54    }
55
56    #[test]
57    fn no_ftp_has_no_options() {
58        let services = system(&juniper::parse_braces(
59            "system {\n    services {\n        telnet;\n    }\n}\n",
60        ))
61        .unwrap()
62        .services
63        .unwrap();
64        assert!(services.ftp_service.is_none());
65    }
66}