Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 11, 2025
1 parent f59c9bf commit 6faf1ac
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 253 deletions.
84 changes: 7 additions & 77 deletions crates/uv-distribution-types/src/prioritized_distribution.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use arcstr::ArcStr;
use owo_colors::OwoColorize;
use std::collections::BTreeSet;
use std::fmt::{Display, Formatter};
use std::str::FromStr;

use arcstr::ArcStr;
use owo_colors::OwoColorize;
use tracing::debug;

use uv_distribution_filename::{BuildTag, WheelFilename};
use uv_pep440::VersionSpecifiers;
use uv_pep508::{MarkerExpression, MarkerOperator, MarkerTree, MarkerValueString};
use uv_platform_tags::{AbiTag, IncompatibleTag, LanguageTag, TagPriority, Tags};
use uv_platform_tags::{IncompatibleTag, TagPriority, Tags};
use uv_pypi_types::{HashDigest, Yanked};

use crate::{
Expand Down Expand Up @@ -271,8 +271,6 @@ pub enum WheelCompatibility {
Compatible(HashComparison, Option<TagPriority>, Option<BuildTag>),
}

pub type ExampleTag = String;

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum IncompatibleWheel {
/// The wheel was published after the exclude newer time.
Expand Down Expand Up @@ -538,27 +536,11 @@ impl PrioritizedDist {
.collect()
}

/// Returns the set of all platform tags for the distribution.
pub fn platform_tags(&self) -> BTreeSet<&str> {
self.0
.wheels
.iter()
.flat_map(|(wheel, _)| wheel.filename.platform_tag.iter().map(String::as_str))
.collect()
}

/// Returns the set of all ABI tags for the distribution.
pub fn available_platform_tags<'a>(&'a self, tags: &'a Tags) -> BTreeSet<&'a str> {
// We might need to... invert the order? So that it goes from platform, to ABI, to Python?
// I have no idea if that will fix it. Otherwise, I might have to hardcode tags or
// something.
//
// We could always just filter out tags that we know are always applicable (like `none`).
//
// What do we want? Like, the other two tags are compatible, but the third isn't?
/// Returns the set of platform tags for the distribution that are ABI-compatible with the given
/// tags.
pub fn platform_tags<'a>(&'a self, tags: &'a Tags) -> BTreeSet<&'a str> {
let mut candidates = BTreeSet::new();
for (wheel, _) in &self.0.wheels {
// For each wheel, if the Python and ABI tags are compatible, return the platform tags.
for wheel_py in &wheel.filename.python_tag {
for wheel_abi in &wheel.filename.abi_tag {
if tags.is_compatible_abi(wheel_py.as_str(), wheel_abi.as_str()) {
Expand All @@ -569,58 +551,6 @@ impl PrioritizedDist {
}
candidates
}

pub fn closest_tag(&self, tags: &Tags) -> Option<(&str, &str, &str)> {
let mut closest: Option<(&str, &str, &str, TagPriority)> = None;
for (py, abi, platform, priority) in tags.iter() {
for (wheel, _) in &self.0.wheels {
for wheel_py in &wheel.filename.python_tag {
for wheel_abi in &wheel.filename.abi_tag {
for wheel_platform in &wheel.filename.platform_tag {
// If two of the three tags match...
let compatiblity = u8::from(py == wheel_py)
+ u8::from(abi == wheel_abi)
+ u8::from(platform == wheel_platform);
if compatiblity > 1 {
if let Some((py, abi, .., existing_priority)) = closest {
if priority > existing_priority {
closest = Some((
wheel_py.as_str(),
wheel_abi.as_str(),
wheel_platform.as_str(),
priority,
));
} else if priority == existing_priority {
// Break ties based on tag comparisons.
if LanguageTag::from_str(wheel_py)
>= LanguageTag::from_str(py)
&& AbiTag::from_str(wheel_abi) >= AbiTag::from_str(abi)
{
closest = Some((
wheel_py.as_str(),
wheel_abi.as_str(),
wheel_platform.as_str(),
priority,
));
}
}
} else {
closest = Some((
wheel_py.as_str(),
wheel_abi.as_str(),
wheel_platform.as_str(),
priority,
));
}
}
}
}
}
}
}

closest.map(|(py, abi, platform, _)| (py, abi, platform))
}
}

impl<'a> CompatibleDist<'a> {
Expand Down
43 changes: 5 additions & 38 deletions crates/uv-platform-tags/src/tags.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::BTreeSet;
use std::fmt::Formatter;
use std::str::FromStr;
use std::sync::Arc;
use std::{cmp, num::NonZeroU32};

Expand Down Expand Up @@ -301,61 +300,29 @@ impl Tags {
max_compatibility
}

/// Return the highest-priority Python tag.
/// Return the highest-priority Python tag for the [`Tags`].
pub fn python_tag(&self) -> Option<&str> {
self.best.as_ref().map(|(py, _, _)| py.as_str())
}

/// Return the highest-priority ABI tag.
/// Return the highest-priority ABI tag for the [`Tags`].
pub fn abi_tag(&self) -> Option<&str> {
self.best.as_ref().map(|(_, abi, _)| abi.as_str())
}

/// Return the highest-priority platform tag.
/// Return the highest-priority platform tag for the [`Tags`].
pub fn platform_tag(&self) -> Option<&str> {
self.best.as_ref().map(|(_, _, platform)| platform.as_str())
}

pub fn abi_tags(&self, python_tag: &str) -> impl Iterator<Item = &str> + '_ {
self.map
.get(python_tag)
.into_iter()
.flat_map(|abis| abis.keys().map(String::as_str))
}

/// Returns `true` if the given language and ABI tags are compatible with the current
/// environment.
pub fn is_compatible_abi<'a>(&'a self, python_tag: &'a str, abi_tag: &'a str) -> bool {
self.map
.get(python_tag)
.map(|abis| abis.contains_key(abi_tag))
.unwrap_or(false)
}

// pub fn python_tags(&self) -> impl Iterator<Item = &str> + '_ {
// self.map.keys().map(String::as_str)
// }
//
// pub fn abi_tags(&self) -> impl Iterator<Item = &str> + '_ {
// self.map.values().flat_map(|abis| abis.keys().map(String::as_str))
// }
//
// pub fn platform_tags(&self) -> impl Iterator<Item = &str> + '_ {
// self.map.values().flat_map(|abis| abis.values().flat_map(|platforms| platforms.keys().map(String::as_str)))
// }

pub fn iter(&self) -> impl Iterator<Item = (&str, &str, &str, TagPriority)> + '_ {
self.map.iter().flat_map(|(python_tag, abi_tags)| {
abi_tags.iter().flat_map(move |(abi_tag, platform_tags)| {
platform_tags.iter().map(move |(platform_tag, priority)| {
(
python_tag.as_str(),
abi_tag.as_str(),
platform_tag.as_str(),
*priority,
)
})
})
})
}
}

/// The priority of a platform tag.
Expand Down
48 changes: 33 additions & 15 deletions crates/uv-resolver/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,22 +340,40 @@ impl NoSolutionError {

impl std::fmt::Debug for NoSolutionError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let Self {
error,
index: _,
available_versions,
available_indexes,
selector,
python_requirement,
index_locations,
index_capabilities,
unavailable_packages,
incomplete_packages,
fork_urls,
fork_indexes,
env,
tags,
workspace_members,
options,
} = self;
f.debug_struct("NoSolutionError")
.field("error", &self.error)
.field("available_versions", &self.available_versions)
.field("available_indexes", &self.available_indexes)
.field("selector", &self.selector)
.field("python_requirement", &self.python_requirement)
.field("index_locations", &self.index_locations)
.field("index_capabilities", &self.index_capabilities)
.field("unavailable_packages", &self.unavailable_packages)
.field("incomplete_packages", &self.incomplete_packages)
.field("fork_urls", &self.fork_urls)
.field("fork_indexes", &self.fork_indexes)
.field("env", &self.env)
.field("tags", &self.tags)
.field("workspace_members", &self.workspace_members)
.field("options", &self.options)
.field("error", error)
.field("available_versions", available_versions)
.field("available_indexes", available_indexes)
.field("selector", selector)
.field("python_requirement", python_requirement)
.field("index_locations", index_locations)
.field("index_capabilities", index_capabilities)
.field("unavailable_packages", unavailable_packages)
.field("incomplete_packages", incomplete_packages)
.field("fork_urls", fork_urls)
.field("fork_indexes", fork_indexes)
.field("env", env)
.field("tags", tags)
.field("workspace_members", workspace_members)
.field("options", options)
.finish()
}
}
Expand Down
Loading

0 comments on commit 6faf1ac

Please sign in to comment.