use super::json::check_json_size;
use super::{
BackupLayoutGuard, JournalLock, JournalLockError, PersistenceError, create_json_durable,
read_json,
};
use crate::model::{
artifacts::ArtifactChecksumRecord,
effect_graph::{EffectGraphRecord, MAX_EFFECT_GRAPH_BYTES},
};
use thiserror::Error;
pub fn create_effect_graph(
layout: &BackupLayoutGuard,
record: &EffectGraphRecord,
) -> Result<(), EffectGraphPersistenceError> {
layout.check_root()?;
let path = layout.root().join("effect-graph.json");
let _lock = JournalLock::acquire(&path)?;
check_json_size(record, MAX_EFFECT_GRAPH_BYTES)?;
create_json_durable(&path, record)?;
Ok(())
}
pub fn read_effect_graph(
layout: &BackupLayoutGuard,
expected: &ArtifactChecksumRecord,
) -> Result<EffectGraphRecord, EffectGraphPersistenceError> {
layout.check_root()?;
let path = layout.root().join("effect-graph.json");
let _lock = JournalLock::acquire(&path)?;
let record: EffectGraphRecord = read_json(&path, MAX_EFFECT_GRAPH_BYTES)?;
check_json_size(&record, MAX_EFFECT_GRAPH_BYTES)?;
if &record.digest() != expected {
return Err(EffectGraphPersistenceError::DigestMismatch);
}
Ok(record)
}
#[derive(Debug, Error)]
pub enum EffectGraphPersistenceError {
#[error("effect graph digest mismatch")]
DigestMismatch,
#[error(transparent)]
Lock(#[from] JournalLockError),
#[error(transparent)]
Persistence(#[from] PersistenceError),
}
#[cfg(all(test, unix))]
mod tests;