Skip to content

Commit

Permalink
Refactor the DOT implementation for tree-of a bit
Browse files Browse the repository at this point in the history
This is only a minor refactoring to keep the original structure (a bit
more compact and "nicer"), avoid an unnecessary `clone()`, and list
`DependencyType::Runtime` explicitly in the `match` to alert us if we
would ever add other variants.

Signed-off-by: Michael Weiss <michael.weiss@eviden.com>
  • Loading branch information
primeos-work committed Sep 11, 2024
1 parent 99decf1 commit 3f6454b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
29 changes: 11 additions & 18 deletions src/commands/tree_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,17 @@ pub async fn tree_of(matches: &ArgMatches, repo: Repository, config: &Configurat

let dot = matches.get_flag("dot");

let package_dags = repo
.packages()
repo.packages()
.filter(|p| pname.as_ref().map(|n| p.name() == n).unwrap_or(true))
.filter(|p| {
pvers
.as_ref()
.map(|v| v.matches(p.version()))
.unwrap_or(true)
})
.map(|package| Dag::for_root_package(package.clone(), &repo, None, &condition_data));

if dot {
package_dags
.and_then_ok(|dag| {
.map(|package| Dag::for_root_package(package.clone(), &repo, None, &condition_data))
.and_then_ok(|dag| {
if dot {
let petgraph: DiGraph<Package, DependencyType> = (*dag.dag()).clone().into();

let dot = Dot::with_attr_getters(
Expand All @@ -87,25 +84,21 @@ pub async fn tree_of(matches: &ArgMatches, repo: Repository, config: &Configurat
"{} ",
match er.weight() {
DependencyType::Build => "style = \"dotted\"",
_ => "",
DependencyType::Runtime => "",
}
)
},
&|_, node| format!("label = \"{}\" ", node.1.clone().display_name_version()),
&|_, node| format!("label = \"{}\" ", node.1.display_name_version()),
);

println!("{:?}", dot);
Ok(())
})
.collect::<Result<()>>()
} else {
package_dags
.and_then_ok(|tree| {
} else {
let stdout = std::io::stdout();
let mut outlock = stdout.lock();

ptree::write_tree(&tree.display(), &mut outlock).map_err(Error::from)
})
.collect::<Result<()>>()
}
ptree::write_tree(&dag.display(), &mut outlock).map_err(Error::from)
}
})
.collect::<Result<()>>()
}
2 changes: 1 addition & 1 deletion src/package/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Package {
}
}

pub fn display_name_version(self) -> String {
pub fn display_name_version(&self) -> String {
format!("{} {}", self.name, self.version)
}

Expand Down

0 comments on commit 3f6454b

Please sign in to comment.