use crate::{child_block, has_leaf, toggle};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SnapshotRecovery {
pub automatic: Option<bool>,
pub configuration_only: bool,
pub configuration_preference: bool,
pub on_upgrade: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Snapshot {
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;
#[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());
}
}