]> git.proxmox.com Git - cargo.git/commitdiff
Add --name hint for `new` when package name is retricted
authorWeihang Lo <me@weihanglo.tw>
Thu, 3 Sep 2020 17:27:46 +0000 (01:27 +0800)
committerWeihang Lo <me@weihanglo.tw>
Thu, 3 Sep 2020 17:27:46 +0000 (01:27 +0800)
src/cargo/ops/cargo_new.rs

index 9bbcc6d9f54d135ef4ab359bf1af000f4af5a9d2..a9b5938eb4b92f6666add4b71ca35a68ee33b3d6 100644 (file)
@@ -154,7 +154,19 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions) -> CargoResult<&'a str> {
     })
 }
 
-fn check_name(name: &str, name_help: &str, has_bin: bool, shell: &mut Shell) -> CargoResult<()> {
+fn check_name(
+    name: &str,
+    show_name_help: bool,
+    has_bin: bool,
+    shell: &mut Shell,
+) -> CargoResult<()> {
+    // If --name is already used to override, no point in suggesting it
+    // again as a fix.
+    let name_help = if show_name_help {
+        "\nuse --name to override crate name"
+    } else {
+        ""
+    };
     restricted_names::validate_package_name(name, "crate name", name_help)?;
 
     if restricted_names::is_keyword(name) {
@@ -363,7 +375,12 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
     }
 
     let name = get_name(path, opts)?;
-    check_name(name, "", opts.kind.is_bin(), &mut config.shell())?;
+    check_name(
+        name,
+        opts.name.is_none(),
+        opts.kind.is_bin(),
+        &mut config.shell(),
+    )?;
 
     let mkopts = MkOptions {
         version_control: opts.version_control,
@@ -411,13 +428,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
         // user may mean "initialize for library, but also add binary target"
     }
     let has_bin = src_paths_types.iter().any(|x| x.bin);
-    // If --name is already used to override, no point in suggesting it
-    // again as a fix.
-    let name_help = match opts.name {
-        Some(_) => "",
-        None => "\nuse --name to override crate name",
-    };
-    check_name(name, name_help, has_bin, &mut config.shell())?;
+    check_name(name, opts.name.is_none(), has_bin, &mut config.shell())?;
 
     let mut version_control = opts.version_control;