use std::{
fs::File,
io::{self, Write as _},
path::Path,
};
pub fn replace(path: &Path, bytes: &[u8]) -> io::Result<()> {
let parent = path
.parent()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "state path has no parent"))?;
std::fs::create_dir_all(parent)?;
let mut temporary = tempfile::NamedTempFile::new_in(parent)?;
temporary.write_all(bytes)?;
temporary.as_file().sync_all()?;
temporary.persist(path).map_err(|error| error.error)?;
if let Ok(directory) = File::open(parent) {
let _ = directory.sync_all();
}
Ok(())
}