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

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

/// The `recovery` block of `system snapshot`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SnapshotRecovery {
    /// `Some(true)` for `automatic`, `Some(false)` for `no-automatic`.
    pub automatic: Option<bool>,
    pub configuration_only: bool,
    pub configuration_preference: bool,
    pub on_upgrade: bool,
}

/// The `system snapshot` options.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Snapshot {
    /// `Some(true)` for `configuration`, `Some(false)` for `no-configuration`: whether non-recovery
    /// snapshots store the configuration.
    pub configuration: Option<bool>,
    pub recovery: Option<SnapshotRecovery>,
}

pub(super) fn parse_snapshot(snapshot: &ConfigLine) -> Snapshot {
    Snapshot {
        configuration: toggle(snapshot, "configuration"),
        recovery: child_block(snapshot, "recovery").map(|recovery| SnapshotRecovery {
            automatic: toggle(recovery, "automatic"),
            configuration_only: has_leaf(recovery, "configuration-only"),
            configuration_preference: has_leaf(recovery, "configuration-preference"),
            on_upgrade: has_leaf(recovery, "on-upgrade"),
        }),
    }
}

#[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_snapshot_options() {
        let snapshot = system(&juniper::parse_braces(
            "system {\n    snapshot {\n        configuration;\n        recovery {\n            automatic;\n            configuration-only;\n            on-upgrade;\n        }\n    }\n}\n",
        ))
        .unwrap()
        .snapshot
        .unwrap();
        assert_eq!(snapshot.configuration, Some(true));
        let recovery = snapshot.recovery.unwrap();
        assert_eq!(recovery.automatic, Some(true));
        assert!(recovery.configuration_only);
        assert!(!recovery.configuration_preference);
        assert!(recovery.on_upgrade);
    }

    #[test]
    fn reads_the_negated_snapshot_options() {
        let snapshot = system(&juniper::parse_braces(
            "system {\n    snapshot {\n        no-configuration;\n        recovery {\n            no-automatic;\n        }\n    }\n}\n",
        ))
        .unwrap()
        .snapshot
        .unwrap();
        assert_eq!(snapshot.configuration, Some(false));
        assert_eq!(snapshot.recovery.unwrap().automatic, Some(false));
    }

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