mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-28 11:20:36 +00:00
refactor(tree): replace previous depth: u32
opts with new type DisplayDepth
refactor(tree): replace previous `depth: u32` opts with new type `DisplayDepth`
This commit is contained in:
parent
1c0f6cfdcc
commit
fc00223fca
@ -2,7 +2,7 @@ use crate::cli;
|
||||
use crate::command_prelude::*;
|
||||
use anyhow::{bail, format_err};
|
||||
use cargo::core::dependency::DepKind;
|
||||
use cargo::ops::tree::{self, EdgeKind};
|
||||
use cargo::ops::tree::{self, DisplayDepth, EdgeKind};
|
||||
use cargo::ops::Packages;
|
||||
use cargo::util::print_available_packages;
|
||||
use cargo::util::CargoResult;
|
||||
@ -162,6 +162,12 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
|
||||
|
||||
let pkgs_to_prune = args._values_of("prune");
|
||||
|
||||
let display_depth = args
|
||||
._value_of("depth")
|
||||
.map(|s| s.parse::<DisplayDepth>())
|
||||
.transpose()?
|
||||
.unwrap_or(DisplayDepth::MaxDisplayDepth(u32::MAX));
|
||||
|
||||
let packages = args.packages_from_flags()?;
|
||||
let mut invert = args
|
||||
.get_many::<String>("invert")
|
||||
@ -222,7 +228,7 @@ subtree of the package given to -p.\n\
|
||||
duplicates: args.flag("duplicates"),
|
||||
format: args.get_one::<String>("format").cloned().unwrap(),
|
||||
graph_features,
|
||||
max_display_depth: args.value_of_u32("depth")?.unwrap_or(u32::MAX),
|
||||
display_depth,
|
||||
no_proc_macro,
|
||||
};
|
||||
|
||||
|
@ -43,8 +43,9 @@ pub struct TreeOptions {
|
||||
pub format: String,
|
||||
/// Includes features in the tree as separate nodes.
|
||||
pub graph_features: bool,
|
||||
/// Maximum display depth of the dependency tree.
|
||||
pub max_display_depth: u32,
|
||||
/// Display depth of the dependency tree.
|
||||
/// If non-negative integer, display dependencies with that amount of max depth.
|
||||
pub display_depth: DisplayDepth,
|
||||
/// Excludes proc-macro dependencies.
|
||||
pub no_proc_macro: bool,
|
||||
}
|
||||
@ -86,6 +87,30 @@ impl FromStr for Prefix {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum DisplayDepth {
|
||||
MaxDisplayDepth(u32),
|
||||
}
|
||||
|
||||
impl FromStr for DisplayDepth {
|
||||
type Err = clap::Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
s => s.parse().map(Self::MaxDisplayDepth).map_err(|_| {
|
||||
clap::Error::raw(
|
||||
clap::error::ErrorKind::ValueValidation,
|
||||
format!(
|
||||
"supported values for --depth are non-negative integers, \
|
||||
but `{}` is unknown",
|
||||
s
|
||||
),
|
||||
)
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Symbols {
|
||||
down: &'static str,
|
||||
tee: &'static str,
|
||||
@ -250,7 +275,7 @@ fn print(
|
||||
pkgs_to_prune,
|
||||
opts.prefix,
|
||||
opts.no_dedupe,
|
||||
opts.max_display_depth,
|
||||
opts.display_depth,
|
||||
&mut visited_deps,
|
||||
&mut levels_continue,
|
||||
&mut print_stack,
|
||||
@ -270,7 +295,7 @@ fn print_node<'a>(
|
||||
pkgs_to_prune: &[PackageIdSpec],
|
||||
prefix: Prefix,
|
||||
no_dedupe: bool,
|
||||
max_display_depth: u32,
|
||||
display_depth: DisplayDepth,
|
||||
visited_deps: &mut HashSet<usize>,
|
||||
levels_continue: &mut Vec<bool>,
|
||||
print_stack: &mut Vec<usize>,
|
||||
@ -329,7 +354,7 @@ fn print_node<'a>(
|
||||
pkgs_to_prune,
|
||||
prefix,
|
||||
no_dedupe,
|
||||
max_display_depth,
|
||||
display_depth,
|
||||
visited_deps,
|
||||
levels_continue,
|
||||
print_stack,
|
||||
@ -349,7 +374,7 @@ fn print_dependencies<'a>(
|
||||
pkgs_to_prune: &[PackageIdSpec],
|
||||
prefix: Prefix,
|
||||
no_dedupe: bool,
|
||||
max_display_depth: u32,
|
||||
display_depth: DisplayDepth,
|
||||
visited_deps: &mut HashSet<usize>,
|
||||
levels_continue: &mut Vec<bool>,
|
||||
print_stack: &mut Vec<usize>,
|
||||
@ -378,6 +403,10 @@ fn print_dependencies<'a>(
|
||||
}
|
||||
}
|
||||
|
||||
let max_display_depth = match display_depth {
|
||||
DisplayDepth::MaxDisplayDepth(max) => max,
|
||||
};
|
||||
|
||||
// Current level exceeds maximum display depth. Skip.
|
||||
if levels_continue.len() + 1 > max_display_depth as usize {
|
||||
return;
|
||||
@ -407,7 +436,7 @@ fn print_dependencies<'a>(
|
||||
pkgs_to_prune,
|
||||
prefix,
|
||||
no_dedupe,
|
||||
max_display_depth,
|
||||
display_depth,
|
||||
visited_deps,
|
||||
levels_continue,
|
||||
print_stack,
|
||||
|
Loading…
x
Reference in New Issue
Block a user