Skip to content

Commit

Permalink
Extract annotation format functions onto match statement
Browse files Browse the repository at this point in the history
  • Loading branch information
DrJackilD committed Feb 20, 2024
1 parent 67741bb commit 4d71213
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions crates/uv-resolver/src/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,34 +289,6 @@ impl<'a> DisplayResolutionGraph<'a> {
annotation_style,
}
}

fn annotation_line(edges: Vec<&Dist>) -> String {
let deps = edges
.into_iter()
.map(|dependency| format!("{}", dependency.name()))
.collect::<Vec<_>>()
.join(", ");
format!("{}", format!("# via {deps}").green())
}

fn annotation_split(edges: Vec<&Dist>) -> String {
let mut result: String = String::new();
match edges.len() {
0 => {}
1 => {
result = format!("{}", format!(" # via {}", edges[0].name()).green());
}
_ => {
let deps = edges
.into_iter()
.map(|dependency| format!(" # {}", dependency.name()))
.collect::<Vec<_>>()
.join("\n");
result = format!("{}", format!(" # via\n{deps}").green());
}
}
result
}
}

/// Write the graph in the `{name}=={version}` format of requirements.txt that pip uses.
Expand Down Expand Up @@ -422,14 +394,33 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> {
AnnotationStyle::Line => {
if !edges.is_empty() {
sep = if self.show_hashes { "\n " } else { " " };
annotation = DisplayResolutionGraph::annotation_line(edges);
let deps = edges
.into_iter()
.map(|dependency| format!("{}", dependency.name()))
.collect::<Vec<_>>()
.join(", ");
annotation = format!("{}", format!("# via {deps}").green());
}
}
AnnotationStyle::Split => {
if !edges.is_empty() {
sep = "\n";
}
annotation = DisplayResolutionGraph::annotation_split(edges);
match edges.len() {
0 => {}
1 => {
annotation =
format!("{}", format!(" # via {}", edges[0].name()).green());
}
_ => {
let deps = edges
.into_iter()
.map(|dependency| format!(" # {}", dependency.name()))
.collect::<Vec<_>>()
.join("\n");
annotation = format!("{}", format!(" # via\n{deps}").green());
}
}
}
}
}
Expand Down

0 comments on commit 4d71213

Please sign in to comment.