use std::fmt::Write;
use uuid::Uuid;
use super::{
plan::{Action, Plan},
safepath::SafePath,
};
use crate::constants::NS_TAG;
fn namespace() -> Uuid {
Uuid::new_v5(&Uuid::NAMESPACE_URL, NS_TAG.as_bytes())
}
fn sp_rel(p: &SafePath) -> String {
p.rel().to_string_lossy().to_string()
}
fn serialize_action(a: &Action) -> String {
match a {
Action::EnsureSymlink { source, target } => {
format!("E:{}->{}", sp_rel(source), sp_rel(target))
}
Action::RestoreFromBackup { target } => {
format!("R:{}", sp_rel(target))
}
}
}
#[must_use]
pub fn plan_id(plan: &Plan) -> Uuid {
let ns = namespace();
let mut s = String::new();
for a in &plan.actions {
s.push_str(&serialize_action(a));
s.push('\n');
}
Uuid::new_v5(&ns, s.as_bytes())
}
#[must_use]
pub fn action_id(plan_id: &Uuid, action: &Action, idx: usize) -> Uuid {
let mut s = serialize_action(action);
let _ = write!(s, "#{idx}"); Uuid::new_v5(plan_id, s.as_bytes())
}