Skip to content

Commit

Permalink
refactor(tree): replace previous depth: u32 opts with new type `Dis…
Browse files Browse the repository at this point in the history
…playDepth`

refactor(tree): replace previous `depth: u32` opts with new type `DisplayDepth`
  • Loading branch information
ShoyuVanilla committed Dec 12, 2024
1 parent 1c0f6cf commit fc00223
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
10 changes: 8 additions & 2 deletions src/bin/cargo/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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,
};

Expand Down
43 changes: 36 additions & 7 deletions src/cargo/ops/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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>,
Expand Down Expand Up @@ -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,
Expand All @@ -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>,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit fc00223

Please sign in to comment.