use std::{io::Write as _, path::Path};
pub(crate) fn replace(path: &Path, contents: impl AsRef<[u8]>) -> std::io::Result<()> {
let parent = path.parent().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"destination has no parent",
)
})?;
let mut temporary = tempfile::NamedTempFile::new_in(parent)?;
temporary.write_all(contents.as_ref())?;
temporary.flush()?;
temporary.as_file().sync_all()?;
temporary.persist(path).map_err(|error| error.error)?;
Ok(())
}