Refactor Config::configure to take &mut.
authorRichard Dodd <richard.dodd@itp-group.co.uk>
Wed, 4 Oct 2017 17:03:08 +0000 (18:03 +0100)
committerRichard Dodd <richard.dodd@itp-group.co.uk>
Wed, 4 Oct 2017 18:12:50 +0000 (19:12 +0100)
33 files changed:
src/bin/bench.rs
src/bin/build.rs
src/bin/cargo.rs
src/bin/check.rs
src/bin/clean.rs
src/bin/doc.rs
src/bin/fetch.rs
src/bin/generate_lockfile.rs
src/bin/git_checkout.rs
src/bin/help.rs
src/bin/init.rs
src/bin/install.rs
src/bin/locate_project.rs
src/bin/login.rs
src/bin/metadata.rs
src/bin/new.rs
src/bin/owner.rs
src/bin/package.rs
src/bin/pkgid.rs
src/bin/publish.rs
src/bin/read_manifest.rs
src/bin/run.rs
src/bin/rustc.rs
src/bin/rustdoc.rs
src/bin/search.rs
src/bin/test.rs
src/bin/uninstall.rs
src/bin/update.rs
src/bin/verify_project.rs
src/bin/version.rs
src/bin/yank.rs
src/cargo/lib.rs
src/cargo/util/config.rs

index cb39fc9ceb54d8e53790bfa600c8820ef3c39b0c..1aa82dd9f877b5b445f0768f277b15f7e1ff5720 100644 (file)
@@ -95,7 +95,7 @@ not affect how many jobs are used when running the benchmarks.
 Compilation can be customized with the `bench` profile in the manifest.
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-bench; args={:?}",
            env::args().collect::<Vec<_>>());
 
index 89e63b5173f5bea50d6a4111b6c1a0bb0ca9a373..883e30db6b15fa8e3adccb68b1a469b98966be53 100644 (file)
@@ -87,7 +87,7 @@ the manifest. The default profile for this command is `dev`, but passing
 the --release flag will use the `release` profile instead.
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-build; args={:?}",
            env::args().collect::<Vec<_>>());
     config.configure(options.flag_verbose,
index 48c2a134799a51b6e3b695657dc4b61c700d0ce7..e92bcdbe5ff3ba0231e62c18d31f377c8eb0bac9 100644 (file)
@@ -75,7 +75,7 @@ See 'cargo help <command>' for more information on a specific command.
 fn main() {
     env_logger::init().unwrap();
 
-    let config = match Config::default() {
+    let mut config = match Config::default() {
         Ok(cfg) => cfg,
         Err(e) => {
             let mut shell = Shell::new();
@@ -92,7 +92,7 @@ fn main() {
             })
             .collect());
         let rest = &args;
-        cargo::call_main_without_stdin(execute, &config, USAGE, rest, true)
+        cargo::call_main_without_stdin(execute, &mut config, USAGE, rest, true)
     })();
 
     match result {
@@ -146,7 +146,7 @@ each_subcommand!(declare_mod);
   because they are fundamental (and intertwined). Other commands can rely
   on this top-level information.
 */
-fn execute(flags: Flags, config: &Config) -> CliResult {
+fn execute(flags: Flags, config: &mut Config) -> CliResult {
     config.configure(flags.flag_verbose,
                    flags.flag_quiet,
                    &flags.flag_color,
@@ -250,11 +250,12 @@ fn execute(flags: Flags, config: &Config) -> CliResult {
     execute_external_subcommand(config, &args[1], &args)
 }
 
-fn try_execute_builtin_command(config: &Config, args: &[String]) -> Option<CliResult> {
+fn try_execute_builtin_command(config: &mut Config, args: &[String]) -> Option<CliResult> {
     macro_rules! cmd {
         ($name:ident) => (if args[1] == stringify!($name).replace("_", "-") {
             config.shell().set_verbosity(Verbosity::Verbose);
-            let r = cargo::call_main_without_stdin($name::execute, config,
+            let r = cargo::call_main_without_stdin($name::execute,
+                                                   config,
                                                    $name::USAGE,
                                                    &args,
                                                    false);
index 488071d629439152c7481a034cafd6b58e6aa081..9822041307672473b1cbc463f3d573d367f06a96 100644 (file)
@@ -87,7 +87,7 @@ pub struct Options {
     flag_z: Vec<String>,
 }
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-check; args={:?}",
            env::args().collect::<Vec<_>>());
 
index 9e331dd64b9db449ceee32b1f24c404e2329fd97..446b5e502b27e90c4ffc3374ce50a98d0e6ac75b 100644 (file)
@@ -45,7 +45,7 @@ given, then all packages' artifacts are removed. For more information on SPEC
 and its format, see the `cargo help pkgid` command.
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-clean; args={:?}", env::args().collect::<Vec<_>>());
     config.configure(options.flag_verbose,
                      options.flag_quiet,
index 836b10ca6d108e29e2e3d280cd5c8dbe14a25102..6e17836963c2d0d1cafc61f6b33c2f113b8aea39 100644 (file)
@@ -74,7 +74,7 @@ current package is documented. For more information on SPEC and its format, see
 the `cargo help pkgid` command.
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-check; args={:?}",
            env::args().collect::<Vec<_>>());
 
index 8dce4633fe9ea41e638603683ca62b081d230d24..880b77eb60e2b9fdc06f4843498f9f42e987346d 100644 (file)
@@ -41,7 +41,7 @@ If the lockfile is not available, then this is the equivalent of
 all updated.
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index f6733768cfb78e2cf3e599135369f1ca1f0e3679..11cc83639d3c958455081768175454e35fe78707 100644 (file)
@@ -34,7 +34,7 @@ Options:
     -Z FLAG ...              Unstable (nightly-only) flags to Cargo
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-generate-lockfile; args={:?}", env::args().collect::<Vec<_>>());
     config.configure(options.flag_verbose,
                      options.flag_quiet,
index ed020c7eff17046147641bfd5aa1633fa6a3a8c3..ec3ae7a5b11496d21ccac9c1f03095efa15175bc 100644 (file)
@@ -32,7 +32,7 @@ Options:
     -Z FLAG ...              Unstable (nightly-only) flags to Cargo
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index c5059e4ff5e15d687af38ee4ff7601d66835f2bc..f7f564ee7b9a81a4bf28200d6dc66461d7e7b560 100644 (file)
@@ -14,7 +14,7 @@ Options:
     -h, --help          Print this message
 ";
 
-pub fn execute(_: Options, _: &Config) -> CliResult {
+pub fn execute(_: Options, _: &mut Config) -> CliResult {
     // This is a dummy command just so that `cargo help help` works.
     // The actual delegation of help flag to subcommands is handled by the
     // cargo command.
index 29319e181913e4c13ac8d27b0fa03f75d47f55c0..9252ddd3900f3f9e0ed49521d87312f35d46100c 100644 (file)
@@ -43,7 +43,7 @@ Options:
     -Z FLAG ...         Unstable (nightly-only) flags to Cargo
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-init; args={:?}", env::args().collect::<Vec<_>>());
     config.configure(options.flag_verbose,
                      options.flag_quiet,
index 4fedab896eda3f1f243e07cbebded8d3d93abe96..c7062d40c6fd39a08ec6e62078dc664d5caa7e82 100644 (file)
@@ -101,7 +101,7 @@ the more explicit `install --path .`.
 The `--list` option will list all installed packages (and their versions).
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index e0c6164e7c73789ac687eea85a8ec37aa5b6809a..6e16cca2d37331e758d6d6df7b5dd1cc16e9cce0 100644 (file)
@@ -23,8 +23,7 @@ pub struct ProjectLocation {
     root: String
 }
 
-pub fn execute(flags: LocateProjectFlags,
-               config: &Config) -> CliResult {
+pub fn execute(flags: LocateProjectFlags, config: &mut Config) -> CliResult {
     let root = find_root_manifest_for_wd(flags.flag_manifest_path, config.cwd())?;
 
     let string = root.to_str()
index c41118cc35066d09f7601a51cf47c5c3185aa77e..99ce8e75542af129fbcd5ffd0a92d2b2ba43adf2 100644 (file)
@@ -37,7 +37,7 @@ Options:
 
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index 9d52d610b2194d82eac1026836073d88ca9b1bd6..d10fe8c445e57ef5690586d540d2ffd7cb434d41 100644 (file)
@@ -46,7 +46,7 @@ Options:
     -Z FLAG ...                Unstable (nightly-only) flags to Cargo
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index dc0878d195c2264e2c50bb7dc76702b3e5e4e3af..c006fd1e4d9407f62ae238e8df064d9d30c87c27 100644 (file)
@@ -43,7 +43,7 @@ Options:
     -Z FLAG ...         Unstable (nightly-only) flags to Cargo
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-new; args={:?}", env::args().collect::<Vec<_>>());
     config.configure(options.flag_verbose,
                      options.flag_quiet,
index 438bb109deec64138025e8d93b3771f4680fdebd..6c76a6fafc5381bdff21a24def0c61fab17c4c14 100644 (file)
@@ -47,7 +47,7 @@ See http://doc.crates.io/crates-io.html#cargo-owner for detailed documentation
 and troubleshooting.
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index 06fc589bd948e7f17d94663c5268e19002d1bc67..31e3330ad5f6a486528ff1823fc68a07cc4e6742 100644 (file)
@@ -44,7 +44,7 @@ Options:
     -Z FLAG ...             Unstable (nightly-only) flags to Cargo
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index d69c982df9175fe5e5bc4a36ef6af817f34a2b25..01f8a8f6ba522ccc6afff4d7ec6f12666911771c 100644 (file)
@@ -55,8 +55,7 @@ Example Package IDs
 
 ";
 
-pub fn execute(options: Options,
-               config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index 0ce5d897fdb4d0251b148786b535f62f09fa681f..c34a0e2706773ac4399514d191e9b80299cb1de7 100644 (file)
@@ -49,7 +49,7 @@ Options:
 
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index 651174e87079b6718881a0258c41f3d1f0452d6c..eee2210d835eb85308abeb837db048140df8aa91 100644 (file)
@@ -26,7 +26,7 @@ Options:
     --color WHEN             Coloring: auto, always, never
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-read-manifest; args={:?}",
            env::args().collect::<Vec<_>>());
     config.shell().set_color_choice(options.flag_color.as_ref().map(|s| &s[..]))?;
index d6bee1f09f55cc56d13dcdbcbb529a1ab3002714..21486baf6eaabfcee1241e02a7899452276a10b7 100644 (file)
@@ -64,7 +64,7 @@ arguments to both Cargo and the binary, the ones after `--` go to the binary,
 the ones before go to Cargo.
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index fafa8632dcbdafd69a7e0747c72527dcb9bfd716..e6f5dc540f6fcd2235abef275631d154db08cff5 100644 (file)
@@ -86,7 +86,7 @@ processes spawned by Cargo, use the $RUSTFLAGS environment variable or the
 `build.rustflags` configuration option.
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-rustc; args={:?}",
            env::args().collect::<Vec<_>>());
     config.configure(options.flag_verbose,
index 6ff12689cc32cffa1dc27ad269f35082f25f7479..156a6b86796a53bb755707bd3ef6abe7afdede05 100644 (file)
@@ -83,7 +83,7 @@ current package is documented. For more information on SPEC and its format, see
 the `cargo help pkgid` command.
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index 5d5846e857e4e8bd227ab0beed833da6b54a708e..165dea1c8b2cf6d85d04934d8f5c524adeffff06 100644 (file)
@@ -38,7 +38,7 @@ Options:
     -Z FLAG ...              Unstable (nightly-only) flags to Cargo
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index 7e30e38e6c32b1360f2c553f0bd5688788e2ee9d..2208a18afdd6f50dded99f084046eb52bef048a1 100644 (file)
@@ -116,7 +116,7 @@ To get the list of all options available for the test binaries use this:
   cargo test -- --help
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-test; args={:?}",
            env::args().collect::<Vec<_>>());
 
index 69079265e665e2437d0f79b5a41cc89ba0d08755..2adf2d04156ea094916fab0a54f54d18abf1fbe0 100644 (file)
@@ -40,7 +40,7 @@ uninstalled for a crate but the `--bin` and `--example` flags can be used to
 only uninstall particular binaries.
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index a9a57312ee1e524248d9d9309309cea7f67b88fd..1e7f92b535abe3b1c1a8d59425cd191be52f23d5 100644 (file)
@@ -60,7 +60,7 @@ updated.
 For more information about package id specifications, see `cargo help pkgid`.
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-update; args={:?}", env::args().collect::<Vec<_>>());
     config.configure(options.flag_verbose,
                      options.flag_quiet,
index 208905671fa75930fd3f261ff7dcd3cb65bb2dc4..a054470263b600955334f1ac2238231570e84fca 100644 (file)
@@ -39,7 +39,7 @@ Options:
     -Z FLAG ...             Unstable (nightly-only) flags to Cargo
 ";
 
-pub fn execute(args: Flags, config: &Config) -> CliResult {
+pub fn execute(args: Flags, config: &mut Config) -> CliResult {
     config.configure(args.flag_verbose,
                      args.flag_quiet,
                      &args.flag_color,
index f036def0367a3a394f85072922a71a84e7f328ec..6d3772f1c978864dcb0de232e639ad5c15bf6024 100644 (file)
@@ -18,7 +18,7 @@ Options:
     --color WHEN             Coloring: auto, always, never
 ";
 
-pub fn execute(_: Options, _: &Config) -> CliResult {
+pub fn execute(_: Options, _: &mut Config) -> CliResult {
     debug!("executing; cmd=cargo-version; args={:?}", env::args().collect::<Vec<_>>());
 
     println!("{}", cargo::version());
index 0526c4c4f0ddac9fd6ea83d222f0f3f1a57a2004..a00892a51937a75aaa6d8ab0ac8454e7d33760f9 100644 (file)
@@ -45,7 +45,7 @@ download the yanked version to use it. Cargo will, however, not allow any new
 crates to be locked to any yanked version.
 ";
 
-pub fn execute(options: Options, config: &Config) -> CliResult {
+pub fn execute(options: Options, config: &mut Config) -> CliResult {
     config.configure(options.flag_verbose,
                      options.flag_quiet,
                      &options.flag_color,
index 3ecc7b1add2dfba667e5f19c7e9274013b301a0d..f20118b80045ba2f3ded74ac3278bcf052b219d0 100755 (executable)
@@ -109,8 +109,8 @@ impl fmt::Display for VersionInfo {
 }
 
 pub fn call_main_without_stdin<'de, Flags: Deserialize<'de>>(
-            exec: fn(Flags, &Config) -> CliResult,
-            config: &Config,
+            exec: fn(Flags, &mut Config) -> CliResult,
+            config: &mut Config,
             usage: &str,
             args: &[String],
             options_first: bool) -> CliResult
index 38e2e303fab8bb2a578fddae8bd51cc01cf0b935..c2316a9609ac7bebdcb8d8722cb6d85b8f04b7bc 100644 (file)
@@ -107,37 +107,46 @@ impl Config {
         Ok(Config::new(shell, cwd, homedir))
     }
 
+    /// The user's cargo home directory (OS-dependent)
     pub fn home(&self) -> &Filesystem { &self.home_path }
 
+    /// The cargo git directory (`<cargo_home>/git`)
     pub fn git_path(&self) -> Filesystem {
         self.home_path.join("git")
     }
 
+    /// The cargo registry index directory (`<cargo_home>/registry/index`)
     pub fn registry_index_path(&self) -> Filesystem {
         self.home_path.join("registry").join("index")
     }
 
+    /// The cargo registry cache directory (`<cargo_home>/registry/path`)
     pub fn registry_cache_path(&self) -> Filesystem {
         self.home_path.join("registry").join("cache")
     }
 
+    /// The cargo registry source directory (`<cargo_home>/registry/src`)
     pub fn registry_source_path(&self) -> Filesystem {
         self.home_path.join("registry").join("src")
     }
 
+    /// Get a reference to the shell, for e.g. writing error messages
     pub fn shell(&self) -> RefMut<Shell> {
         self.shell.borrow_mut()
     }
 
+    /// Get the path to the `rustdoc` executable
     pub fn rustdoc(&self) -> CargoResult<&Path> {
         self.rustdoc.get_or_try_init(|| self.get_tool("rustdoc")).map(AsRef::as_ref)
     }
 
+    /// Get the path to the `rustc` executable
     pub fn rustc(&self) -> CargoResult<&Rustc> {
         self.rustc.get_or_try_init(|| Rustc::new(self.get_tool("rustc")?,
                                                  self.maybe_get_tool("rustc_wrapper")?))
     }
 
+    /// Get the path to the `cargo` executable
     pub fn cargo_exe(&self) -> CargoResult<&Path> {
         self.cargo_exe.get_or_try_init(||
             env::current_exe().and_then(|path| path.canonicalize())
@@ -384,7 +393,7 @@ impl Config {
         })
     }
 
-    pub fn configure(&self,
+    pub fn configure(&mut self,
                      verbose: u32,
                      quiet: Option<bool>,
                      color: &Option<String>,
@@ -450,6 +459,7 @@ impl Config {
         !self.frozen.get() && !self.locked.get()
     }
 
+    /// Loads configuration from the filesystem
     pub fn load_values(&self) -> CargoResult<HashMap<String, ConfigValue>> {
         let mut cfg = CV::Table(HashMap::new(), PathBuf::from("."));
 
@@ -483,6 +493,7 @@ impl Config {
         }
     }
 
+    /// Loads credentials config from the credentials file into the ConfigValue object, if present.
     fn load_credentials(&self, cfg: &mut ConfigValue) -> CargoResult<()> {
         let home_path = self.home_path.clone().into_path_unlocked();
         let credentials = home_path.join("credentials");
@@ -516,6 +527,8 @@ impl Config {
 
         match (registry, value) {
             (&mut CV::Table(ref mut old, _), CV::Table(ref mut new, _)) => {
+                // Take ownership of `new` by swapping it with an empty hashmap, so we can move
+                // into an iterator.
                 let new = mem::replace(new, HashMap::new());
                 for (key, value) in new {
                     old.insert(key, value);