use crate::{arg_of, args_of, has_leaf, list_values};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct FtpService {
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;
#[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());
}
}