Skip to content

Commit

Permalink
Split metadata parsing into a module (#7656)
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin authored Sep 24, 2024
1 parent 83f1abd commit 484717d
Show file tree
Hide file tree
Showing 18 changed files with 984 additions and 925 deletions.
6 changes: 3 additions & 3 deletions crates/distribution-types/src/dependency_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pep440_rs::{Version, VersionSpecifiers};
use pep508_rs::Requirement;
use pypi_types::{Metadata23, VerbatimParsedUrl};
use pypi_types::{MetadataResolver, VerbatimParsedUrl};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use uv_normalize::{ExtraName, PackageName};
Expand All @@ -20,7 +20,7 @@ impl DependencyMetadata {
}

/// Retrieve a [`StaticMetadata`] entry by [`PackageName`] and [`Version`].
pub fn get(&self, package: &PackageName, version: &Version) -> Option<Metadata23> {
pub fn get(&self, package: &PackageName, version: &Version) -> Option<MetadataResolver> {
let versions = self.0.get(package)?;

// Search for an exact, then a global match.
Expand All @@ -29,7 +29,7 @@ impl DependencyMetadata {
.find(|v| v.version.as_ref() == Some(version))
.or_else(|| versions.iter().find(|v| v.version.is_none()))?;

Some(Metadata23 {
Some(MetadataResolver {
name: metadata.name.clone(),
version: version.clone(),
requires_dist: metadata.requires_dist.clone(),
Expand Down
6 changes: 3 additions & 3 deletions crates/distribution-types/src/installed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,13 @@ impl InstalledDist {
}

/// Read the `METADATA` file from a `.dist-info` directory.
pub fn metadata(&self) -> Result<pypi_types::Metadata23> {
pub fn metadata(&self) -> Result<pypi_types::MetadataResolver> {
match self {
Self::Registry(_) | Self::Url(_) => {
let path = self.path().join("METADATA");
let contents = fs::read(&path)?;
// TODO(zanieb): Update this to use thiserror so we can unpack parse errors downstream
pypi_types::Metadata23::parse_metadata(&contents).with_context(|| {
pypi_types::MetadataResolver::parse_metadata(&contents).with_context(|| {
format!(
"Failed to parse `METADATA` file at: {}",
path.user_display()
Expand All @@ -306,7 +306,7 @@ impl InstalledDist {
_ => unreachable!(),
};
let contents = fs::read(path.as_ref())?;
pypi_types::Metadata23::parse_metadata(&contents).with_context(|| {
pypi_types::MetadataResolver::parse_metadata(&contents).with_context(|| {
format!(
"Failed to parse `PKG-INFO` file at: {}",
path.user_display()
Expand Down
Loading

0 comments on commit 484717d

Please sign in to comment.