use crate::{child_block, has_leaf};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ExportFormat {
pub json_ietf: bool,
pub json_verbose: bool,
pub state_data_json_compact: bool,
}
pub(super) fn parse_export_format(format: &ConfigLine) -> ExportFormat {
let json = child_block(format, "json");
let state_json = child_block(format, "state-data").and_then(|state| child_block(state, "json"));
ExportFormat {
json_ietf: json.is_some_and(|json| has_leaf(json, "ietf")),
json_verbose: json.is_some_and(|json| has_leaf(json, "verbose")),
state_data_json_compact: state_json.is_some_and(|json| has_leaf(json, "compact")),
}
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_the_export_format() {
let format = system(&juniper::parse_braces(
"system {\n export-format {\n json {\n ietf;\n }\n state-data {\n json {\n compact;\n }\n }\n }\n}\n",
))
.unwrap()
.export_format
.unwrap();
assert!(format.json_ietf);
assert!(!format.json_verbose);
assert!(format.state_data_json_compact);
}
#[test]
fn no_export_format_is_none() {
let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
assert!(system.export_format.is_none());
}
}