Skip to content

Commit feaa312

Browse files
committed
Update cargo tree from review comments.
1 parent 96a3937 commit feaa312

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

src/cargo/ops/tree/format/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use self::parse::{Parser, RawChunk};
22
use super::{Graph, Node};
3-
use anyhow::{anyhow, Error};
3+
use anyhow::{bail, Error};
44
use std::fmt;
55

66
mod parse;
@@ -27,9 +27,9 @@ impl Pattern {
2727
RawChunk::Argument("r") => Chunk::Repository,
2828
RawChunk::Argument("f") => Chunk::Features,
2929
RawChunk::Argument(a) => {
30-
return Err(anyhow!("unsupported pattern `{}`", a));
30+
bail!("unsupported pattern `{}`", a);
3131
}
32-
RawChunk::Error(err) => return Err(anyhow!("{}", err)),
32+
RawChunk::Error(err) => bail!("{}", err),
3333
};
3434
chunks.push(chunk);
3535
}
@@ -63,8 +63,8 @@ impl<'a> fmt::Display for Display<'a> {
6363
} => {
6464
let package = self.graph.package_for_id(*package_id);
6565
for chunk in &self.pattern.0 {
66-
match *chunk {
67-
Chunk::Raw(ref s) => fmt.write_str(s)?,
66+
match chunk {
67+
Chunk::Raw(s) => fmt.write_str(s)?,
6868
Chunk::Package => {
6969
write!(fmt, "{} v{}", package.name(), package.version())?;
7070

@@ -74,12 +74,12 @@ impl<'a> fmt::Display for Display<'a> {
7474
}
7575
}
7676
Chunk::License => {
77-
if let Some(ref license) = package.manifest().metadata().license {
77+
if let Some(license) = &package.manifest().metadata().license {
7878
write!(fmt, "{}", license)?;
7979
}
8080
}
8181
Chunk::Repository => {
82-
if let Some(ref repository) = package.manifest().metadata().repository {
82+
if let Some(repository) = &package.manifest().metadata().repository {
8383
write!(fmt, "{}", repository)?;
8484
}
8585
}
@@ -98,6 +98,8 @@ impl<'a> fmt::Display for Display<'a> {
9898
write!(fmt, " (command-line)")?;
9999
}
100100
}
101+
// The node_index in Node::Feature must point to a package
102+
// node, see `add_feature`.
101103
_ => panic!("unexpected feature node {:?}", for_node),
102104
}
103105
}

src/cargo/ops/tree/format/parse.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1+
//! Parser for the `--format` string for `cargo tree`.
2+
13
use std::iter;
24
use std::str;
35

46
pub enum RawChunk<'a> {
7+
/// Raw text to include in the output.
58
Text(&'a str),
9+
/// A substitution to place in the output. For example, the argument "p"
10+
/// emits the package name.
611
Argument(&'a str),
12+
/// Indicates an error in the format string. The given string is a
13+
/// human-readable message explaining the error.
714
Error(&'static str),
815
}
916

17+
/// `cargo tree` format parser.
18+
///
19+
/// The format string indicates how each package should be displayed. It
20+
/// includes simple markers surrounded in curly braces that will be
21+
/// substituted with their corresponding values. For example, the text
22+
/// "{p} license:{l}" will substitute the `{p}` with the package name/version
23+
/// (and optionally source), and the `{l}` will be the license from
24+
/// `Cargo.toml`.
25+
///
26+
/// Substitutions are alphabetic characters between curly braces, like `{p}`
27+
/// or `{foo}`. The actual interpretation of these are done in the `Pattern`
28+
/// struct.
29+
///
30+
/// Bare curly braces can be included in the output with double braces like
31+
/// `{{` will include a single `{`, similar to Rust's format strings.
1032
pub struct Parser<'a> {
1133
s: &'a str,
1234
it: iter::Peekable<str::CharIndices<'a>>,

src/cargo/ops/tree/graph.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,15 @@ impl<'a> Graph<'a> {
9292
}
9393

9494
/// Returns a list of nodes the given node index points to for the given kind.
95-
///
96-
/// Returns None if there are none.
97-
pub fn connected_nodes(&self, from: usize, kind: &Edge) -> Option<Vec<usize>> {
95+
pub fn connected_nodes(&self, from: usize, kind: &Edge) -> Vec<usize> {
9896
match self.edges[from].0.get(kind) {
9997
Some(indexes) => {
10098
// Created a sorted list for consistent output.
10199
let mut indexes = indexes.clone();
102100
indexes.sort_unstable_by(|a, b| self.nodes[*a].cmp(&self.nodes[*b]));
103-
Some(indexes)
101+
indexes
104102
}
105-
None => None,
103+
None => Vec::new(),
106104
}
107105
}
108106

@@ -123,6 +121,8 @@ impl<'a> Graph<'a> {
123121
})
124122
.map(|(i, node)| (node, i))
125123
.collect();
124+
// Sort for consistent output (the same command should always return
125+
// the same output). "unstable" since nodes should always be unique.
126126
result.sort_unstable();
127127
result.into_iter().map(|(_node, i)| i).collect()
128128
}
@@ -403,13 +403,15 @@ fn add_feature(
403403
to: usize,
404404
kind: Edge,
405405
) -> usize {
406+
// `to` *must* point to a package node.
407+
assert!(matches! {graph.nodes[to], Node::Package{..}});
406408
let node = Node::Feature {
407409
node_index: to,
408410
name,
409411
};
410-
let (node_index, _new) = match graph.index.get(&node) {
411-
Some(idx) => (*idx, false),
412-
None => (graph.add_node(node), true),
412+
let node_index = match graph.index.get(&node) {
413+
Some(idx) => *idx,
414+
None => graph.add_node(node),
413415
};
414416
if let Some(from) = from {
415417
graph.edges[from].add_edge(kind, node_index);

src/cargo/ops/tree/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ fn print_dependencies<'a>(
291291
print_stack: &mut Vec<usize>,
292292
kind: &Edge,
293293
) {
294-
let deps = match graph.connected_nodes(node_index, kind) {
295-
Some(deps) => deps,
296-
None => return,
297-
};
294+
let deps = graph.connected_nodes(node_index, kind);
295+
if deps.is_empty() {
296+
return;
297+
}
298298

299299
let name = match kind {
300300
Edge::Dep(DepKind::Normal) => None,

0 commit comments

Comments
 (0)