commons/src/rollback.rs
2026-08-23 22:38:12 +02:00

50 lines
1.5 KiB
Rust

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
/// Opaque reference to a pre-mutation snapshot owned by the workspace
/// executor. Reef stores this reference as workflow metadata; it must never
/// contain a host path or the snapshot bytes.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RollbackReference {
pub reference_id: Uuid,
pub execution_id: Uuid,
pub workspace_lease_id: Uuid,
pub workspace_id: Uuid,
pub project_id: Uuid,
pub expires_at: DateTime<Utc>,
}
impl RollbackReference {
pub fn is_expired(&self, now: DateTime<Utc>) -> bool {
self.expires_at <= now
}
pub fn validates_execution(&self, execution_id: Uuid) -> bool {
self.execution_id == execution_id && self.reference_id != Uuid::nil()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn reference() -> RollbackReference {
RollbackReference {
reference_id: Uuid::new_v4(),
execution_id: Uuid::new_v4(),
workspace_lease_id: Uuid::new_v4(),
workspace_id: Uuid::new_v4(),
project_id: Uuid::new_v4(),
expires_at: Utc::now() + chrono::Duration::minutes(1),
}
}
#[test]
fn reference_is_opaque_and_execution_bound() {
let reference = reference();
assert!(reference.validates_execution(reference.execution_id));
assert!(!reference.validates_execution(Uuid::new_v4()));
assert!(!reference.is_expired(Utc::now()));
}
}