Skip to main content

asterust_junos/system/
configuration.rs

1//! `configuration`
2//! `system { configuration { input { format { json { ... } } } } }`.
3//!
4//! # See also
5//!
6//! - [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)
7
8use crate::{child_block, has_leaf};
9use asterust::config::ConfigLine;
10
11/// The `system configuration` settings for loading configuration data.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct ConfigurationInput {
15    /// `input format json reorder-list-keys`: reorder list entries so that keys come first.
16    pub json_reorder_list_keys: bool,
17}
18
19pub(super) fn parse_configuration(configuration: &ConfigLine) -> ConfigurationInput {
20    let json = child_block(configuration, "input")
21        .and_then(|input| child_block(input, "format"))
22        .and_then(|format| child_block(format, "json"));
23    ConfigurationInput {
24        json_reorder_list_keys: json.is_some_and(|json| has_leaf(json, "reorder-list-keys")),
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use crate::system::system;
31    use asterust::config::juniper;
32
33    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
34    #[test]
35    fn extracts_the_json_input_format() {
36        let configuration = system(&juniper::parse_braces(
37            "system {\n    configuration {\n        input {\n            format {\n                json {\n                    reorder-list-keys;\n                }\n            }\n        }\n    }\n}\n",
38        ))
39        .unwrap()
40        .configuration
41        .unwrap();
42        assert!(configuration.json_reorder_list_keys);
43    }
44
45    #[test]
46    fn no_configuration_is_none() {
47        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
48        assert!(system.configuration.is_none());
49    }
50}