fn moves_for()

in cargo-guppy/src/mv.rs [292:334]


fn moves_for<'g: 'a, 'a>(
    pkg_graph: &'g PackageGraph,
    src_dir: &'a Utf8Path,
    dest_dir: &'a DestDir,
) -> Result<Vec<(&'g Utf8Path, PackageMove<'g>)>> {
    // TODO: speed this up using a trie in guppy? Probably not that important.
    let workspace = pkg_graph.workspace();
    let workspace_root = workspace.root();
    // Ensure that the path refers to a package.
    let _package = workspace.member_by_path(src_dir)?;

    // Now look for all paths that start with the package.
    workspace
        .iter_by_path()
        .filter_map(move |(workspace_path, package)| {
            if workspace_path.starts_with(src_dir) {
                let pair = dest_dir.join(workspace_path, src_dir).and_then(|new_path| {
                    // Check that the new path doesn't exist already.
                    let abs_new_path = workspace_root.join(&new_path);
                    if abs_new_path.exists() {
                        bail!(
                            "attempted to move {} to {}, which already exists",
                            workspace_path,
                            new_path
                        );
                    }

                    // new_path can sometimes have a trailing slash -- remove it if it does.
                    let mut new_path = new_path.into_string();
                    if new_path.ends_with(MAIN_SEPARATOR) {
                        new_path.pop();
                    }
                    let new_path = new_path.into();

                    Ok((workspace_path, PackageMove { package, new_path }))
                });
                Some(pair)
            } else {
                None
            }
        })
        .collect()
}