asterust-junos 0.5.12

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

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

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

pub(super) fn parse_ftp(ftp: &ConfigLine) -> FtpService {
    FtpService {
        authentication_order: args_of(ftp, "authentication-order")
            .into_iter()
            .flat_map(list_values)
            .collect(),
        connection_limit: arg_of(ftp, "connection-limit").map(str::to_string),
        rate_limit: arg_of(ftp, "rate-limit").map(str::to_string),
        enable_anonymous_login: has_leaf(ftp, "enable-anonymous-login"),
    }
}

#[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_ftp_options() {
        let ftp = system(&juniper::parse_braces(
            "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",
        ))
        .unwrap()
        .services
        .unwrap()
        .ftp_service
        .unwrap();
        assert_eq!(ftp.authentication_order, ["password"]);
        assert_eq!(ftp.connection_limit.as_deref(), Some("20"));
        assert_eq!(ftp.rate_limit.as_deref(), Some("8"));
        assert!(ftp.enable_anonymous_login);
    }

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