use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{active_children, arg_of, child_block, has_leaf, named_children, toggle};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ResiliencyFile {
pub name: String,
pub files: Option<String>,
pub size: Option<String>,
pub world_readable: Option<bool>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ResiliencyStore {
pub database: bool,
pub files: Vec<ResiliencyFile>,
pub forwarding_files: Vec<ResiliencyFile>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Resiliency {
pub exceptions: Vec<String>,
pub store: Option<ResiliencyStore>,
pub traceoptions: Option<Traceoptions>,
}
fn files(store: &ConfigLine, keyword: &str) -> Vec<ResiliencyFile> {
named_children(store, keyword)
.map(|(name, file)| ResiliencyFile {
name: name.to_string(),
files: arg_of(file, "files").map(str::to_string),
size: arg_of(file, "size").map(str::to_string),
world_readable: toggle(file, "world-readable"),
})
.collect()
}
pub(super) fn parse_resiliency(resiliency: &ConfigLine) -> Resiliency {
Resiliency {
exceptions: child_block(resiliency, "exceptions")
.map(|exceptions| {
active_children(exceptions)
.map(|exception| exception.text.clone())
.collect()
})
.unwrap_or_default(),
store: child_block(resiliency, "store").map(|store| ResiliencyStore {
database: has_leaf(store, "database"),
files: files(store, "file"),
forwarding_files: files(store, "fwding-file"),
}),
traceoptions: parse_traceoptions(resiliency),
}
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_the_resiliency_settings() {
let resiliency = system(&juniper::parse_braces(
r#"
system {
resiliency {
exceptions {
forwarding;
routing;
}
store {
database;
file exceptions.log {
files 5;
size 10m;
world-readable;
}
fwding-file fwd.log {
files 3;
no-world-readable;
}
}
traceoptions {
file resiliency.log;
flag all;
}
}
}
"#,
))
.unwrap()
.resiliency
.unwrap();
assert_eq!(resiliency.exceptions, ["forwarding", "routing"]);
let store = resiliency.store.unwrap();
assert!(store.database);
assert_eq!(store.files[0].name, "exceptions.log");
assert_eq!(store.files[0].files.as_deref(), Some("5"));
assert_eq!(store.files[0].size.as_deref(), Some("10m"));
assert_eq!(store.files[0].world_readable, Some(true));
assert_eq!(store.forwarding_files[0].name, "fwd.log");
assert_eq!(store.forwarding_files[0].world_readable, Some(false));
assert_eq!(store.forwarding_files[0].size, None);
assert_eq!(resiliency.traceoptions.unwrap().file.as_deref(), Some("resiliency.log"));
}
#[test]
fn no_resiliency_is_none() {
let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
assert!(system.resiliency.is_none());
}
}