From fce8e02e61bcb52e2fe8c21d6351d6cc8f3a4af8 Mon Sep 17 00:00:00 2001 From: konsti Date: Tue, 7 May 2024 23:38:01 +0200 Subject: [PATCH] Rename dependencies type to available and unavailable (#216) In uv, dependencies are either available or unavailable. They are not unknown, but rather missing due to some brokenness: We're offline but the dep is not cached, the version list failed to deserialize, etc. (https://github.com/astral-sh/uv/blob/0b84eb01408eb0e90b5720b027359aac10708665/crates/uv-resolver/src/resolver/mod.rs#L945-L996). This change is a rename of the variants of `Dependencies` to reflect that, upstreaming the change from uv. --- examples/caching_dependency_provider.rs | 8 ++++---- src/lib.rs | 3 +-- src/solver.rs | 27 ++++++++++++------------- src/type_aliases.rs | 2 +- tests/proptest.rs | 12 +++++------ tests/sat_dependency_provider.rs | 4 ++-- 6 files changed, 27 insertions(+), 29 deletions(-) diff --git a/examples/caching_dependency_provider.rs b/examples/caching_dependency_provider.rs index 683df4b5..ae04886a 100644 --- a/examples/caching_dependency_provider.rs +++ b/examples/caching_dependency_provider.rs @@ -32,18 +32,18 @@ impl> DependencyProvider for CachingDependenc ) -> Result, DP::Err> { let mut cache = self.cached_dependencies.borrow_mut(); match cache.get_dependencies(package, version) { - Ok(Dependencies::Unknown(_)) => { + Ok(Dependencies::Unavailable(_)) => { let dependencies = self.remote_dependencies.get_dependencies(package, version); match dependencies { - Ok(Dependencies::Known(dependencies)) => { + Ok(Dependencies::Available(dependencies)) => { cache.add_dependencies( package.clone(), version.clone(), dependencies.clone(), ); - Ok(Dependencies::Known(dependencies)) + Ok(Dependencies::Available(dependencies)) } - Ok(Dependencies::Unknown(reason)) => Ok(Dependencies::Unknown(reason)), + Ok(Dependencies::Unavailable(reason)) => Ok(Dependencies::Unavailable(reason)), error @ Err(_) => error, } } diff --git a/src/lib.rs b/src/lib.rs index 434d614c..4d02e7b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -121,7 +121,6 @@ //! //! The third method [get_dependencies](crate::solver::DependencyProvider::get_dependencies) //! aims at retrieving the dependencies of a given package at a given version. -//! Returns [None] if dependencies are unknown. //! //! In a real scenario, these two methods may involve reading the file system //! or doing network request, so you may want to hold a cache in your @@ -151,7 +150,7 @@ //! External incompatibilities have reasons that are independent //! of the way this algorithm is implemented such as //! - dependencies: "package_a" at version 1 depends on "package_b" at version 4 -//! - missing dependencies: dependencies of "package_a" are unknown +//! - missing dependencies: dependencies of "package_a" are unavailable //! - absence of version: there is no version of "package_a" in the range [3.1.0 4.0.0[ //! //! Derived incompatibilities are obtained during the algorithm execution by deduction, diff --git a/src/solver.rs b/src/solver.rs index 821fbf1e..7a38b285 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -149,8 +149,8 @@ pub fn resolve( } })?; - let known_dependencies = match dependencies { - Dependencies::Unknown(reason) => { + let dependencies = match dependencies { + Dependencies::Unavailable(reason) => { state.add_incompatibility(Incompatibility::custom_version( p.clone(), v.clone(), @@ -158,21 +158,18 @@ pub fn resolve( )); continue; } - Dependencies::Known(x) if x.contains_key(p) => { + Dependencies::Available(x) if x.contains_key(p) => { return Err(PubGrubError::SelfDependency { package: p.clone(), version: v, }); } - Dependencies::Known(x) => x, + Dependencies::Available(x) => x, }; // Add that package and version if the dependencies are not problematic. - let dep_incompats = state.add_incompatibility_from_dependencies( - p.clone(), - v.clone(), - &known_dependencies, - ); + let dep_incompats = + state.add_incompatibility_from_dependencies(p.clone(), v.clone(), &dependencies); state.partial_solution.add_version( p.clone(), @@ -194,9 +191,9 @@ pub fn resolve( #[derive(Clone)] pub enum Dependencies { /// Package dependencies are unavailable with the reason why they are missing. - Unknown(M), + Unavailable(M), /// Container for all available package versions. - Known(DependencyConstraints), + Available(DependencyConstraints), } /// Trait that allows the algorithm to retrieve available packages and their dependencies. @@ -277,7 +274,7 @@ pub trait DependencyProvider { ) -> Result, Self::Err>; /// Retrieves the package dependencies. - /// Return [Dependencies::Unknown] if its dependencies are unknown. + /// Return [Dependencies::Unavailable] if its dependencies are unavailable. #[allow(clippy::type_complexity)] fn get_dependencies( &self, @@ -398,8 +395,10 @@ impl DependencyProvider for OfflineDependencyProvide version: &VS::V, ) -> Result, Infallible> { Ok(match self.dependencies(package, version) { - None => Dependencies::Unknown("its dependencies could not be determined".to_string()), - Some(dependencies) => Dependencies::Known(dependencies), + None => { + Dependencies::Unavailable("its dependencies could not be determined".to_string()) + } + Some(dependencies) => Dependencies::Available(dependencies), }) } } diff --git a/src/type_aliases.rs b/src/type_aliases.rs index 96aabbf4..8b893d18 100644 --- a/src/type_aliases.rs +++ b/src/type_aliases.rs @@ -17,7 +17,7 @@ pub type SelectedDependencies = /// Holds information about all possible versions a given package can accept. /// There is a difference in semantics between an empty map -/// inside [DependencyConstraints] and [Dependencies::Unknown](crate::solver::Dependencies::Unknown): +/// inside [DependencyConstraints] and [Dependencies::Unavailable](crate::solver::Dependencies::Unavailable): /// the former means the package has no dependency and it is a known fact, /// while the latter means they could not be fetched by the [DependencyProvider]. pub type DependencyConstraints = Map; diff --git a/tests/proptest.rs b/tests/proptest.rs index 356b443f..33e5cece 100644 --- a/tests/proptest.rs +++ b/tests/proptest.rs @@ -322,8 +322,8 @@ fn retain_versions( continue; } let deps = match dependency_provider.get_dependencies(n, v).unwrap() { - Dependencies::Unknown(_) => panic!(), - Dependencies::Known(deps) => deps, + Dependencies::Unavailable(_) => panic!(), + Dependencies::Available(deps) => deps, }; smaller_dependency_provider.add_dependencies(n.clone(), v.clone(), deps) } @@ -346,8 +346,8 @@ fn retain_dependencies( for n in dependency_provider.packages() { for v in dependency_provider.versions(n).unwrap() { let deps = match dependency_provider.get_dependencies(n, v).unwrap() { - Dependencies::Unknown(_) => panic!(), - Dependencies::Known(deps) => deps, + Dependencies::Unavailable(_) => panic!(), + Dependencies::Available(deps) => deps, }; smaller_dependency_provider.add_dependencies( n.clone(), @@ -517,8 +517,8 @@ proptest! { .get_dependencies(package, version) .unwrap() { - Dependencies::Unknown(_) => panic!(), - Dependencies::Known(d) => d.into_iter().collect(), + Dependencies::Unavailable(_) => panic!(), + Dependencies::Available(d) => d.into_iter().collect(), }; if !dependencies.is_empty() { to_remove.insert((package, **version, dep_idx.get(&dependencies).0)); diff --git a/tests/sat_dependency_provider.rs b/tests/sat_dependency_provider.rs index 36840f8e..ec496c1b 100644 --- a/tests/sat_dependency_provider.rs +++ b/tests/sat_dependency_provider.rs @@ -66,8 +66,8 @@ impl SatResolve { // active packages need each of there `deps` to be satisfied for (p, v, var) in &all_versions { let deps = match dp.get_dependencies(p, v).unwrap() { - Dependencies::Unknown(_) => panic!(), - Dependencies::Known(d) => d, + Dependencies::Unavailable(_) => panic!(), + Dependencies::Available(d) => d, }; for (p1, range) in &deps { let empty_vec = vec![];