use std::path::Path;
#[derive(Debug, PartialEq, Eq)]
pub enum Outcome {
Written,
Unchanged,
}
pub fn sync_page(path: &Path, content: &str, bless: bool) -> Result<Outcome, String> {
if bless {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.map_err(|error| format!("cannot create {}: {error}", parent.display()))?;
}
std::fs::write(path, content)
.map_err(|error| format!("cannot write {}: {error}", path.display()))?;
return Ok(Outcome::Written);
}
let committed = match std::fs::read_to_string(path) {
Ok(text) => text,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => String::new(),
Err(error) => return Err(format!("cannot read {}: {error}", path.display())),
};
if committed == content {
return Ok(Outcome::Unchanged);
}
Err(format!(
"{} is stale -- regenerate it with `{}`",
path.display(),
super::pages::REGENERATE
))
}