2023-11-08 06:57:26 -08:00
use std ::fmt ::Formatter ;
2023-10-19 23:23:26 -04:00
use pubgrub ::range ::Range ;
2023-12-01 13:36:12 -06:00
use pubgrub ::report ::{ DefaultStringReporter , Reporter } ;
2023-10-09 23:29:09 -04:00
use thiserror ::Error ;
2023-11-01 06:21:44 -07:00
use url ::Url ;
2023-10-09 23:29:09 -04:00
2023-11-24 18:47:58 +01:00
use distribution_types ::{ BuiltDist , SourceDist } ;
2023-10-09 23:29:09 -04:00
use pep508_rs ::Requirement ;
2023-11-24 18:47:58 +01:00
use puffin_distribution ::DistributionDatabaseError ;
2023-11-03 15:58:12 -07:00
use puffin_normalize ::PackageName ;
2023-10-09 23:29:09 -04:00
2023-10-29 19:03:52 -07:00
use crate ::pubgrub ::{ PubGrubPackage , PubGrubVersion } ;
2023-12-01 13:36:12 -06:00
use crate ::PubGrubReportFormatter ;
2023-10-15 22:05:44 -04:00
2023-10-09 23:29:09 -04:00
#[ derive(Error, Debug) ]
pub enum ResolveError {
#[ error( " Failed to find a version of {0} that satisfies the requirement " ) ]
NotFound ( Requirement ) ,
2023-10-16 13:26:46 -04:00
#[ error( " The request stream terminated unexpectedly " ) ]
StreamTermination ,
2023-10-09 23:29:09 -04:00
#[ error(transparent) ]
2023-10-22 20:18:30 -07:00
Client ( #[ from ] puffin_client ::Error ) ,
2023-10-09 23:29:09 -04:00
#[ error(transparent) ]
TrySend ( #[ from ] futures ::channel ::mpsc ::SendError ) ,
2023-10-15 22:05:44 -04:00
2023-10-16 13:26:46 -04:00
#[ error(transparent) ]
Join ( #[ from ] tokio ::task ::JoinError ) ,
2023-10-15 22:05:44 -04:00
#[ error(transparent) ]
2023-11-08 06:57:26 -08:00
PubGrub ( #[ from ] RichPubGrubError ) ,
2023-10-25 22:05:13 +02:00
2023-11-03 15:58:12 -07:00
#[ error( " Package metadata name `{metadata}` does not match given name `{given}` " ) ]
NameMismatch {
given : PackageName ,
metadata : PackageName ,
} ,
2023-11-05 09:09:58 -08:00
#[ error( " ~= operator requires at least two release segments: {0} " ) ]
InvalidTildeEquals ( pep440_rs ::VersionSpecifier ) ,
2023-11-17 08:28:12 -06:00
#[ error( " Conflicting URLs for package `{0}`: \n - {1} \n - {2} " ) ]
2023-11-05 09:09:58 -08:00
ConflictingUrls ( PackageName , String , String ) ,
2023-11-16 14:02:06 -06:00
#[ error( " Conflicting versions for `{0}`: {1} " ) ]
ConflictingVersions ( String , String ) ,
2023-11-05 09:09:58 -08:00
#[ error( " Package `{0}` attempted to resolve via URL: {1}. URL dependencies must be expressed as direct requirements or constraints. Consider adding `{0} @ {1}` to your dependencies or constraints file. " ) ]
DisallowedUrl ( PackageName , Url ) ,
2023-11-20 23:08:54 +00:00
#[ error(transparent) ]
DistributionType ( #[ from ] distribution_types ::Error ) ,
2023-11-24 18:47:58 +01:00
#[ error( " Failed to download {0} " ) ]
Fetch ( Box < BuiltDist > , #[ source ] DistributionDatabaseError ) ,
2023-11-01 06:21:44 -07:00
2023-11-24 18:47:58 +01:00
#[ error( " Failed to download and build {0} " ) ]
FetchAndBuild ( Box < SourceDist > , #[ source ] DistributionDatabaseError ) ,
2023-10-09 23:29:09 -04:00
}
impl < T > From < futures ::channel ::mpsc ::TrySendError < T > > for ResolveError {
fn from ( value : futures ::channel ::mpsc ::TrySendError < T > ) -> Self {
value . into_send_error ( ) . into ( )
}
}
2023-11-08 06:57:26 -08:00
/// A wrapper around [`pubgrub::error::PubGrubError`] that displays a resolution failure report.
#[ derive(Debug) ]
pub struct RichPubGrubError {
source : pubgrub ::error ::PubGrubError < PubGrubPackage , Range < PubGrubVersion > > ,
}
impl std ::error ::Error for RichPubGrubError { }
impl std ::fmt ::Display for RichPubGrubError {
fn fmt ( & self , f : & mut Formatter < '_ > ) -> std ::fmt ::Result {
if let pubgrub ::error ::PubGrubError ::NoSolution ( derivation_tree ) = & self . source {
2023-12-01 13:36:12 -06:00
let formatter = PubGrubReportFormatter ;
let report = DefaultStringReporter ::report_with_formatter ( derivation_tree , & formatter ) ;
2023-11-08 06:57:26 -08:00
write! ( f , " {report} " )
} else {
write! ( f , " {} " , self . source )
}
}
}
impl From < pubgrub ::error ::PubGrubError < PubGrubPackage , Range < PubGrubVersion > > > for ResolveError {
fn from ( value : pubgrub ::error ::PubGrubError < PubGrubPackage , Range < PubGrubVersion > > ) -> Self {
ResolveError ::PubGrub ( RichPubGrubError { source : value } )
}
}