use crate::error::Error;
use std::path::Path;
use tokio::fs;
pub async fn write_file_atomic(
path: &Path,
content: &str,
force: bool,
) -> Result<WriteOutcome, Error> {
if path.exists() && !force {
return Ok(WriteOutcome::Skipped);
}
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).await?;
}
let temp_path = path.with_extension("tmp");
fs::write(&temp_path, content).await?;
fs::rename(&temp_path, path).await?;
Ok(WriteOutcome::Written)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WriteOutcome {
Written,
Skipped,
}