use anyhow::{Context, Result};
use std::path::Path;
pub fn atomic_write(path: &Path, content: &[u8]) -> Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("Failed to create directory: {}", parent.display()))?;
}
let tmp_path = path.with_extension("tmp");
std::fs::write(&tmp_path, content)
.with_context(|| format!("Failed to write temp file: {}", tmp_path.display()))?;
std::fs::rename(&tmp_path, path).with_context(|| {
format!(
"Failed to rename {} -> {}",
tmp_path.display(),
path.display()
)
})?;
Ok(())
}
pub fn atomic_write_str(path: &Path, content: &str) -> Result<()> {
atomic_write(path, content.as_bytes())
}
#[cfg(test)]
#[path = "file_util_tests.rs"]
mod tests;