Skip to main content

ralph/queue/
save.rs

1//! Queue file saving functionality.
2//!
3//! Responsibilities:
4//! - Serialize queue files to JSON with pretty formatting.
5//! - Write queue files atomically to prevent corruption.
6//!
7//! Not handled here:
8//! - Queue file loading or backup creation.
9//! - Lock acquisition (assumed to be held by caller).
10//!
11//! Invariants/assumptions:
12//! - Queue files are written atomically using `write_atomic`.
13//! - Serialization should never fail for valid QueueFile structures.
14
15use crate::contracts::QueueFile;
16use crate::fsutil;
17use anyhow::{Context, Result};
18use std::path::Path;
19
20pub fn save_queue(path: &Path, queue: &QueueFile) -> Result<()> {
21    let rendered = serde_json::to_string_pretty(queue).context("serialize queue JSON")?;
22    fsutil::write_atomic(path, rendered.as_bytes())
23        .with_context(|| format!("write queue JSON {}", path.display()))?;
24    Ok(())
25}