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.
This commit is contained in:
konsti 2024-03-18 13:27:24 +01:00 committed by GitHub
parent ecc46c5412
commit 060a2fb80b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 74 additions and 51 deletions

View File

@ -1,3 +1,5 @@
use std::fmt::{Display, Formatter};
use pep440_rs::VersionSpecifiers; use pep440_rs::VersionSpecifiers;
use platform_tags::{IncompatibleTag, TagCompatibility, TagPriority}; use platform_tags::{IncompatibleTag, TagCompatibility, TagPriority};
use pypi_types::{Hashes, Yanked}; use pypi_types::{Hashes, Yanked};
@ -44,6 +46,68 @@ pub enum IncompatibleDist {
Unavailable, 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)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum WheelCompatibility { pub enum WheelCompatibility {
Incompatible(IncompatibleWheel), Incompatible(IncompatibleWheel),

View File

@ -21,10 +21,11 @@ use distribution_types::{
BuiltDist, Dist, DistributionMetadata, IncompatibleDist, IncompatibleSource, IncompatibleWheel, BuiltDist, Dist, DistributionMetadata, IncompatibleDist, IncompatibleSource, IncompatibleWheel,
Name, RemoteSource, SourceDist, VersionOrUrl, Name, RemoteSource, SourceDist, VersionOrUrl,
}; };
pub(crate) use locals::Locals;
use pep440_rs::{Version, MIN_VERSION}; use pep440_rs::{Version, MIN_VERSION};
use pep508_rs::{MarkerEnvironment, Requirement}; use pep508_rs::{MarkerEnvironment, Requirement};
use platform_tags::{IncompatibleTag, Tags}; use platform_tags::Tags;
use pypi_types::{Metadata23, Yanked}; use pypi_types::Metadata23;
pub(crate) use urls::Urls; pub(crate) use urls::Urls;
use uv_client::{FlatIndex, RegistryClient}; use uv_client::{FlatIndex, RegistryClient};
use uv_distribution::DistributionDatabase; use uv_distribution::DistributionDatabase;
@ -52,10 +53,8 @@ pub use crate::resolver::provider::{
}; };
use crate::resolver::reporter::Facade; use crate::resolver::reporter::Facade;
pub use crate::resolver::reporter::{BuildId, Reporter}; pub use crate::resolver::reporter::{BuildId, Reporter};
use crate::yanks::AllowedYanks; use crate::yanks::AllowedYanks;
use crate::{DependencyMode, Options}; use crate::{DependencyMode, Options};
pub(crate) use locals::Locals;
mod index; mod index;
mod locals; mod locals;
@ -347,8 +346,12 @@ impl<'a, Provider: ResolverProvider> Resolver<'a, Provider> {
// them as incompatible dependencies instead of marking the package version // them as incompatible dependencies instead of marking the package version
// as unavailable directly // as unavailable directly
UnavailableVersion::IncompatibleDist( UnavailableVersion::IncompatibleDist(
IncompatibleDist::Source(IncompatibleSource::RequiresPython(requires_python)) IncompatibleDist::Source(IncompatibleSource::RequiresPython(
| IncompatibleDist::Wheel(IncompatibleWheel::RequiresPython(requires_python)) requires_python,
))
| IncompatibleDist::Wheel(IncompatibleWheel::RequiresPython(
requires_python,
)),
) => { ) => {
let python_version = requires_python let python_version = requires_python
.iter() .iter()
@ -369,51 +372,7 @@ impl<'a, Provider: ResolverProvider> Resolver<'a, Provider> {
continue; continue;
} }
UnavailableVersion::IncompatibleDist(incompatibility) => { UnavailableVersion::IncompatibleDist(incompatibility) => {
match incompatibility { incompatibility.to_string()
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()
}
} }
}; };
state.add_incompatibility(Incompatibility::unavailable( state.add_incompatibility(Incompatibility::unavailable(