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