From 060a2fb80b2ff57cdf2438e1beeacaf897968daf Mon Sep 17 00:00:00 2001 From: konsti Date: Mon, 18 Mar 2024 13:27:24 +0100 Subject: [PATCH] Shrink `solve()` a little by moving formatting out (#2506) `solve()` is our main loop. Shrinking it down a little by moving formatting out makes it easier to follow. --- .../src/prioritized_distribution.rs | 64 +++++++++++++++++++ crates/uv-resolver/src/resolver/mod.rs | 61 +++--------------- 2 files changed, 74 insertions(+), 51 deletions(-) diff --git a/crates/distribution-types/src/prioritized_distribution.rs b/crates/distribution-types/src/prioritized_distribution.rs index d6e702d98..110a69587 100644 --- a/crates/distribution-types/src/prioritized_distribution.rs +++ b/crates/distribution-types/src/prioritized_distribution.rs @@ -1,3 +1,5 @@ +use std::fmt::{Display, Formatter}; + use pep440_rs::VersionSpecifiers; use platform_tags::{IncompatibleTag, TagCompatibility, TagPriority}; use pypi_types::{Hashes, Yanked}; @@ -44,6 +46,68 @@ pub enum IncompatibleDist { Unavailable, } +impl Display for IncompatibleDist { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Self::Wheel(incompatibility) => match incompatibility { + IncompatibleWheel::NoBinary => { + f.write_str("no source distribution is available and using wheels is disabled") + } + IncompatibleWheel::Tag(tag) => match tag { + IncompatibleTag::Invalid => { + f.write_str("no wheels are available with valid tags") + } + IncompatibleTag::Python => { + f.write_str("no wheels are available with a matching Python implementation") + } + IncompatibleTag::Abi => { + f.write_str("no wheels are available with a matching Python ABI") + } + IncompatibleTag::Platform => { + f.write_str("no wheels are available with a matching platform") + } + }, + IncompatibleWheel::Yanked(yanked) => match yanked { + Yanked::Bool(_) => f.write_str("it was yanked"), + Yanked::Reason(reason) => write!( + f, + "it was yanked (reason: {})", + reason.trim().trim_end_matches('.') + ), + }, + IncompatibleWheel::ExcludeNewer(ts) => match ts { + Some(_) => f.write_str("it was published after the exclude newer time"), + None => f.write_str("it has no publish time"), + }, + IncompatibleWheel::RequiresPython(python) => { + write!(f, "it requires at python {python}") + } + }, + Self::Source(incompatibility) => match incompatibility { + IncompatibleSource::NoBuild => { + f.write_str("no wheels are usable and building from source is disabled") + } + IncompatibleSource::Yanked(yanked) => match yanked { + Yanked::Bool(_) => f.write_str("it was yanked"), + Yanked::Reason(reason) => write!( + f, + "it was yanked (reason: {})", + reason.trim().trim_end_matches('.') + ), + }, + IncompatibleSource::ExcludeNewer(ts) => match ts { + Some(_) => f.write_str("it was published after the exclude newer time"), + None => f.write_str("it has no publish time"), + }, + IncompatibleSource::RequiresPython(python) => { + write!(f, "it requires python {python}") + } + }, + Self::Unavailable => f.write_str("no distributions are available"), + } + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum WheelCompatibility { Incompatible(IncompatibleWheel), diff --git a/crates/uv-resolver/src/resolver/mod.rs b/crates/uv-resolver/src/resolver/mod.rs index 3d56d0cae..fe9965d0a 100644 --- a/crates/uv-resolver/src/resolver/mod.rs +++ b/crates/uv-resolver/src/resolver/mod.rs @@ -21,10 +21,11 @@ use distribution_types::{ BuiltDist, Dist, DistributionMetadata, IncompatibleDist, IncompatibleSource, IncompatibleWheel, Name, RemoteSource, SourceDist, VersionOrUrl, }; +pub(crate) use locals::Locals; use pep440_rs::{Version, MIN_VERSION}; use pep508_rs::{MarkerEnvironment, Requirement}; -use platform_tags::{IncompatibleTag, Tags}; -use pypi_types::{Metadata23, Yanked}; +use platform_tags::Tags; +use pypi_types::Metadata23; pub(crate) use urls::Urls; use uv_client::{FlatIndex, RegistryClient}; use uv_distribution::DistributionDatabase; @@ -52,10 +53,8 @@ pub use crate::resolver::provider::{ }; use crate::resolver::reporter::Facade; pub use crate::resolver::reporter::{BuildId, Reporter}; - use crate::yanks::AllowedYanks; use crate::{DependencyMode, Options}; -pub(crate) use locals::Locals; mod index; mod locals; @@ -347,8 +346,12 @@ impl<'a, Provider: ResolverProvider> Resolver<'a, Provider> { // them as incompatible dependencies instead of marking the package version // as unavailable directly UnavailableVersion::IncompatibleDist( - IncompatibleDist::Source(IncompatibleSource::RequiresPython(requires_python)) - | IncompatibleDist::Wheel(IncompatibleWheel::RequiresPython(requires_python)) + IncompatibleDist::Source(IncompatibleSource::RequiresPython( + requires_python, + )) + | IncompatibleDist::Wheel(IncompatibleWheel::RequiresPython( + requires_python, + )), ) => { let python_version = requires_python .iter() @@ -369,51 +372,7 @@ impl<'a, Provider: ResolverProvider> Resolver<'a, Provider> { continue; } UnavailableVersion::IncompatibleDist(incompatibility) => { - match incompatibility { - IncompatibleDist::Wheel(incompatibility) => { - match incompatibility { - IncompatibleWheel::NoBinary => "no source distribution is available and using wheels is disabled".to_string(), - IncompatibleWheel::Tag(tag) => { - match tag { - IncompatibleTag::Invalid => "no wheels are available with valid tags".to_string(), - IncompatibleTag::Python => "no wheels are available with a matching Python implementation".to_string(), - IncompatibleTag::Abi => "no wheels are available with a matching Python ABI".to_string(), - IncompatibleTag::Platform => "no wheels are available with a matching platform".to_string(), - } - } - IncompatibleWheel::Yanked(yanked) => match yanked { - Yanked::Bool(_) => "it was yanked".to_string(), - Yanked::Reason(reason) => format!( - "it was yanked (reason: {})", - reason.trim().trim_end_matches('.') - ), - }, - IncompatibleWheel::ExcludeNewer(ts) => match ts { - Some(_) => "it was published after the exclude newer time".to_string(), - None => "it has no publish time".to_string() - } - IncompatibleWheel::RequiresPython(_) => unreachable!(), - } - } - IncompatibleDist::Source(incompatibility) => { - match incompatibility { - IncompatibleSource::NoBuild => "no wheels are usable and building from source is disabled".to_string(), - IncompatibleSource::Yanked(yanked) => match yanked { - Yanked::Bool(_) => "it was yanked".to_string(), - Yanked::Reason(reason) => format!( - "it was yanked (reason: {})", - reason.trim().trim_end_matches('.') - ), - }, - IncompatibleSource::ExcludeNewer(ts) => match ts { - Some(_) => "it was published after the exclude newer time".to_string(), - None => "it has no publish time".to_string() - } - IncompatibleSource::RequiresPython(_) => unreachable!(), - } - } - IncompatibleDist::Unavailable => "no distributions are available".to_string() - } + incompatibility.to_string() } }; state.add_incompatibility(Incompatibility::unavailable(