Skip to content

Commit fc00223

Browse files
committed
refactor(tree): replace previous depth: u32 opts with new type DisplayDepth
refactor(tree): replace previous `depth: u32` opts with new type `DisplayDepth`
1 parent 1c0f6cf commit fc00223

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::cli;
22
use crate::command_prelude::*;
33
use anyhow::{bail, format_err};
44
use cargo::core::dependency::DepKind;
5-
use cargo::ops::tree::{self, EdgeKind};
5+
use cargo::ops::tree::{self, DisplayDepth, EdgeKind};
66
use cargo::ops::Packages;
77
use cargo::util::print_available_packages;
88
use cargo::util::CargoResult;
@@ -162,6 +162,12 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
162162

163163
let pkgs_to_prune = args._values_of("prune");
164164

165+
let display_depth = args
166+
._value_of("depth")
167+
.map(|s| s.parse::<DisplayDepth>())
168+
.transpose()?
169+
.unwrap_or(DisplayDepth::MaxDisplayDepth(u32::MAX));
170+
165171
let packages = args.packages_from_flags()?;
166172
let mut invert = args
167173
.get_many::<String>("invert")
@@ -222,7 +228,7 @@ subtree of the package given to -p.\n\
222228
duplicates: args.flag("duplicates"),
223229
format: args.get_one::<String>("format").cloned().unwrap(),
224230
graph_features,
225-
max_display_depth: args.value_of_u32("depth")?.unwrap_or(u32::MAX),
231+
display_depth,
226232
no_proc_macro,
227233
};
228234

src/cargo/ops/tree/mod.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ pub struct TreeOptions {
4343
pub format: String,
4444
/// Includes features in the tree as separate nodes.
4545
pub graph_features: bool,
46-
/// Maximum display depth of the dependency tree.
47-
pub max_display_depth: u32,
46+
/// Display depth of the dependency tree.
47+
/// If non-negative integer, display dependencies with that amount of max depth.
48+
pub display_depth: DisplayDepth,
4849
/// Excludes proc-macro dependencies.
4950
pub no_proc_macro: bool,
5051
}
@@ -86,6 +87,30 @@ impl FromStr for Prefix {
8687
}
8788
}
8889

90+
#[derive(Clone, Copy)]
91+
pub enum DisplayDepth {
92+
MaxDisplayDepth(u32),
93+
}
94+
95+
impl FromStr for DisplayDepth {
96+
type Err = clap::Error;
97+
98+
fn from_str(s: &str) -> Result<Self, Self::Err> {
99+
match s {
100+
s => s.parse().map(Self::MaxDisplayDepth).map_err(|_| {
101+
clap::Error::raw(
102+
clap::error::ErrorKind::ValueValidation,
103+
format!(
104+
"supported values for --depth are non-negative integers, \
105+
but `{}` is unknown",
106+
s
107+
),
108+
)
109+
}),
110+
}
111+
}
112+
}
113+
89114
struct Symbols {
90115
down: &'static str,
91116
tee: &'static str,
@@ -250,7 +275,7 @@ fn print(
250275
pkgs_to_prune,
251276
opts.prefix,
252277
opts.no_dedupe,
253-
opts.max_display_depth,
278+
opts.display_depth,
254279
&mut visited_deps,
255280
&mut levels_continue,
256281
&mut print_stack,
@@ -270,7 +295,7 @@ fn print_node<'a>(
270295
pkgs_to_prune: &[PackageIdSpec],
271296
prefix: Prefix,
272297
no_dedupe: bool,
273-
max_display_depth: u32,
298+
display_depth: DisplayDepth,
274299
visited_deps: &mut HashSet<usize>,
275300
levels_continue: &mut Vec<bool>,
276301
print_stack: &mut Vec<usize>,
@@ -329,7 +354,7 @@ fn print_node<'a>(
329354
pkgs_to_prune,
330355
prefix,
331356
no_dedupe,
332-
max_display_depth,
357+
display_depth,
333358
visited_deps,
334359
levels_continue,
335360
print_stack,
@@ -349,7 +374,7 @@ fn print_dependencies<'a>(
349374
pkgs_to_prune: &[PackageIdSpec],
350375
prefix: Prefix,
351376
no_dedupe: bool,
352-
max_display_depth: u32,
377+
display_depth: DisplayDepth,
353378
visited_deps: &mut HashSet<usize>,
354379
levels_continue: &mut Vec<bool>,
355380
print_stack: &mut Vec<usize>,
@@ -378,6 +403,10 @@ fn print_dependencies<'a>(
378403
}
379404
}
380405

406+
let max_display_depth = match display_depth {
407+
DisplayDepth::MaxDisplayDepth(max) => max,
408+
};
409+
381410
// Current level exceeds maximum display depth. Skip.
382411
if levels_continue.len() + 1 > max_display_depth as usize {
383412
return;
@@ -407,7 +436,7 @@ fn print_dependencies<'a>(
407436
pkgs_to_prune,
408437
prefix,
409438
no_dedupe,
410-
max_display_depth,
439+
display_depth,
411440
visited_deps,
412441
levels_continue,
413442
print_stack,

0 commit comments

Comments
 (0)