use std::path::Path;
use tokio::io::AsyncWriteExt;
use color_eyre::eyre;
use tokio::fs;
use uuid::Uuid;
pub async fn write_text(text: &str, path: impl AsRef<Path>) -> eyre::Result<()> {
let path = path.as_ref();
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).await?;
}
let tmp_path = path.with_extension(format!("tmp-{}", Uuid::new_v4()));
let mut file = fs::File::create(&tmp_path).await?;
file.write_all(text.as_bytes()).await?;
file.flush().await?;
file.sync_all().await?;
fs::rename(&tmp_path, path).await?;
Ok(())
}