use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::checkpoint::{Checkpoint, SyncPhase};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckpointFile {
pub database_type: String,
pub checkpoint: serde_json::Value,
pub phase: SyncPhase,
pub created_at: DateTime<Utc>,
}
impl CheckpointFile {
pub fn new<C: Checkpoint>(checkpoint: &C, phase: SyncPhase) -> anyhow::Result<Self> {
Ok(Self {
database_type: C::DATABASE_TYPE.to_string(),
checkpoint: serde_json::to_value(checkpoint)?,
phase,
created_at: Utc::now(),
})
}
pub fn parse<C: Checkpoint>(&self) -> anyhow::Result<C> {
if self.database_type != C::DATABASE_TYPE {
anyhow::bail!(
"Checkpoint type mismatch: expected '{}', found '{}'",
C::DATABASE_TYPE,
self.database_type
);
}
Ok(serde_json::from_value(self.checkpoint.clone())?)
}
pub fn database_type(&self) -> &str {
&self.database_type
}
pub fn phase(&self) -> &SyncPhase {
&self.phase
}
pub fn created_at(&self) -> DateTime<Utc> {
self.created_at
}
}