Skip to content

Commit 2e1f9f4

Browse files
committed
refactor(update): Consolidate change rendering
1 parent 31acf3f commit 2e1f9f4

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/cargo/ops/cargo_update.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ fn print_lockfile_generation(
524524
assert_eq!(change.kind, PackageChangeKind::Added);
525525
ws.gctx().shell().status_with_color(
526526
change.kind.status(),
527-
format!("{package_id}{note}"),
527+
format!("{change}{note}"),
528528
&change.kind.style(),
529529
)?;
530530
}
@@ -562,19 +562,12 @@ fn print_lockfile_sync(
562562
};
563563

564564
let package_id = change.package_id;
565-
if let Some(previous_id) = change.previous_id {
565+
if change.previous_id.is_some() {
566566
let required_rust_version = report_required_rust_version(ws, resolve, package_id);
567567
let latest = report_latest(&possibilities, package_id);
568568
let note = required_rust_version.or(latest).unwrap_or_default();
569569

570-
let msg = if package_id.source_id().is_git() {
571-
format!(
572-
"{previous_id} -> #{}{note}",
573-
&package_id.source_id().precise_git_fragment().unwrap()[..8],
574-
)
575-
} else {
576-
format!("{previous_id} -> v{}{note}", package_id.version())
577-
};
570+
let msg = format!("{change}{note}");
578571

579572
if change.kind == PackageChangeKind::Downgraded {
580573
ws.gctx().shell().status_with_color(
@@ -597,7 +590,7 @@ fn print_lockfile_sync(
597590

598591
ws.gctx().shell().status_with_color(
599592
change.kind.status(),
600-
format!("{package_id}{note}"),
593+
format!("{change}{note}"),
601594
&change.kind.style(),
602595
)?;
603596
}
@@ -636,19 +629,12 @@ fn print_lockfile_updates(
636629
};
637630

638631
let package_id = change.package_id;
639-
if let Some(previous_id) = change.previous_id {
632+
if change.previous_id.is_some() {
640633
let required_rust_version = report_required_rust_version(ws, resolve, package_id);
641634
let latest = report_latest(&possibilities, package_id);
642635
let note = required_rust_version.or(latest).unwrap_or_default();
643636

644-
let msg = if package_id.source_id().is_git() {
645-
format!(
646-
"{previous_id} -> #{}{note}",
647-
&package_id.source_id().precise_git_fragment().unwrap()[..8],
648-
)
649-
} else {
650-
format!("{previous_id} -> v{}{note}", package_id.version())
651-
};
637+
let msg = format!("{change}{note}");
652638

653639
if change.kind == PackageChangeKind::Downgraded {
654640
ws.gctx().shell().status_with_color(
@@ -667,7 +653,7 @@ fn print_lockfile_updates(
667653
if change.kind == PackageChangeKind::Removed {
668654
ws.gctx().shell().status_with_color(
669655
change.kind.status(),
670-
format!("{package_id}"),
656+
format!("{change}"),
671657
&change.kind.style(),
672658
)?;
673659
} else if change.kind == PackageChangeKind::Added {
@@ -677,7 +663,7 @@ fn print_lockfile_updates(
677663

678664
ws.gctx().shell().status_with_color(
679665
change.kind.status(),
680-
format!("{package_id}{note}"),
666+
format!("{change}{note}"),
681667
&change.kind.style(),
682668
)?;
683669
}
@@ -694,7 +680,7 @@ fn print_lockfile_updates(
694680
if ws.gctx().shell().verbosity() == Verbosity::Verbose {
695681
ws.gctx().shell().status_with_color(
696682
change.kind.status(),
697-
format!("{package_id}{note}"),
683+
format!("{change}{note}"),
698684
&change.kind.style(),
699685
)?;
700686
}
@@ -911,6 +897,25 @@ impl PackageChange {
911897
}
912898
}
913899

900+
impl std::fmt::Display for PackageChange {
901+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
902+
let package_id = self.package_id;
903+
if let Some(previous_id) = self.previous_id {
904+
if package_id.source_id().is_git() {
905+
write!(
906+
f,
907+
"{previous_id} -> #{}",
908+
&package_id.source_id().precise_git_fragment().unwrap()[..8],
909+
)
910+
} else {
911+
write!(f, "{previous_id} -> v{}", package_id.version())
912+
}
913+
} else {
914+
write!(f, "{package_id}")
915+
}
916+
}
917+
}
918+
914919
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
915920
enum PackageChangeKind {
916921
Added,

0 commit comments

Comments
 (0)