fn apply_edits()

in cargo-guppy/src/mv.rs [359:405]


fn apply_edits(manifest_path: &Utf8Path, edits: &[ManifestEdit<'_>]) -> Result<()> {
    let mut document = read_toml(manifest_path)?;
    let table = document.as_table_mut();

    // Grab the list of target specs.
    let all_targets = match table.get("target") {
        Some(target_section) => match target_section.as_table() {
            Some(table) => table.iter().map(|(target, _)| target.to_string()).collect(),
            None => {
                bail!("in {}, [target] is not a table", manifest_path);
            }
        },
        None => {
            // There's no 'target' section in the manifest.
            Vec::new()
        }
    };

    // Search through:
    // * dependencies, build-dependencies, dev-dependencies
    // * [target.'foo'.dependencies], .build-dependencies and .dev-dependencies
    for edit in edits {
        apply_edit(table, edit)
            .wrap_err_with(|| eyre!("error while applying edits to {}", manifest_path))?;
        for target in &all_targets {
            let target_table = match &mut table["target"][target] {
                Item::Table(target_table) => target_table,
                _ => {
                    // Not a table, skip it.
                    continue;
                }
            };
            apply_edit(target_table, edit).wrap_err_with(|| {
                eyre!(
                    "error while applying edits to {}, section [target.'{}']",
                    manifest_path,
                    target
                )
            })?;
        }
    }

    fs::write(manifest_path, document.to_string())
        .wrap_err_with(|| eyre!("error while writing manifest {}", manifest_path))?;

    Ok(())
}