Skip to content

Commit 030da59

Browse files
committed
refactor(lockfile): Simplify printing code
1 parent 4000d03 commit 030da59

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/cargo/ops/cargo_generate_lockfile.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::util::cache_lock::CacheLockMode;
1010
use crate::util::context::GlobalContext;
1111
use crate::util::style;
1212
use crate::util::CargoResult;
13-
use anstyle::Style;
1413
use std::cmp::Ordering;
1514
use std::collections::{BTreeMap, HashSet};
1615
use tracing::debug;
@@ -154,7 +153,7 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
154153
true,
155154
)?;
156155

157-
print_lockfile_update(opts.gctx, &previous_resolve, &resolve, &mut registry)?;
156+
print_lockfile_updates(opts.gctx, &previous_resolve, &resolve, &mut registry)?;
158157
if opts.dry_run {
159158
opts.gctx
160159
.shell()
@@ -165,16 +164,14 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
165164
Ok(())
166165
}
167166

168-
fn print_lockfile_update(
167+
fn print_lockfile_updates(
169168
gctx: &GlobalContext,
170169
previous_resolve: &Resolve,
171170
resolve: &Resolve,
172171
registry: &mut PackageRegistry<'_>,
173172
) -> CargoResult<()> {
174-
// Summarize what is changing for the user.
175-
let print_change = |status: &str, msg: String, color: &Style| {
176-
gctx.shell().status_with_color(status, msg, color)
177-
};
173+
let mut shell = gctx.shell();
174+
178175
let mut unchanged_behind = 0;
179176
for diff in PackageDiff::diff(&previous_resolve, &resolve) {
180177
fn format_latest(version: semver::Version) -> String {
@@ -222,13 +219,13 @@ fn print_lockfile_update(
222219
// This metadata is often stuff like git commit hashes, which are
223220
// not meaningfully ordered.
224221
if removed.version().cmp_precedence(added.version()) == Ordering::Greater {
225-
print_change("Downgrading", msg, &style::WARN)?;
222+
shell.status_with_color("Downgrading", msg, &style::WARN)?;
226223
} else {
227-
print_change("Updating", msg, &style::GOOD)?;
224+
shell.status_with_color("Updating", msg, &style::GOOD)?;
228225
}
229226
} else {
230227
for package in diff.removed.iter() {
231-
print_change("Removing", format!("{package}"), &style::ERROR)?;
228+
shell.status_with_color("Removing", format!("{package}"), &style::ERROR)?;
232229
}
233230
for package in diff.added.iter() {
234231
let latest = if !possibilities.is_empty() {
@@ -244,7 +241,7 @@ fn print_lockfile_update(
244241
}
245242
.unwrap_or_default();
246243

247-
print_change("Adding", format!("{package}{latest}"), &style::NOTE)?;
244+
shell.status_with_color("Adding", format!("{package}{latest}"), &style::NOTE)?;
248245
}
249246
}
250247
for package in &diff.unchanged {
@@ -262,8 +259,8 @@ fn print_lockfile_update(
262259

263260
if let Some(latest) = latest {
264261
unchanged_behind += 1;
265-
if gctx.shell().verbosity() == Verbosity::Verbose {
266-
gctx.shell().status_with_color(
262+
if shell.verbosity() == Verbosity::Verbose {
263+
shell.status_with_color(
267264
"Unchanged",
268265
format!("{package}{latest}"),
269266
&anstyle::Style::new().bold(),
@@ -272,13 +269,13 @@ fn print_lockfile_update(
272269
}
273270
}
274271
}
275-
if gctx.shell().verbosity() == Verbosity::Verbose {
276-
gctx.shell().note(
272+
if shell.verbosity() == Verbosity::Verbose {
273+
shell.note(
277274
"to see how you depend on a package, run `cargo tree --invert --package <dep>@<ver>`",
278275
)?;
279276
} else {
280277
if 0 < unchanged_behind {
281-
gctx.shell().note(format!(
278+
shell.note(format!(
282279
"pass `--verbose` to see {unchanged_behind} unchanged dependencies behind latest"
283280
))?;
284281
}

0 commit comments

Comments
 (0)