asterust_junos/system/
export_format.rs1use crate::{child_block, has_leaf};
9use asterust::config::ConfigLine;
10
11#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct ExportFormat {
15 pub json_ietf: bool,
17 pub json_verbose: bool,
19 pub state_data_json_compact: bool,
21}
22
23pub(super) fn parse_export_format(format: &ConfigLine) -> ExportFormat {
24 let json = child_block(format, "json");
25 let state_json = child_block(format, "state-data").and_then(|state| child_block(state, "json"));
26 ExportFormat {
27 json_ietf: json.is_some_and(|json| has_leaf(json, "ietf")),
28 json_verbose: json.is_some_and(|json| has_leaf(json, "verbose")),
29 state_data_json_compact: state_json.is_some_and(|json| has_leaf(json, "compact")),
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use crate::system::system;
36 use asterust::config::juniper;
37
38 #[test]
40 fn extracts_the_export_format() {
41 let format = system(&juniper::parse_braces(
42 "system {\n export-format {\n json {\n ietf;\n }\n state-data {\n json {\n compact;\n }\n }\n }\n}\n",
43 ))
44 .unwrap()
45 .export_format
46 .unwrap();
47 assert!(format.json_ietf);
48 assert!(!format.json_verbose);
49 assert!(format.state_data_json_compact);
50 }
51
52 #[test]
53 fn no_export_format_is_none() {
54 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
55 assert!(system.export_format.is_none());
56 }
57}