Skip to content

Commit

Permalink
refactor(update): Consolidate change rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 20, 2024
1 parent 1a2844e commit 6819658
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/cargo/ops/cargo_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ fn print_lockfile_generation(
assert_eq!(change.kind, PackageChangeKind::Added);
ws.gctx().shell().status_with_color(
change.kind.status(),
format!("{package_id}{note}"),
format!("{change}{note}"),
&change.kind.style(),
)?;
}
Expand Down Expand Up @@ -562,19 +562,12 @@ fn print_lockfile_sync(
};

let package_id = change.package_id;
if let Some(previous_id) = change.previous_id {
if change.previous_id.is_some() {
let required_rust_version = report_required_rust_version(ws, resolve, package_id);
let latest = report_latest(&possibilities, package_id);
let note = required_rust_version.or(latest).unwrap_or_default();

let msg = if package_id.source_id().is_git() {
format!(
"{previous_id} -> #{}{note}",
&package_id.source_id().precise_git_fragment().unwrap()[..8],
)
} else {
format!("{previous_id} -> v{}{note}", package_id.version())
};
let msg = format!("{change}{note}");

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

ws.gctx().shell().status_with_color(
change.kind.status(),
format!("{package_id}{note}"),
format!("{change}{note}"),
&change.kind.style(),
)?;
}
Expand Down Expand Up @@ -636,19 +629,12 @@ fn print_lockfile_updates(
};

let package_id = change.package_id;
if let Some(previous_id) = change.previous_id {
if change.previous_id.is_some() {
let required_rust_version = report_required_rust_version(ws, resolve, package_id);
let latest = report_latest(&possibilities, package_id);
let note = required_rust_version.or(latest).unwrap_or_default();

let msg = if package_id.source_id().is_git() {
format!(
"{previous_id} -> #{}{note}",
&package_id.source_id().precise_git_fragment().unwrap()[..8],
)
} else {
format!("{previous_id} -> v{}{note}", package_id.version())
};
let msg = format!("{change}{note}");

if change.kind == PackageChangeKind::Downgraded {
ws.gctx().shell().status_with_color(
Expand All @@ -667,7 +653,7 @@ fn print_lockfile_updates(
if change.kind == PackageChangeKind::Removed {
ws.gctx().shell().status_with_color(
change.kind.status(),
format!("{package_id}"),
format!("{change}"),
&change.kind.style(),
)?;
} else if change.kind == PackageChangeKind::Added {
Expand All @@ -677,7 +663,7 @@ fn print_lockfile_updates(

ws.gctx().shell().status_with_color(
change.kind.status(),
format!("{package_id}{note}"),
format!("{change}{note}"),
&change.kind.style(),
)?;
}
Expand All @@ -694,7 +680,7 @@ fn print_lockfile_updates(
if ws.gctx().shell().verbosity() == Verbosity::Verbose {
ws.gctx().shell().status_with_color(
change.kind.status(),
format!("{package_id}{note}"),
format!("{change}{note}"),
&change.kind.style(),
)?;
}
Expand Down Expand Up @@ -911,6 +897,25 @@ impl PackageChange {
}
}

impl std::fmt::Display for PackageChange {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let package_id = self.package_id;
if let Some(previous_id) = self.previous_id {
if package_id.source_id().is_git() {
write!(
f,
"{previous_id} -> #{}",
&package_id.source_id().precise_git_fragment().unwrap()[..8],
)
} else {
write!(f, "{previous_id} -> v{}", package_id.version())
}
} else {
write!(f, "{package_id}")
}
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum PackageChangeKind {
Added,
Expand Down

0 comments on commit 6819658

Please sign in to comment.