Fix formatting of negated singleton versions in error messages (#836)

Closes #805 
Requires https://github.com/zanieb/pubgrub/pull/17
This commit is contained in:
Zanie Blue 2024-01-08 12:33:01 -06:00 committed by GitHub
parent c3d37db85b
commit 2b0c2e294b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 9 deletions

View file

@ -125,7 +125,10 @@ impl ReportFormatter<PubGrubPackage, Range<PubGrubVersion>> for PubGrubReportFor
&External::FromDependencyOf((*p2).clone(), r2.clone(), (*p1).clone(), r1.clone()),
),
slice => {
let str_terms: Vec<_> = slice.iter().map(|(p, t)| format!("{p} {t}")).collect();
let str_terms: Vec<_> = slice
.iter()
.map(|(p, t)| format!("{p}{}", PubGrubTerm::from_term((*t).clone())))
.collect();
str_terms.join(", ") + " are incompatible"
}
}
@ -232,3 +235,29 @@ impl std::fmt::Display for PubGrubHint {
}
}
}
/// A derivative of the [Term] type with custom formatting.
struct PubGrubTerm {
inner: Term<Range<PubGrubVersion>>,
}
impl std::fmt::Display for PubGrubTerm {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.inner {
Term::Positive(set) => write!(f, "{set}"),
Term::Negative(set) => {
if let Some(version) = set.as_singleton() {
write!(f, "!={version}")
} else {
write!(f, "!( {set} )")
}
}
}
}
}
impl PubGrubTerm {
fn from_term(term: Term<Range<PubGrubVersion>>) -> PubGrubTerm {
PubGrubTerm { inner: term }
}
}