From ddd44c8113c42ae6cb9e558b042e1018cb1b865f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 12 Sep 2022 08:59:10 -0500 Subject: [PATCH 01/11] style(add): Clean up Context import --- src/cargo/ops/cargo_add/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index c584a03f858..fdaeaefe680 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -4,11 +4,11 @@ mod crate_spec; mod dependency; mod manifest; -use anyhow::Context; use std::collections::BTreeSet; use std::collections::VecDeque; use std::path::Path; +use anyhow::Context as _; use cargo_util::paths; use indexmap::IndexSet; use termcolor::Color::Green; From 74e1a705626ced8c17dbb2d65f22164764552f85 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 12 Sep 2022 09:11:14 -0500 Subject: [PATCH 02/11] refactor(add): Decouple available features from inheritance --- src/cargo/ops/cargo_add/mod.rs | 40 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index fdaeaefe680..ede28c7e86d 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -339,7 +339,25 @@ fn resolve_dependency( dependency = dependency.clear_version(); } - dependency = populate_available_features(dependency, config, registry, ws)?; + let query = dependency.query(config)?; + let query = match query { + MaybeWorkspace::Workspace(_workspace) => { + let dep = find_workspace_dep(dependency.toml_key(), ws.root_manifest())?; + if let Some(features) = dep.features.clone() { + dependency = dependency.set_inherited_features(features); + } + let query = dep.query(config)?; + match query { + MaybeWorkspace::Workspace(_) => { + unreachable!("This should have been caught when parsing a workspace root") + } + MaybeWorkspace::Other(query) => query, + } + } + MaybeWorkspace::Other(query) => query, + }; + + dependency = populate_available_features(dependency, &query, registry)?; Ok(dependency) } @@ -585,31 +603,13 @@ fn populate_dependency(mut dependency: Dependency, arg: &DepOp) -> Dependency { /// Lookup available features fn populate_available_features( mut dependency: Dependency, - config: &Config, + query: &crate::core::dependency::Dependency, registry: &mut PackageRegistry<'_>, - ws: &Workspace<'_>, ) -> CargoResult { if !dependency.available_features.is_empty() { return Ok(dependency); } - let query = dependency.query(config)?; - let query = match query { - MaybeWorkspace::Workspace(_workspace) => { - let dep = find_workspace_dep(dependency.toml_key(), ws.root_manifest())?; - if let Some(features) = dep.features.clone() { - dependency = dependency.set_inherited_features(features); - } - let query = dep.query(config)?; - match query { - MaybeWorkspace::Workspace(_) => { - unreachable!("This should have been caught when parsing a workspace root") - } - MaybeWorkspace::Other(query) => query, - } - } - MaybeWorkspace::Other(query) => query, - }; let possibilities = loop { match registry.query_vec(&query, QueryKind::Fuzzy) { std::task::Poll::Ready(res) => { From 2da6c046c086e6c699eda88f8828a60436facbc9 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 12 Sep 2022 09:18:15 -0500 Subject: [PATCH 03/11] refactor(add): Consolidate looking up available features This optimization doesn't really make a difference and it makes it harder to separate UI / edit concerns. --- src/cargo/ops/cargo_add/dependency.rs | 9 --------- src/cargo/ops/cargo_add/mod.rs | 8 ++------ 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/src/cargo/ops/cargo_add/dependency.rs b/src/cargo/ops/cargo_add/dependency.rs index e86f12e033c..64691777e45 100644 --- a/src/cargo/ops/cargo_add/dependency.rs +++ b/src/cargo/ops/cargo_add/dependency.rs @@ -85,15 +85,6 @@ impl Dependency { self } - /// Set the available features of the dependency to a given vec - pub fn set_available_features( - mut self, - available_features: BTreeMap>, - ) -> Self { - self.available_features = available_features; - self - } - /// Populate from cargo pub fn set_available_features_from_cargo( mut self, diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index ede28c7e86d..57df8c6d8f6 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -284,9 +284,7 @@ fn resolve_dependency( // Overwrite with `crate_spec` old_dep.source = selected_dep.source; } - old_dep = populate_dependency(old_dep, arg); - old_dep.available_features = selected_dep.available_features; - old_dep + populate_dependency(old_dep, arg) } } else { selected_dep @@ -318,9 +316,7 @@ fn resolve_dependency( ))?; dependency.name = latest.name; // Normalize the name } - dependency = dependency - .set_source(latest.source.expect("latest always has a source")) - .set_available_features(latest.available_features); + dependency = dependency.set_source(latest.source.expect("latest always has a source")); } } From 2715b560ba9f2f7cb5e3f88b62a90b0260136812 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 12 Sep 2022 09:22:24 -0500 Subject: [PATCH 04/11] refactor(add): Pull UI concerns out of edit logic --- src/cargo/ops/cargo_add/dependency.rs | 36 +------------- src/cargo/ops/cargo_add/mod.rs | 72 ++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 41 deletions(-) diff --git a/src/cargo/ops/cargo_add/dependency.rs b/src/cargo/ops/cargo_add/dependency.rs index 64691777e45..d105d9d9728 100644 --- a/src/cargo/ops/cargo_add/dependency.rs +++ b/src/cargo/ops/cargo_add/dependency.rs @@ -1,4 +1,3 @@ -use std::collections::BTreeMap; use std::fmt::{Display, Formatter}; use std::path::{Path, PathBuf}; @@ -6,8 +5,6 @@ use indexmap::IndexSet; use toml_edit::KeyMut; use super::manifest::str_or_1_len_table; -use crate::core::FeatureMap; -use crate::core::FeatureValue; use crate::core::GitReference; use crate::core::SourceId; use crate::core::Summary; @@ -40,9 +37,6 @@ pub struct Dependency { /// If the dependency is renamed, this is the new name for the dependency /// as a string. None if it is not renamed. pub rename: Option, - - /// Features that are exposed by the dependency - pub available_features: BTreeMap>, } impl Dependency { @@ -57,7 +51,6 @@ impl Dependency { source: None, registry: None, rename: None, - available_features: Default::default(), } } @@ -85,28 +78,6 @@ impl Dependency { self } - /// Populate from cargo - pub fn set_available_features_from_cargo( - mut self, - available_features: &FeatureMap, - ) -> Dependency { - self.available_features = available_features - .iter() - .map(|(k, v)| { - ( - k.as_str().to_owned(), - v.iter() - .filter_map(|v| match v { - FeatureValue::Feature(f) => Some(f.as_str().to_owned()), - FeatureValue::Dep { .. } | FeatureValue::DepFeature { .. } => None, - }) - .collect::>(), - ) - }) - .collect(); - self - } - /// Set whether the dependency is optional #[allow(dead_code)] pub fn set_optional(mut self, opt: bool) -> Self { @@ -338,8 +309,6 @@ impl Dependency { None }; - let available_features = BTreeMap::default(); - let optional = table.get("optional").and_then(|v| v.as_bool()); let dep = Self { @@ -349,7 +318,6 @@ impl Dependency { registry, default_features, features, - available_features, optional, inherited_features: None, }; @@ -637,9 +605,7 @@ impl<'s> From<&'s Summary> for Dependency { } else { RegistrySource::new(other.version().to_string()).into() }; - Dependency::new(other.name().as_str()) - .set_source(source) - .set_available_features_from_cargo(other.features()) + Dependency::new(other.name().as_str()).set_source(source) } } diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index 57df8c6d8f6..cc54b7a8877 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -4,6 +4,7 @@ mod crate_spec; mod dependency; mod manifest; +use std::collections::BTreeMap; use std::collections::BTreeSet; use std::collections::VecDeque; use std::path::Path; @@ -18,10 +19,13 @@ use toml_edit::Item as TomlItem; use crate::core::dependency::DepKind; use crate::core::registry::PackageRegistry; +use crate::core::FeatureMap; +use crate::core::FeatureValue; use crate::core::Package; use crate::core::QueryKind; use crate::core::Registry; use crate::core::Shell; +use crate::core::Summary; use crate::core::Workspace; use crate::CargoResult; use crate::Config; @@ -200,7 +204,7 @@ fn resolve_dependency( section: &DepTable, config: &Config, registry: &mut PackageRegistry<'_>, -) -> CargoResult { +) -> CargoResult { let crate_spec = arg .crate_spec .as_deref() @@ -353,7 +357,7 @@ fn resolve_dependency( MaybeWorkspace::Other(query) => query, }; - dependency = populate_available_features(dependency, &query, registry)?; + let dependency = populate_available_features(dependency, &query, registry)?; Ok(dependency) } @@ -596,12 +600,68 @@ fn populate_dependency(mut dependency: Dependency, arg: &DepOp) -> Dependency { dependency } +pub struct DependencyEx { + dep: Dependency, + available_features: BTreeMap>, +} + +impl DependencyEx { + fn new(dep: Dependency) -> Self { + Self { + dep, + available_features: Default::default(), + } + } + + fn set_available_features(&mut self, available_features: &FeatureMap) { + self.available_features = available_features + .iter() + .map(|(k, v)| { + ( + k.as_str().to_owned(), + v.iter() + .filter_map(|v| match v { + FeatureValue::Feature(f) => Some(f.as_str().to_owned()), + FeatureValue::Dep { .. } | FeatureValue::DepFeature { .. } => None, + }) + .collect::>(), + ) + }) + .collect(); + } +} + +impl<'s> From<&'s Summary> for DependencyEx { + fn from(other: &'s Summary) -> Self { + let dep = Dependency::from(other); + let mut dep = Self::new(dep); + dep.set_available_features(other.features()); + dep + } +} + +impl std::fmt::Display for DependencyEx { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.dep.fmt(f) + } +} + +impl std::ops::Deref for DependencyEx { + type Target = Dependency; + + fn deref(&self) -> &Self::Target { + &self.dep + } +} + /// Lookup available features fn populate_available_features( - mut dependency: Dependency, + dependency: Dependency, query: &crate::core::dependency::Dependency, registry: &mut PackageRegistry<'_>, -) -> CargoResult { +) -> CargoResult { + let mut dependency = DependencyEx::new(dependency); + if !dependency.available_features.is_empty() { return Ok(dependency); } @@ -627,12 +687,12 @@ fn populate_available_features( .ok_or_else(|| { anyhow::format_err!("the crate `{dependency}` could not be found in registry index.") })?; - dependency = dependency.set_available_features_from_cargo(lowest_common_denominator.features()); + dependency.set_available_features(lowest_common_denominator.features()); Ok(dependency) } -fn print_msg(shell: &mut Shell, dep: &Dependency, section: &[String]) -> CargoResult<()> { +fn print_msg(shell: &mut Shell, dep: &DependencyEx, section: &[String]) -> CargoResult<()> { use std::fmt::Write; if matches!(shell.verbosity(), crate::core::shell::Verbosity::Quiet) { From 391281b6d7863f38ccce5b3bb3aeb7e77be7f1d4 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 12 Sep 2022 09:26:14 -0500 Subject: [PATCH 05/11] refactor(add): Make it easier to add more summary info --- src/cargo/ops/cargo_add/mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index cc54b7a8877..08695525be1 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -19,7 +19,6 @@ use toml_edit::Item as TomlItem; use crate::core::dependency::DepKind; use crate::core::registry::PackageRegistry; -use crate::core::FeatureMap; use crate::core::FeatureValue; use crate::core::Package; use crate::core::QueryKind; @@ -613,8 +612,9 @@ impl DependencyEx { } } - fn set_available_features(&mut self, available_features: &FeatureMap) { - self.available_features = available_features + fn apply_summary(&mut self, summary: &Summary) { + self.available_features = summary + .features() .iter() .map(|(k, v)| { ( @@ -635,7 +635,7 @@ impl<'s> From<&'s Summary> for DependencyEx { fn from(other: &'s Summary) -> Self { let dep = Dependency::from(other); let mut dep = Self::new(dep); - dep.set_available_features(other.features()); + dep.apply_summary(other); dep } } @@ -687,7 +687,7 @@ fn populate_available_features( .ok_or_else(|| { anyhow::format_err!("the crate `{dependency}` could not be found in registry index.") })?; - dependency.set_available_features(lowest_common_denominator.features()); + dependency.apply_summary(&lowest_common_denominator); Ok(dependency) } From 73906aeaf8b342f38fd7b6b7a40da028f55c3d15 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 12 Sep 2022 09:37:05 -0500 Subject: [PATCH 06/11] fix(add): Clarify which version the features are added for This gives a hint to users that we might not be showing the feature list for the latest version but the earliest version. Also when using a workspace dependency or re-using an existing dependency, this is a good reminder of what the version requirement is that was selected. However, when the user or add (the common case) selected a full precision requirement, this is redundant. I'm also mixed on whether the meta version should show up. Fixes #11073 --- src/cargo/ops/cargo_add/mod.rs | 13 ++++++++++++- .../add_normalized_name_external/stderr.log | 4 ++-- .../build_prefer_existing_version/stderr.log | 2 +- .../detect_workspace_inherit_features/stderr.log | 2 +- .../dev_prefer_existing_version/stderr.log | 2 +- tests/testsuite/cargo_add/features/stderr.log | 2 +- tests/testsuite/cargo_add/features_empty/stderr.log | 2 +- .../features_multiple_occurrences/stderr.log | 2 +- .../cargo_add/features_preserve/stderr.log | 2 +- .../cargo_add/features_spaced_values/stderr.log | 2 +- .../testsuite/cargo_add/features_unknown/stderr.log | 2 +- tests/testsuite/cargo_add/list_features/stderr.log | 2 +- .../cargo_add/list_features_path/stderr.log | 2 +- .../list_features_path_no_default/stderr.log | 2 +- .../cargo_add/merge_activated_features/stderr.log | 2 +- .../cargo_add/overwrite_features/stderr.log | 2 +- .../overwrite_inherit_features_noop/stderr.log | 2 +- .../cargo_add/overwrite_inline_features/stderr.log | 2 +- .../cargo_add/overwrite_name_dev_noop/stderr.log | 2 +- .../cargo_add/overwrite_name_noop/stderr.log | 2 +- .../overwrite_optional_with_no_optional/stderr.log | 2 +- .../cargo_add/overwrite_path_noop/stderr.log | 2 +- .../overwrite_preserves_inline_table/stderr.log | 2 +- .../overwrite_workspace_dep_features/stderr.log | 2 +- tests/testsuite/cargo_add/require_weak/stderr.log | 2 +- .../cargo_add/unknown_inherited_feature/stderr.log | 2 +- 26 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index 08695525be1..7012e3bcb17 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -601,6 +601,7 @@ fn populate_dependency(mut dependency: Dependency, arg: &DepOp) -> Dependency { pub struct DependencyEx { dep: Dependency, + available_version: Option, available_features: BTreeMap>, } @@ -608,11 +609,13 @@ impl DependencyEx { fn new(dep: Dependency) -> Self { Self { dep, + available_version: None, available_features: Default::default(), } } fn apply_summary(&mut self, summary: &Summary) { + self.available_version = Some(summary.version().clone()); self.available_features = summary .features() .iter() @@ -765,7 +768,15 @@ fn print_msg(shell: &mut Shell, dep: &DependencyEx, section: &[String]) -> Cargo deactivated.sort(); if !activated.is_empty() || !deactivated.is_empty() { let prefix = format!("{:>13}", " "); - shell.write_stderr(format_args!("{}Features:\n", prefix), &ColorSpec::new())?; + let suffix = if let Some(version) = &dep.available_version { + format!(" as of v{}", version) + } else { + "".to_owned() + }; + shell.write_stderr( + format_args!("{}Features{}:\n", prefix, suffix), + &ColorSpec::new(), + )?; for feat in activated { shell.write_stderr(&prefix, &ColorSpec::new())?; shell.write_stderr('+', &ColorSpec::new().set_bold(true).set_fg(Some(Green)))?; diff --git a/tests/testsuite/cargo_add/add_normalized_name_external/stderr.log b/tests/testsuite/cargo_add/add_normalized_name_external/stderr.log index c7d45114329..9f7fc219e14 100644 --- a/tests/testsuite/cargo_add/add_normalized_name_external/stderr.log +++ b/tests/testsuite/cargo_add/add_normalized_name_external/stderr.log @@ -2,7 +2,7 @@ warning: translating `linked_hash_map` to `linked-hash-map` warning: translating `Inflector` to `inflector` Adding linked-hash-map v0.5.4 to dependencies. - Features: + Features as of v0.5.4: - clippy - heapsize - heapsize_impl @@ -11,7 +11,7 @@ warning: translating `Inflector` to `inflector` - serde_impl - serde_test Adding inflector v0.11.4 to dependencies. - Features: + Features as of v0.11.4: + heavyweight + lazy_static + regex diff --git a/tests/testsuite/cargo_add/build_prefer_existing_version/stderr.log b/tests/testsuite/cargo_add/build_prefer_existing_version/stderr.log index 554aa2ef31d..ba6d8507075 100644 --- a/tests/testsuite/cargo_add/build_prefer_existing_version/stderr.log +++ b/tests/testsuite/cargo_add/build_prefer_existing_version/stderr.log @@ -1,4 +1,4 @@ Adding cargo-list-test-fixture-dependency (local) to build-dependencies. - Features: + Features as of v0.0.0: - one - two diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_features/stderr.log b/tests/testsuite/cargo_add/detect_workspace_inherit_features/stderr.log index 86d9fb3b857..02dde7a3441 100644 --- a/tests/testsuite/cargo_add/detect_workspace_inherit_features/stderr.log +++ b/tests/testsuite/cargo_add/detect_workspace_inherit_features/stderr.log @@ -1,5 +1,5 @@ Adding foo (workspace) to dependencies. - Features: + Features as of v0.0.0: + default-base + default-merge-base + default-test-base diff --git a/tests/testsuite/cargo_add/dev_prefer_existing_version/stderr.log b/tests/testsuite/cargo_add/dev_prefer_existing_version/stderr.log index 06673543a9f..32f9a3e82f4 100644 --- a/tests/testsuite/cargo_add/dev_prefer_existing_version/stderr.log +++ b/tests/testsuite/cargo_add/dev_prefer_existing_version/stderr.log @@ -1,4 +1,4 @@ Adding cargo-list-test-fixture-dependency (local) to dev-dependencies. - Features: + Features as of v0.0.0: - one - two diff --git a/tests/testsuite/cargo_add/features/stderr.log b/tests/testsuite/cargo_add/features/stderr.log index 386f3db5acc..58cfb8eeb01 100644 --- a/tests/testsuite/cargo_add/features/stderr.log +++ b/tests/testsuite/cargo_add/features/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features: + Features as of v99999.0.0+my-package: + eyes - ears - mouth diff --git a/tests/testsuite/cargo_add/features_empty/stderr.log b/tests/testsuite/cargo_add/features_empty/stderr.log index 796b9601b4d..71380066c85 100644 --- a/tests/testsuite/cargo_add/features_empty/stderr.log +++ b/tests/testsuite/cargo_add/features_empty/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features: + Features as of v99999.0.0+my-package: - ears - eyes - mouth diff --git a/tests/testsuite/cargo_add/features_multiple_occurrences/stderr.log b/tests/testsuite/cargo_add/features_multiple_occurrences/stderr.log index 615459052e4..618a9397b94 100644 --- a/tests/testsuite/cargo_add/features_multiple_occurrences/stderr.log +++ b/tests/testsuite/cargo_add/features_multiple_occurrences/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features: + Features as of v99999.0.0+my-package: + eyes + nose - ears diff --git a/tests/testsuite/cargo_add/features_preserve/stderr.log b/tests/testsuite/cargo_add/features_preserve/stderr.log index 386f3db5acc..58cfb8eeb01 100644 --- a/tests/testsuite/cargo_add/features_preserve/stderr.log +++ b/tests/testsuite/cargo_add/features_preserve/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features: + Features as of v99999.0.0+my-package: + eyes - ears - mouth diff --git a/tests/testsuite/cargo_add/features_spaced_values/stderr.log b/tests/testsuite/cargo_add/features_spaced_values/stderr.log index 615459052e4..618a9397b94 100644 --- a/tests/testsuite/cargo_add/features_spaced_values/stderr.log +++ b/tests/testsuite/cargo_add/features_spaced_values/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features: + Features as of v99999.0.0+my-package: + eyes + nose - ears diff --git a/tests/testsuite/cargo_add/features_unknown/stderr.log b/tests/testsuite/cargo_add/features_unknown/stderr.log index 7f59af09a6e..0d1f3f69a1f 100644 --- a/tests/testsuite/cargo_add/features_unknown/stderr.log +++ b/tests/testsuite/cargo_add/features_unknown/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features: + Features as of v99999.0.0+my-package: + noze - ears - eyes diff --git a/tests/testsuite/cargo_add/list_features/stderr.log b/tests/testsuite/cargo_add/list_features/stderr.log index 796b9601b4d..71380066c85 100644 --- a/tests/testsuite/cargo_add/list_features/stderr.log +++ b/tests/testsuite/cargo_add/list_features/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features: + Features as of v99999.0.0+my-package: - ears - eyes - mouth diff --git a/tests/testsuite/cargo_add/list_features_path/stderr.log b/tests/testsuite/cargo_add/list_features_path/stderr.log index af6747fe836..60419c14862 100644 --- a/tests/testsuite/cargo_add/list_features_path/stderr.log +++ b/tests/testsuite/cargo_add/list_features_path/stderr.log @@ -1,5 +1,5 @@ Adding your-face (local) to dependencies. - Features: + Features as of v0.1.3: + mouth + nose - eyes diff --git a/tests/testsuite/cargo_add/list_features_path_no_default/stderr.log b/tests/testsuite/cargo_add/list_features_path_no_default/stderr.log index 7f47a220ec0..9f63857dcaa 100644 --- a/tests/testsuite/cargo_add/list_features_path_no_default/stderr.log +++ b/tests/testsuite/cargo_add/list_features_path_no_default/stderr.log @@ -1,5 +1,5 @@ Adding your-face (local) to dependencies. - Features: + Features as of v0.1.3: - eyes - mouth - nose diff --git a/tests/testsuite/cargo_add/merge_activated_features/stderr.log b/tests/testsuite/cargo_add/merge_activated_features/stderr.log index 86d9fb3b857..02dde7a3441 100644 --- a/tests/testsuite/cargo_add/merge_activated_features/stderr.log +++ b/tests/testsuite/cargo_add/merge_activated_features/stderr.log @@ -1,5 +1,5 @@ Adding foo (workspace) to dependencies. - Features: + Features as of v0.0.0: + default-base + default-merge-base + default-test-base diff --git a/tests/testsuite/cargo_add/overwrite_features/stderr.log b/tests/testsuite/cargo_add/overwrite_features/stderr.log index 615459052e4..618a9397b94 100644 --- a/tests/testsuite/cargo_add/overwrite_features/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_features/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features: + Features as of v99999.0.0+my-package: + eyes + nose - ears diff --git a/tests/testsuite/cargo_add/overwrite_inherit_features_noop/stderr.log b/tests/testsuite/cargo_add/overwrite_inherit_features_noop/stderr.log index 0eee2e6bd46..3c7133bbc52 100644 --- a/tests/testsuite/cargo_add/overwrite_inherit_features_noop/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_inherit_features_noop/stderr.log @@ -1,3 +1,3 @@ Adding foo (workspace) to dependencies. - Features: + Features as of v0.0.0: + test diff --git a/tests/testsuite/cargo_add/overwrite_inline_features/stderr.log b/tests/testsuite/cargo_add/overwrite_inline_features/stderr.log index a686cba55d5..b0f656df90e 100644 --- a/tests/testsuite/cargo_add/overwrite_inline_features/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_inline_features/stderr.log @@ -1,7 +1,7 @@ Updating `dummy-registry` index Adding unrelateed-crate v99999.0.0 to dependencies. Adding your-face v99999.0.0 to dependencies. - Features: + Features as of v99999.0.0+my-package: + ears + eyes + mouth diff --git a/tests/testsuite/cargo_add/overwrite_name_dev_noop/stderr.log b/tests/testsuite/cargo_add/overwrite_name_dev_noop/stderr.log index 2fe3f6a2906..a9d0c65d7ad 100644 --- a/tests/testsuite/cargo_add/overwrite_name_dev_noop/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_name_dev_noop/stderr.log @@ -1,4 +1,4 @@ Adding your-face (local) to dev-dependencies. - Features: + Features as of v0.0.0: + mouth + nose diff --git a/tests/testsuite/cargo_add/overwrite_name_noop/stderr.log b/tests/testsuite/cargo_add/overwrite_name_noop/stderr.log index 2f0b90de0d2..fdd8d51a170 100644 --- a/tests/testsuite/cargo_add/overwrite_name_noop/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_name_noop/stderr.log @@ -1,4 +1,4 @@ Adding your-face (local) to optional dependencies. - Features: + Features as of v0.0.0: + mouth + nose diff --git a/tests/testsuite/cargo_add/overwrite_optional_with_no_optional/stderr.log b/tests/testsuite/cargo_add/overwrite_optional_with_no_optional/stderr.log index 5fe113e86c7..5ef0f6090e2 100644 --- a/tests/testsuite/cargo_add/overwrite_optional_with_no_optional/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_optional_with_no_optional/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features: + Features as of v99999.0.0+my-package: - ears - eyes - mouth diff --git a/tests/testsuite/cargo_add/overwrite_path_noop/stderr.log b/tests/testsuite/cargo_add/overwrite_path_noop/stderr.log index 2f0b90de0d2..fdd8d51a170 100644 --- a/tests/testsuite/cargo_add/overwrite_path_noop/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_path_noop/stderr.log @@ -1,4 +1,4 @@ Adding your-face (local) to optional dependencies. - Features: + Features as of v0.0.0: + mouth + nose diff --git a/tests/testsuite/cargo_add/overwrite_preserves_inline_table/stderr.log b/tests/testsuite/cargo_add/overwrite_preserves_inline_table/stderr.log index 615459052e4..618a9397b94 100644 --- a/tests/testsuite/cargo_add/overwrite_preserves_inline_table/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_preserves_inline_table/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features: + Features as of v99999.0.0+my-package: + eyes + nose - ears diff --git a/tests/testsuite/cargo_add/overwrite_workspace_dep_features/stderr.log b/tests/testsuite/cargo_add/overwrite_workspace_dep_features/stderr.log index 18ed7c2d8fa..e5579fda621 100644 --- a/tests/testsuite/cargo_add/overwrite_workspace_dep_features/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_workspace_dep_features/stderr.log @@ -1,5 +1,5 @@ Adding foo (local) to dependencies. - Features: + Features as of v0.0.0: + default-base + default-merge-base + default-test-base diff --git a/tests/testsuite/cargo_add/require_weak/stderr.log b/tests/testsuite/cargo_add/require_weak/stderr.log index 796b9601b4d..71380066c85 100644 --- a/tests/testsuite/cargo_add/require_weak/stderr.log +++ b/tests/testsuite/cargo_add/require_weak/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features: + Features as of v99999.0.0+my-package: - ears - eyes - mouth diff --git a/tests/testsuite/cargo_add/unknown_inherited_feature/stderr.log b/tests/testsuite/cargo_add/unknown_inherited_feature/stderr.log index 693e8872105..acc7bd2bbd0 100644 --- a/tests/testsuite/cargo_add/unknown_inherited_feature/stderr.log +++ b/tests/testsuite/cargo_add/unknown_inherited_feature/stderr.log @@ -1,5 +1,5 @@ Adding foo (workspace) to dependencies. - Features: + Features as of v0.0.0: + default-base + default-merge-base + default-test-base From 7ce2b7d42a156f90b41fa6c68fc4dd5f9cb2b226 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 12 Sep 2022 09:46:22 -0500 Subject: [PATCH 07/11] fix(add): Limit 'Features as of vX.Y.Z' to when relevant This will only show the messaeg if we didn't already show a version req with full precision specified ... mostly. We'll also skip it if its a local or git dependency but we never show the version in those cases because it doesn't matter. The `precise_version` logic came from cargo-upgrade. --- src/cargo/ops/cargo_add/mod.rs | 43 ++++++++++++++++++- .../add_normalized_name_external/stderr.log | 4 +- .../build_prefer_existing_version/stderr.log | 2 +- .../cargo_add/list_features_path/stderr.log | 2 +- .../list_features_path_no_default/stderr.log | 2 +- .../overwrite_name_dev_noop/stderr.log | 2 +- .../cargo_add/overwrite_name_noop/stderr.log | 2 +- .../cargo_add/overwrite_path_noop/stderr.log | 2 +- .../stderr.log | 2 +- 9 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index 7012e3bcb17..8357601b6bb 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -769,7 +769,16 @@ fn print_msg(shell: &mut Shell, dep: &DependencyEx, section: &[String]) -> Cargo if !activated.is_empty() || !deactivated.is_empty() { let prefix = format!("{:>13}", " "); let suffix = if let Some(version) = &dep.available_version { - format!(" as of v{}", version) + let version = version.to_string(); + let version_req = dep + .version() + .and_then(|v| semver::VersionReq::parse(v).ok()) + .and_then(|v| precise_version(&v)); + if version_req.as_deref() != Some(version.as_str()) { + format!(" as of v{}", version) + } else { + "".to_owned() + } } else { "".to_owned() }; @@ -832,3 +841,35 @@ fn find_workspace_dep(toml_key: &str, root_manifest: &Path) -> CargoResult Option { + version_req + .comparators + .iter() + .filter(|c| { + matches!( + c.op, + // Only ops we can determine a precise version from + semver::Op::Exact + | semver::Op::GreaterEq + | semver::Op::LessEq + | semver::Op::Tilde + | semver::Op::Caret + | semver::Op::Wildcard + ) + }) + .filter_map(|c| { + // Only do it when full precision is specified + c.minor.and_then(|minor| { + c.patch.map(|patch| semver::Version { + major: c.major, + minor, + patch, + pre: c.pre.clone(), + build: Default::default(), + }) + }) + }) + .max() + .map(|v| v.to_string()) +} diff --git a/tests/testsuite/cargo_add/add_normalized_name_external/stderr.log b/tests/testsuite/cargo_add/add_normalized_name_external/stderr.log index 9f7fc219e14..c7d45114329 100644 --- a/tests/testsuite/cargo_add/add_normalized_name_external/stderr.log +++ b/tests/testsuite/cargo_add/add_normalized_name_external/stderr.log @@ -2,7 +2,7 @@ warning: translating `linked_hash_map` to `linked-hash-map` warning: translating `Inflector` to `inflector` Adding linked-hash-map v0.5.4 to dependencies. - Features as of v0.5.4: + Features: - clippy - heapsize - heapsize_impl @@ -11,7 +11,7 @@ warning: translating `Inflector` to `inflector` - serde_impl - serde_test Adding inflector v0.11.4 to dependencies. - Features as of v0.11.4: + Features: + heavyweight + lazy_static + regex diff --git a/tests/testsuite/cargo_add/build_prefer_existing_version/stderr.log b/tests/testsuite/cargo_add/build_prefer_existing_version/stderr.log index ba6d8507075..554aa2ef31d 100644 --- a/tests/testsuite/cargo_add/build_prefer_existing_version/stderr.log +++ b/tests/testsuite/cargo_add/build_prefer_existing_version/stderr.log @@ -1,4 +1,4 @@ Adding cargo-list-test-fixture-dependency (local) to build-dependencies. - Features as of v0.0.0: + Features: - one - two diff --git a/tests/testsuite/cargo_add/list_features_path/stderr.log b/tests/testsuite/cargo_add/list_features_path/stderr.log index 60419c14862..af6747fe836 100644 --- a/tests/testsuite/cargo_add/list_features_path/stderr.log +++ b/tests/testsuite/cargo_add/list_features_path/stderr.log @@ -1,5 +1,5 @@ Adding your-face (local) to dependencies. - Features as of v0.1.3: + Features: + mouth + nose - eyes diff --git a/tests/testsuite/cargo_add/list_features_path_no_default/stderr.log b/tests/testsuite/cargo_add/list_features_path_no_default/stderr.log index 9f63857dcaa..7f47a220ec0 100644 --- a/tests/testsuite/cargo_add/list_features_path_no_default/stderr.log +++ b/tests/testsuite/cargo_add/list_features_path_no_default/stderr.log @@ -1,5 +1,5 @@ Adding your-face (local) to dependencies. - Features as of v0.1.3: + Features: - eyes - mouth - nose diff --git a/tests/testsuite/cargo_add/overwrite_name_dev_noop/stderr.log b/tests/testsuite/cargo_add/overwrite_name_dev_noop/stderr.log index a9d0c65d7ad..2fe3f6a2906 100644 --- a/tests/testsuite/cargo_add/overwrite_name_dev_noop/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_name_dev_noop/stderr.log @@ -1,4 +1,4 @@ Adding your-face (local) to dev-dependencies. - Features as of v0.0.0: + Features: + mouth + nose diff --git a/tests/testsuite/cargo_add/overwrite_name_noop/stderr.log b/tests/testsuite/cargo_add/overwrite_name_noop/stderr.log index fdd8d51a170..2f0b90de0d2 100644 --- a/tests/testsuite/cargo_add/overwrite_name_noop/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_name_noop/stderr.log @@ -1,4 +1,4 @@ Adding your-face (local) to optional dependencies. - Features as of v0.0.0: + Features: + mouth + nose diff --git a/tests/testsuite/cargo_add/overwrite_path_noop/stderr.log b/tests/testsuite/cargo_add/overwrite_path_noop/stderr.log index fdd8d51a170..2f0b90de0d2 100644 --- a/tests/testsuite/cargo_add/overwrite_path_noop/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_path_noop/stderr.log @@ -1,4 +1,4 @@ Adding your-face (local) to optional dependencies. - Features as of v0.0.0: + Features: + mouth + nose diff --git a/tests/testsuite/cargo_add/overwrite_workspace_dep_features/stderr.log b/tests/testsuite/cargo_add/overwrite_workspace_dep_features/stderr.log index e5579fda621..18ed7c2d8fa 100644 --- a/tests/testsuite/cargo_add/overwrite_workspace_dep_features/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_workspace_dep_features/stderr.log @@ -1,5 +1,5 @@ Adding foo (local) to dependencies. - Features as of v0.0.0: + Features: + default-base + default-merge-base + default-test-base From be257b8b25e7f41b849904596d5efafcfcc3bda9 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 12 Sep 2022 09:51:38 -0500 Subject: [PATCH 08/11] fix(add): Remove redundant version in build metadata cases --- src/cargo/ops/cargo_add/mod.rs | 2 ++ tests/testsuite/cargo_add/features/stderr.log | 2 +- tests/testsuite/cargo_add/features_empty/stderr.log | 2 +- .../cargo_add/features_multiple_occurrences/stderr.log | 2 +- tests/testsuite/cargo_add/features_preserve/stderr.log | 2 +- tests/testsuite/cargo_add/features_spaced_values/stderr.log | 2 +- tests/testsuite/cargo_add/features_unknown/stderr.log | 2 +- tests/testsuite/cargo_add/list_features/stderr.log | 2 +- tests/testsuite/cargo_add/overwrite_features/stderr.log | 2 +- tests/testsuite/cargo_add/overwrite_inline_features/stderr.log | 2 +- .../cargo_add/overwrite_optional_with_no_optional/stderr.log | 2 +- .../cargo_add/overwrite_preserves_inline_table/stderr.log | 2 +- tests/testsuite/cargo_add/require_weak/stderr.log | 2 +- 13 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index 8357601b6bb..72a8dfb35f2 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -769,6 +769,8 @@ fn print_msg(shell: &mut Shell, dep: &DependencyEx, section: &[String]) -> Cargo if !activated.is_empty() || !deactivated.is_empty() { let prefix = format!("{:>13}", " "); let suffix = if let Some(version) = &dep.available_version { + let mut version = version.clone(); + version.build = Default::default(); let version = version.to_string(); let version_req = dep .version() diff --git a/tests/testsuite/cargo_add/features/stderr.log b/tests/testsuite/cargo_add/features/stderr.log index 58cfb8eeb01..386f3db5acc 100644 --- a/tests/testsuite/cargo_add/features/stderr.log +++ b/tests/testsuite/cargo_add/features/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features as of v99999.0.0+my-package: + Features: + eyes - ears - mouth diff --git a/tests/testsuite/cargo_add/features_empty/stderr.log b/tests/testsuite/cargo_add/features_empty/stderr.log index 71380066c85..796b9601b4d 100644 --- a/tests/testsuite/cargo_add/features_empty/stderr.log +++ b/tests/testsuite/cargo_add/features_empty/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features as of v99999.0.0+my-package: + Features: - ears - eyes - mouth diff --git a/tests/testsuite/cargo_add/features_multiple_occurrences/stderr.log b/tests/testsuite/cargo_add/features_multiple_occurrences/stderr.log index 618a9397b94..615459052e4 100644 --- a/tests/testsuite/cargo_add/features_multiple_occurrences/stderr.log +++ b/tests/testsuite/cargo_add/features_multiple_occurrences/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features as of v99999.0.0+my-package: + Features: + eyes + nose - ears diff --git a/tests/testsuite/cargo_add/features_preserve/stderr.log b/tests/testsuite/cargo_add/features_preserve/stderr.log index 58cfb8eeb01..386f3db5acc 100644 --- a/tests/testsuite/cargo_add/features_preserve/stderr.log +++ b/tests/testsuite/cargo_add/features_preserve/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features as of v99999.0.0+my-package: + Features: + eyes - ears - mouth diff --git a/tests/testsuite/cargo_add/features_spaced_values/stderr.log b/tests/testsuite/cargo_add/features_spaced_values/stderr.log index 618a9397b94..615459052e4 100644 --- a/tests/testsuite/cargo_add/features_spaced_values/stderr.log +++ b/tests/testsuite/cargo_add/features_spaced_values/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features as of v99999.0.0+my-package: + Features: + eyes + nose - ears diff --git a/tests/testsuite/cargo_add/features_unknown/stderr.log b/tests/testsuite/cargo_add/features_unknown/stderr.log index 0d1f3f69a1f..7f59af09a6e 100644 --- a/tests/testsuite/cargo_add/features_unknown/stderr.log +++ b/tests/testsuite/cargo_add/features_unknown/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features as of v99999.0.0+my-package: + Features: + noze - ears - eyes diff --git a/tests/testsuite/cargo_add/list_features/stderr.log b/tests/testsuite/cargo_add/list_features/stderr.log index 71380066c85..796b9601b4d 100644 --- a/tests/testsuite/cargo_add/list_features/stderr.log +++ b/tests/testsuite/cargo_add/list_features/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features as of v99999.0.0+my-package: + Features: - ears - eyes - mouth diff --git a/tests/testsuite/cargo_add/overwrite_features/stderr.log b/tests/testsuite/cargo_add/overwrite_features/stderr.log index 618a9397b94..615459052e4 100644 --- a/tests/testsuite/cargo_add/overwrite_features/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_features/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features as of v99999.0.0+my-package: + Features: + eyes + nose - ears diff --git a/tests/testsuite/cargo_add/overwrite_inline_features/stderr.log b/tests/testsuite/cargo_add/overwrite_inline_features/stderr.log index b0f656df90e..a686cba55d5 100644 --- a/tests/testsuite/cargo_add/overwrite_inline_features/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_inline_features/stderr.log @@ -1,7 +1,7 @@ Updating `dummy-registry` index Adding unrelateed-crate v99999.0.0 to dependencies. Adding your-face v99999.0.0 to dependencies. - Features as of v99999.0.0+my-package: + Features: + ears + eyes + mouth diff --git a/tests/testsuite/cargo_add/overwrite_optional_with_no_optional/stderr.log b/tests/testsuite/cargo_add/overwrite_optional_with_no_optional/stderr.log index 5ef0f6090e2..5fe113e86c7 100644 --- a/tests/testsuite/cargo_add/overwrite_optional_with_no_optional/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_optional_with_no_optional/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features as of v99999.0.0+my-package: + Features: - ears - eyes - mouth diff --git a/tests/testsuite/cargo_add/overwrite_preserves_inline_table/stderr.log b/tests/testsuite/cargo_add/overwrite_preserves_inline_table/stderr.log index 618a9397b94..615459052e4 100644 --- a/tests/testsuite/cargo_add/overwrite_preserves_inline_table/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_preserves_inline_table/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features as of v99999.0.0+my-package: + Features: + eyes + nose - ears diff --git a/tests/testsuite/cargo_add/require_weak/stderr.log b/tests/testsuite/cargo_add/require_weak/stderr.log index 71380066c85..796b9601b4d 100644 --- a/tests/testsuite/cargo_add/require_weak/stderr.log +++ b/tests/testsuite/cargo_add/require_weak/stderr.log @@ -1,6 +1,6 @@ Updating `dummy-registry` index Adding your-face v99999.0.0 to dependencies. - Features as of v99999.0.0+my-package: + Features: - ears - eyes - mouth From f1548f6c3da27c112a51cf0f2941e13cb23c035c Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 13 Sep 2022 09:52:37 -0500 Subject: [PATCH 09/11] refactor(add): Clarify DepednencyEx's name --- src/cargo/ops/cargo_add/mod.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index 72a8dfb35f2..4cdc5c621c3 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -203,7 +203,7 @@ fn resolve_dependency( section: &DepTable, config: &Config, registry: &mut PackageRegistry<'_>, -) -> CargoResult { +) -> CargoResult { let crate_spec = arg .crate_spec .as_deref() @@ -599,13 +599,13 @@ fn populate_dependency(mut dependency: Dependency, arg: &DepOp) -> Dependency { dependency } -pub struct DependencyEx { +pub struct DependencyUI { dep: Dependency, available_version: Option, available_features: BTreeMap>, } -impl DependencyEx { +impl DependencyUI { fn new(dep: Dependency) -> Self { Self { dep, @@ -634,7 +634,7 @@ impl DependencyEx { } } -impl<'s> From<&'s Summary> for DependencyEx { +impl<'s> From<&'s Summary> for DependencyUI { fn from(other: &'s Summary) -> Self { let dep = Dependency::from(other); let mut dep = Self::new(dep); @@ -643,13 +643,13 @@ impl<'s> From<&'s Summary> for DependencyEx { } } -impl std::fmt::Display for DependencyEx { +impl std::fmt::Display for DependencyUI { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.dep.fmt(f) } } -impl std::ops::Deref for DependencyEx { +impl std::ops::Deref for DependencyUI { type Target = Dependency; fn deref(&self) -> &Self::Target { @@ -662,8 +662,8 @@ fn populate_available_features( dependency: Dependency, query: &crate::core::dependency::Dependency, registry: &mut PackageRegistry<'_>, -) -> CargoResult { - let mut dependency = DependencyEx::new(dependency); +) -> CargoResult { + let mut dependency = DependencyUI::new(dependency); if !dependency.available_features.is_empty() { return Ok(dependency); @@ -695,7 +695,7 @@ fn populate_available_features( Ok(dependency) } -fn print_msg(shell: &mut Shell, dep: &DependencyEx, section: &[String]) -> CargoResult<()> { +fn print_msg(shell: &mut Shell, dep: &DependencyUI, section: &[String]) -> CargoResult<()> { use std::fmt::Write; if matches!(shell.verbosity(), crate::core::shell::Verbosity::Quiet) { From 1a20200e09ef7ac5a394509ac02b0167ea11ab12 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 13 Sep 2022 11:17:29 -0500 Subject: [PATCH 10/11] chore: Document new add implementation --- src/cargo/ops/cargo_add/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index 4cdc5c621c3..b6491f59dbb 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -599,9 +599,14 @@ fn populate_dependency(mut dependency: Dependency, arg: &DepOp) -> Dependency { dependency } +/// Track presentation-layer information with the editable representation of a `[dependencies]` +/// entry (Dependency) pub struct DependencyUI { + /// Editable representation of a `[depednencies]` entry dep: Dependency, + /// The version of the crate that we pulled `available_features` from available_version: Option, + /// The widest set of features compatible with `Dependency`s version requirement available_features: BTreeMap>, } @@ -772,6 +777,8 @@ fn print_msg(shell: &mut Shell, dep: &DependencyUI, section: &[String]) -> Cargo let mut version = version.clone(); version.build = Default::default(); let version = version.to_string(); + // Avoid displaying the version if it will visually look like the version req that we + // showed earlier let version_req = dep .version() .and_then(|v| semver::VersionReq::parse(v).ok()) @@ -844,6 +851,8 @@ fn find_workspace_dep(toml_key: &str, root_manifest: &Path) -> CargoResult Option { version_req .comparators From 5a6cfc9a56f822dc982b6c5c3c961d9ac3ba3f82 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 13 Sep 2022 11:18:00 -0500 Subject: [PATCH 11/11] style(add): Use implicit argument capture --- src/cargo/ops/cargo_add/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index b6491f59dbb..48963284e0e 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -784,7 +784,7 @@ fn print_msg(shell: &mut Shell, dep: &DependencyUI, section: &[String]) -> Cargo .and_then(|v| semver::VersionReq::parse(v).ok()) .and_then(|v| precise_version(&v)); if version_req.as_deref() != Some(version.as_str()) { - format!(" as of v{}", version) + format!(" as of v{version}") } else { "".to_owned() }