cargo_eval/util/
mod.rs

1use anyhow::Context as _;
2
3use crate::CargoResult;
4
5pub mod script;
6
7pub fn write_if_changed(path: &std::path::Path, new: &str) -> CargoResult<()> {
8    let write_needed = match std::fs::read_to_string(path) {
9        Ok(current) => current != new,
10        Err(_) => true,
11    };
12    if write_needed {
13        std::fs::write(path, new).with_context(|| format!("failed to write {}", path.display()))?;
14    }
15    Ok(())
16}