Skip to main content

asterust_junos/system/
export_format.rs

1//! `export-format`
2//! `system { export-format { ... } }`.
3//!
4//! # See also
5//!
6//! - [export-format (JSON)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/export-format-edit-system.html)
7
8use crate::{child_block, has_leaf};
9use asterust::config::ConfigLine;
10
11/// The `system export-format` serialization of exported data.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct ExportFormat {
15    /// `json ietf`: emit JSON according to the IETF YANG JSON encoding rules.
16    pub json_ietf: bool,
17    /// `json verbose`.
18    pub json_verbose: bool,
19    /// `state-data json compact`.
20    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    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
39    #[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}