asterust-junos 0.5.10

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `export-format`
//! `system { export-format { ... } }`.
//!
//! # See also
//!
//! - [export-format (JSON)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/export-format-edit-system.html)

use crate::{child_block, has_leaf};
use asterust::config::ConfigLine;

/// The `system export-format` serialization of exported data.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ExportFormat {
    /// `json ietf`: emit JSON according to the IETF YANG JSON encoding rules.
    pub json_ietf: bool,
    /// `json verbose`.
    pub json_verbose: bool,
    /// `state-data json compact`.
    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;

    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
    #[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());
    }
}