asterust-junos 0.5.13

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
//! `configuration`
//! `system { configuration { input { format { json { ... } } } } }`.
//!
//! # See also
//!
//! - [json (Configuration Input Format)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/json-edit-system-configuration-input-format.html)

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

/// The `system configuration` settings for loading configuration data.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ConfigurationInput {
    /// `input format json reorder-list-keys`: reorder list entries so that keys come first.
    pub json_reorder_list_keys: bool,
}

pub(super) fn parse_configuration(configuration: &ConfigLine) -> ConfigurationInput {
    let json = child_block(configuration, "input")
        .and_then(|input| child_block(input, "format"))
        .and_then(|format| child_block(format, "json"));
    ConfigurationInput {
        json_reorder_list_keys: json.is_some_and(|json| has_leaf(json, "reorder-list-keys")),
    }
}

#[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_json_input_format() {
        let configuration = system(&juniper::parse_braces(
            "system {\n    configuration {\n        input {\n            format {\n                json {\n                    reorder-list-keys;\n                }\n            }\n        }\n    }\n}\n",
        ))
        .unwrap()
        .configuration
        .unwrap();
        assert!(configuration.json_reorder_list_keys);
    }

    #[test]
    fn no_configuration_is_none() {
        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
        assert!(system.configuration.is_none());
    }
}