Skip to content

Commit

Permalink
Extract feature suffix formatting to function
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelin01 committed Sep 19, 2023
1 parent e4de526 commit c25bee1
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,24 +946,7 @@ fn print_dep_table_msg(shell: &mut Shell, dep: &DependencyUI) -> CargoResult<()>
let (activated, deactivated) = dep.features();
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();
// 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())
.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()
};
let suffix = format_feature_version_suffix(&dep);

shell.write_stderr(format_args!("{}Features{}:\n", prefix, suffix), &style::NOP)?;

Expand Down Expand Up @@ -1009,6 +992,27 @@ fn print_dep_table_msg(shell: &mut Shell, dep: &DependencyUI) -> CargoResult<()>
Ok(())
}

fn format_feature_version_suffix(dep: &DependencyUI) -> String {
if let Some(version) = &dep.available_version {
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())
.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()
}
}

// Based on Iterator::is_sorted from nightly std; remove in favor of that when stabilized.
fn is_sorted(mut it: impl Iterator<Item = impl PartialOrd>) -> bool {
let Some(mut last) = it.next() else {
Expand Down

0 comments on commit c25bee1

Please sign in to comment.