Skip to content

Commit

Permalink
Nits
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 21, 2024
1 parent ba4d0e2 commit 8c82063
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
87 changes: 44 additions & 43 deletions crates/uv-resolver/src/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use petgraph::Direction;
use pubgrub::range::Range;
use pubgrub::solver::{Kind, State};
use pubgrub::type_aliases::SelectedDependencies;

use rustc_hash::FxHashMap;
use url::Url;

Expand All @@ -23,14 +22,16 @@ use uv_normalize::{ExtraName, PackageName};
use crate::pins::FilePins;
use crate::pubgrub::{PubGrubDistribution, PubGrubPackage, PubGrubPriority};
use crate::resolver::VersionsResponse;

use crate::ResolveError;

/// Indicate the style of annotation comments
/// Indicate the style of annotation comments, used to indicate the dependencies that requested each
/// package.
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
pub enum AnnotationStyle {
/// Render the annotations on a single, comma-separated line.
Line,
/// Render each annotation on its own line.
Split,
}

Expand Down Expand Up @@ -351,17 +352,13 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> {
// Print out the dependency graph.
for (index, node) in nodes {
// Display the node itself.
let mut line: String;
match node {
Node::Distribution(_, dist) => {
line = format!("{}", dist.verbatim());
}
Node::Editable(_, editable) => {
line = format!("-e {}", editable.verbatim());
}
}
let mut line = match node {
Node::Distribution(_, dist) => format!("{}", dist.verbatim()),
Node::Editable(_, editable) => format!("-e {}", editable.verbatim()),
};

// Display the distribution hashes, if any.
let mut has_hashes = false;
if self.show_hashes {
if let Some(hashes) = self
.resolution
Expand All @@ -371,15 +368,17 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> {
{
for hash in hashes {
if let Some(hash) = hash.to_string() {
has_hashes = true;
line.push_str(" \\\n");
line.push_str(&format!(" --hash={hash}"));
}
}
}
}

let mut sep = "";
let mut annotation = String::new();
// Determine the annotation comment and separator (between comment and requirement).
let mut annotation = None;

if self.include_annotations {
// Display all dependencies.
let mut edges = self
Expand All @@ -393,46 +392,48 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> {
match self.annotation_style {
AnnotationStyle::Line => {
if !edges.is_empty() {
sep = if self.show_hashes { "\n " } else { " " };
let separator = if has_hashes { "\n " } else { " " };
let deps = edges
.into_iter()
.map(|dependency| format!("{}", dependency.name()))
.collect::<Vec<_>>()
.join(", ");
annotation = format!("{}", format!("# via {deps}").green());
let comment = format!("{}", format!("# via {deps}").green());
annotation = Some((separator, comment));
}
}
AnnotationStyle::Split => {
if !edges.is_empty() {
sep = "\n";
AnnotationStyle::Split => match edges.as_slice() {
[] => {}
[edge] => {
let separator = "\n";
let comment =
format!("{}", format!(" # via {}", edge.name()).green());
annotation = Some((separator, comment));
}
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());
}
edges => {
let separator = "\n";
let deps = edges
.iter()
.map(|dependency| format!(" # {}", dependency.name()))
.collect::<Vec<_>>()
.join("\n");
let comment = format!("{}", format!(" # via\n{deps}").green());
annotation = Some((separator, comment));
}
}
},
}
}

if let Some((separator, annotation)) = annotation {
// Assemble the line with the annotations and remove trailing whitespaces.
for line in format!("{line:24}{separator}{annotation}").lines() {
let line = line.trim_end();
writeln!(f, "{line}")?;
}
} else {
// Write the line as is.
writeln!(f, "{line}")?;
}
// Assemble the line with the annotations and remove trailing whitespaces.
line = format!("{line:24}{sep}{annotation}")
.split('\n')
.map(str::trim_end)
.collect::<Vec<_>>()
.join("\n");

// Write requirement line to the formatter.
writeln!(f, "{line}")?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ struct PipCompileArgs {
#[clap(long, hide = true)]
emit_find_links: bool,

/// Choose the format of annotation comments
/// Choose the style of the annotation comments, which indicate the source of each package.
#[clap(long, default_value_t=AnnotationStyle::Split, value_enum)]
annotation_style: AnnotationStyle,

Expand Down

0 comments on commit 8c82063

Please sign in to comment.