Skip to content

Commit c25bee1

Browse files
author
Angelo Ross
committed
Extract feature suffix formatting to function
1 parent e4de526 commit c25bee1

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/cargo/ops/cargo_add/mod.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -946,24 +946,7 @@ fn print_dep_table_msg(shell: &mut Shell, dep: &DependencyUI) -> CargoResult<()>
946946
let (activated, deactivated) = dep.features();
947947
if !activated.is_empty() || !deactivated.is_empty() {
948948
let prefix = format!("{:>13}", " ");
949-
let suffix = if let Some(version) = &dep.available_version {
950-
let mut version = version.clone();
951-
version.build = Default::default();
952-
let version = version.to_string();
953-
// Avoid displaying the version if it will visually look like the version req that we
954-
// showed earlier
955-
let version_req = dep
956-
.version()
957-
.and_then(|v| semver::VersionReq::parse(v).ok())
958-
.and_then(|v| precise_version(&v));
959-
if version_req.as_deref() != Some(version.as_str()) {
960-
format!(" as of v{version}")
961-
} else {
962-
"".to_owned()
963-
}
964-
} else {
965-
"".to_owned()
966-
};
949+
let suffix = format_feature_version_suffix(&dep);
967950

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

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

995+
fn format_feature_version_suffix(dep: &DependencyUI) -> String {
996+
if let Some(version) = &dep.available_version {
997+
let mut version = version.clone();
998+
version.build = Default::default();
999+
let version = version.to_string();
1000+
// Avoid displaying the version if it will visually look like the version req that we
1001+
// showed earlier
1002+
let version_req = dep
1003+
.version()
1004+
.and_then(|v| semver::VersionReq::parse(v).ok())
1005+
.and_then(|v| precise_version(&v));
1006+
if version_req.as_deref() != Some(version.as_str()) {
1007+
format!(" as of v{version}")
1008+
} else {
1009+
"".to_owned()
1010+
}
1011+
} else {
1012+
"".to_owned()
1013+
}
1014+
}
1015+
10121016
// Based on Iterator::is_sorted from nightly std; remove in favor of that when stabilized.
10131017
fn is_sorted(mut it: impl Iterator<Item = impl PartialOrd>) -> bool {
10141018
let Some(mut last) = it.next() else {

0 commit comments

Comments
 (0)