Skip to content

Commit db4ed03

Browse files
committed
refactor: separate get_display_path_and_check_membership
Signed-off-by: hi-rustin <[email protected]>
1 parent 874645e commit db4ed03

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,8 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
816816
// This should not block the creation of the new project. It is only a best effort to
817817
// inherit the workspace package keys.
818818
if let Ok(mut workspace_document) = root_manifest.parse::<toml_edit::Document>() {
819-
let (display_path, can_be_a_member) = get_display_path_and_check_membership(
820-
&root_manifest_path,
821-
&path,
822-
&workspace_document,
823-
)?;
819+
let display_path = get_display_path(&root_manifest_path, &path)?;
820+
let can_be_a_member = can_be_workspace_member(&display_path, &workspace_document)?;
824821
// Only try to inherit the workspace stuff if the new package can be a member of the workspace.
825822
if can_be_a_member {
826823
if let Some(workspace_package_keys) = workspace_document
@@ -1003,11 +1000,7 @@ fn update_manifest_with_new_member(
10031000
)
10041001
}
10051002

1006-
fn get_display_path_and_check_membership(
1007-
root_manifest_path: &Path,
1008-
package_path: &Path,
1009-
workspace_document: &toml_edit::Document,
1010-
) -> CargoResult<(String, bool)> {
1003+
fn get_display_path(root_manifest_path: &Path, package_path: &Path) -> CargoResult<String> {
10111004
// Find the relative path for the package from the workspace root directory.
10121005
let workspace_root = root_manifest_path.parent().with_context(|| {
10131006
format!(
@@ -1031,7 +1024,14 @@ fn get_display_path_and_check_membership(
10311024
components.push(comp);
10321025
}
10331026
let display_path = components.join("/");
1027+
Ok(display_path)
1028+
}
10341029

1030+
// Check if the package can be a member of the workspace.
1031+
fn can_be_workspace_member(
1032+
display_path: &str,
1033+
workspace_document: &toml_edit::Document,
1034+
) -> CargoResult<bool> {
10351035
if let Some(exclude) = workspace_document
10361036
.get("workspace")
10371037
.and_then(|workspace| workspace.get("exclude"))
@@ -1042,10 +1042,9 @@ fn get_display_path_and_check_membership(
10421042
.as_str()
10431043
.with_context(|| format!("invalid non-string exclude path `{}`", member))?;
10441044
if pat == display_path {
1045-
return Ok((display_path, false));
1045+
return Ok(false);
10461046
}
10471047
}
10481048
}
1049-
1050-
Ok((display_path, true))
1049+
Ok(true)
10511050
}

0 commit comments

Comments
 (0)