use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use uuid::Uuid; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct SnapshotManifest { pub snapshot_id: Uuid, pub orbit_id: Uuid, pub orbit_name: String, pub labels: Vec, pub provider: String, pub paths: Vec, pub restore_hints: serde_json::Value, } impl SnapshotManifest { /// A copy of a restore target's database taken before it was overwritten. It belongs to the target, not /// to the application's backup history: retention must never weigh it against real captures. pub fn is_restore_safety(&self) -> bool { self.labels.iter().any(|label| label == "restore-safety") || self.restore_hints.get("safety_for").is_some() } /// The restore target a safety copy was taken from. pub fn safety_target(&self) -> Option { self.restore_hints .get("target_id") .and_then(|id| id.as_str()) .map(str::to_string) } } pub fn manifest_hash(manifest: &SnapshotManifest) -> String { let json = serde_json::to_vec(manifest).expect("manifest serializes"); let digest = Sha256::digest(&json); hex::encode(digest) } #[cfg(test)] mod tests { use super::*; #[test] fn hash_is_stable() { let m = SnapshotManifest { snapshot_id: Uuid::nil(), orbit_id: Uuid::nil(), orbit_name: "nightly".into(), labels: vec!["filesystem".into()], provider: "filesystem".into(), paths: vec!["/tmp".into()], restore_hints: serde_json::json!({}), }; let h1 = manifest_hash(&m); let h2 = manifest_hash(&m); assert_eq!(h1, h2); assert_eq!(h1.len(), 64); } }