41
41
//! ## API
42
42
//!
43
43
//! ```
44
+ //! # use std::convert::Infallible;
44
45
//! # use pubgrub::solver::{resolve, OfflineDependencyProvider};
45
46
//! # use pubgrub::version::NumberVersion;
46
47
//! # use pubgrub::error::PubGrubError;
47
48
//! # use pubgrub::range::Range;
48
49
//! #
49
50
//! # type NumVS = Range<NumberVersion>;
50
51
//! #
51
- //! # fn try_main() -> Result<(), PubGrubError<&'static str, NumVS>> {
52
+ //! # fn try_main() -> Result<(), PubGrubError<&'static str, NumVS, Infallible >> {
52
53
//! # let dependency_provider = OfflineDependencyProvider::<&str, NumVS>::new();
53
54
//! # let package = "root";
54
55
//! # let version = 1;
70
71
71
72
use std:: cmp:: Reverse ;
72
73
use std:: collections:: { BTreeMap , BTreeSet as Set } ;
74
+ use std:: convert:: Infallible ;
73
75
use std:: error:: Error ;
74
76
75
77
use crate :: error:: PubGrubError ;
@@ -82,11 +84,11 @@ use log::{debug, info};
82
84
83
85
/// Main function of the library.
84
86
/// Finds a set of packages satisfying dependency bounds for a given package + version pair.
85
- pub fn resolve < P : Package , VS : VersionSet > (
86
- dependency_provider : & impl DependencyProvider < P , VS > ,
87
+ pub fn resolve < P : Package , VS : VersionSet , DP : DependencyProvider < P , VS > > (
88
+ dependency_provider : & DP ,
87
89
package : P ,
88
90
version : impl Into < VS :: V > ,
89
- ) -> Result < SelectedDependencies < P , VS :: V > , PubGrubError < P , VS > > {
91
+ ) -> Result < SelectedDependencies < P , VS :: V > , PubGrubError < P , VS , DP :: Err > > {
90
92
let mut state = State :: init ( package. clone ( ) , version. into ( ) ) ;
91
93
let mut added_dependencies: Map < P , Set < VS :: V > > = Map :: default ( ) ;
92
94
let mut next = package;
@@ -133,7 +135,7 @@ pub fn resolve<P: Package, VS: VersionSet>(
133
135
} ;
134
136
135
137
if !term_intersection. contains ( & v) {
136
- return Err ( PubGrubError :: ErrorChoosingPackageVersion (
138
+ return Err ( PubGrubError :: Failure (
137
139
"choose_package_version picked an incompatible version" . into ( ) ,
138
140
) ) ;
139
141
}
@@ -240,29 +242,30 @@ pub trait DependencyProvider<P: Package, VS: VersionSet> {
240
242
/// the fewest versions that match the outstanding constraint.
241
243
type Priority : Ord + Clone ;
242
244
245
+ /// The kind of error returned from these methods.
246
+ ///
247
+ /// Returning this signals that resolution should fail with this error.
248
+ type Err : Error ;
249
+
243
250
/// Once the resolver has found the highest `Priority` package from all potential valid
244
251
/// packages, it needs to know what vertion of that package to use. The most common pattern
245
252
/// is to select the largest vertion that the range contains.
246
- fn choose_version (
247
- & self ,
248
- package : & P ,
249
- range : & VS ,
250
- ) -> Result < Option < VS :: V > , Box < dyn Error + Send + Sync > > ;
253
+ fn choose_version ( & self , package : & P , range : & VS ) -> Result < Option < VS :: V > , Self :: Err > ;
251
254
252
255
/// Retrieves the package dependencies.
253
256
/// Return [Dependencies::Unknown] if its dependencies are unknown.
254
257
fn get_dependencies (
255
258
& self ,
256
259
package : & P ,
257
260
version : & VS :: V ,
258
- ) -> Result < Dependencies < P , VS > , Box < dyn Error + Send + Sync > > ;
261
+ ) -> Result < Dependencies < P , VS > , Self :: Err > ;
259
262
260
263
/// This is called fairly regularly during the resolution,
261
264
/// if it returns an Err then resolution will be terminated.
262
265
/// This is helpful if you want to add some form of early termination like a timeout,
263
266
/// or you want to add some form of user feedback if things are taking a while.
264
267
/// If not provided the resolver will run as long as needed.
265
- fn should_cancel ( & self ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
268
+ fn should_cancel ( & self ) -> Result < ( ) , Self :: Err > {
266
269
Ok ( ( ) )
267
270
}
268
271
}
@@ -340,11 +343,9 @@ impl<P: Package, VS: VersionSet> OfflineDependencyProvider<P, VS> {
340
343
/// But, that may change in new versions if better heuristics are found.
341
344
/// Versions are picked with the newest versions first.
342
345
impl < P : Package , VS : VersionSet > DependencyProvider < P , VS > for OfflineDependencyProvider < P , VS > {
343
- fn choose_version (
344
- & self ,
345
- package : & P ,
346
- range : & VS ,
347
- ) -> Result < Option < VS :: V > , Box < dyn Error + Send + Sync > > {
346
+ type Err = Infallible ;
347
+
348
+ fn choose_version ( & self , package : & P , range : & VS ) -> Result < Option < VS :: V > , Infallible > {
348
349
Ok ( self
349
350
. dependencies
350
351
. get ( package)
@@ -365,7 +366,7 @@ impl<P: Package, VS: VersionSet> DependencyProvider<P, VS> for OfflineDependency
365
366
& self ,
366
367
package : & P ,
367
368
version : & VS :: V ,
368
- ) -> Result < Dependencies < P , VS > , Box < dyn Error + Send + Sync > > {
369
+ ) -> Result < Dependencies < P , VS > , Infallible > {
369
370
Ok ( match self . dependencies ( package, version) {
370
371
None => Dependencies :: Unknown ,
371
372
Some ( dependencies) => Dependencies :: Known ( dependencies) ,
0 commit comments