Skip to content

Commit

Permalink
improve code and remove root from message
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Nov 1, 2024
1 parent 78a803a commit cf23e27
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/conflict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ impl<'i, I: Interner> fmt::Display for DisplayUnsat<'i, I> {
&ConflictCause::Constrains(version_set_id) => {
writeln!(
f,
"{indent}root constraint '{version_set}' cannot be fulfilled",
"{indent}constraint '{version_set}' cannot be fulfilled",
version_set = self.interner.display_version_set(version_set_id),
)?;
}
Expand Down
56 changes: 20 additions & 36 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,46 +355,30 @@ impl<'s> SnapshotProvider<'s> {
}
}

/// Adds another requirement that matches any version of a package
/// Adds another requirement that matches any version of a package.
/// If you use "*" as the matcher, it will match any version of the package.
pub fn add_package_requirement(&mut self, name: NameId, matcher: &str) -> VersionSetId {
let id = self.snapshot.version_sets.max() + self.additional_version_sets.len();

let package = self.package(name);

// match packages if not `*`
if matcher != "*" {
let matching_candidates = package
.solvables
.iter()
.copied()
.filter(|&s| {
self.solvable(s)
.display
.contains(matcher)
})
.collect();

let version_set = VersionSet {
name,
display: format!("{} {}", package.name, matcher),
matching_candidates,
};

self.additional_version_sets.push(version_set);
return VersionSetId::from_usize(id);
} else {
// if `*` match all packages
let matching_candidates = package.solvables.iter().copied().collect();

let version_set = VersionSet {
name,
display: "*".to_string(),
matching_candidates,
};

self.additional_version_sets.push(version_set);
return VersionSetId::from_usize(id);
}
let matching_candidates = package
.solvables
.iter()
.copied()
.filter(|&s| matcher == "*" || self.solvable(s).display.contains(matcher))
.collect();

self.additional_version_sets.push(VersionSet {
name,
display: if matcher == "*" {
"*".to_string()
} else {
format!("{} {}", package.name, matcher)
},
matching_candidates,
});

VersionSetId::from_usize(id)
}

fn solvable(&self, solvable: SolvableId) -> &Solvable {
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/solver__root_constraints.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ expression: "solve_for_snapshot(snapshot_provider, &[union_req], &[icons_req])"
The following packages are incompatible
└─ union * can be installed with any of the following options:
└─ union union=1
├─ root constraint 'union 5' cannot be fulfilled
├─ constraint 'union 5' cannot be fulfilled
10 changes: 7 additions & 3 deletions tests/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,6 @@ fn test_root_constraints() {
("union", 1, vec!["icons"]),
]);

let icons_name_id = provider.package_name("icons");
let union_name_id = provider.package_name("union");

let snapshot = provider.into_snapshot();
Expand Down Expand Up @@ -1429,9 +1428,14 @@ fn serialize_snapshot(snapshot: &DependencySnapshot, destination: impl AsRef<std
serde_json::to_writer_pretty(file, snapshot).unwrap()
}

fn solve_for_snapshot(provider: SnapshotProvider, root_reqs: &[VersionSetId], root_constraints: &[VersionSetId]) -> String {
fn solve_for_snapshot(
provider: SnapshotProvider,
root_reqs: &[VersionSetId],
root_constraints: &[VersionSetId],
) -> String {
let mut solver = Solver::new(provider);
let problem = Problem::new().requirements(root_reqs.iter().copied().map(Into::into).collect())
let problem = Problem::new()
.requirements(root_reqs.iter().copied().map(Into::into).collect())
.constraints(root_constraints.iter().copied().map(Into::into).collect());
match solver.solve(problem) {
Ok(solvables) => transaction_to_string(solver.provider(), &solvables),
Expand Down

0 comments on commit cf23e27

Please sign in to comment.