fn write_to_cargo_toml()

in tools/cargo-hakari/src/command.rs [479:510]


fn write_to_cargo_toml(
    existing_toml: HakariCargoToml,
    new_contents: &str,
    diff: bool,
    output: OutputContext,
) -> Result<i32> {
    if diff {
        let patch = existing_toml.diff_toml(new_contents);
        let mut formatter = PatchFormatter::new();
        if output.color.is_enabled() {
            formatter = formatter.with_color();
        }
        info!("\n{}", formatter.fmt_patch(&patch));
        if patch.hunks().is_empty() {
            // No differences.
            Ok(0)
        } else {
            Ok(1)
        }
    } else {
        if !existing_toml.is_changed(new_contents) {
            info!("no changes detected");
        } else {
            existing_toml
                .write_to_file(new_contents)
                .with_context(|| "error writing updated Hakari contents")?;
            info!("contents updated");
            regenerate_lockfile(output)?;
        }
        Ok(0)
    }
}