Use a dedicated type for form metadata (#14175)

This commit is contained in:
Charlie Marsh 2025-06-20 20:33:29 -04:00 committed by GitHub
parent e59835d50c
commit 0fef253c4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 122 additions and 105 deletions

View File

@ -390,7 +390,7 @@ pub async fn upload(
download_concurrency: &Semaphore,
reporter: Arc<impl Reporter>,
) -> Result<bool, PublishError> {
let form_metadata = form_metadata(file, filename)
let form_metadata = FormMetadata::read_from_file(file, filename)
.await
.map_err(|err| PublishError::PublishPrepare(file.to_path_buf(), Box::new(err)))?;
@ -644,13 +644,17 @@ async fn metadata(file: &Path, filename: &DistFilename) -> Result<Metadata23, Pu
Ok(Metadata23::parse(&contents)?)
}
/// Collect the non-file fields for the multipart request from the package METADATA.
///
/// Reference implementation: <https://github.com/pypi/warehouse/blob/d2c36d992cf9168e0518201d998b2707a3ef1e72/warehouse/forklift/legacy.py#L1376-L1430>
async fn form_metadata(
#[derive(Debug, Clone)]
struct FormMetadata(Vec<(&'static str, String)>);
impl FormMetadata {
/// Collect the non-file fields for the multipart request from the package METADATA.
///
/// Reference implementation: <https://github.com/pypi/warehouse/blob/d2c36d992cf9168e0518201d998b2707a3ef1e72/warehouse/forklift/legacy.py#L1376-L1430>
async fn read_from_file(
file: &Path,
filename: &DistFilename,
) -> Result<Vec<(&'static str, String)>, PublishPrepareError> {
) -> Result<Self, PublishPrepareError> {
let hash_hex = hash_file(file, Hasher::from(HashAlgorithm::Sha256)).await?;
let Metadata23 {
@ -745,7 +749,13 @@ async fn form_metadata(
add_vec("requires_dist", requires_dist);
add_vec("requires_external", requires_external);
Ok(form_metadata)
Ok(Self(form_metadata))
}
/// Returns an iterator over the metadata fields.
fn iter(&self) -> std::slice::Iter<'_, (&'static str, String)> {
self.0.iter()
}
}
/// Build the upload request.
@ -758,11 +768,11 @@ async fn build_request<'a>(
registry: &DisplaySafeUrl,
client: &'a BaseClient,
credentials: &Credentials,
form_metadata: &[(&'static str, String)],
form_metadata: &FormMetadata,
reporter: Arc<impl Reporter>,
) -> Result<(RequestBuilder<'a>, usize), PublishPrepareError> {
let mut form = reqwest::multipart::Form::new();
for (key, value) in form_metadata {
for (key, value) in form_metadata.iter() {
form = form.text(*key, value.clone());
}
@ -888,16 +898,19 @@ async fn handle_response(registry: &Url, response: Response) -> Result<(), Publi
#[cfg(test)]
mod tests {
use crate::{Reporter, build_request, form_metadata};
use insta::{assert_debug_snapshot, assert_snapshot};
use itertools::Itertools;
use std::path::PathBuf;
use std::sync::Arc;
use insta::{assert_debug_snapshot, assert_snapshot};
use itertools::Itertools;
use uv_auth::Credentials;
use uv_client::BaseClientBuilder;
use uv_distribution_filename::DistFilename;
use uv_redacted::DisplaySafeUrl;
use crate::{FormMetadata, Reporter, build_request};
struct DummyReporter;
impl Reporter for DummyReporter {
@ -916,7 +929,9 @@ mod tests {
let file = PathBuf::from("../../scripts/links/").join(raw_filename);
let filename = DistFilename::try_from_normalized_filename(raw_filename).unwrap();
let form_metadata = form_metadata(&file, &filename).await.unwrap();
let form_metadata = FormMetadata::read_from_file(&file, &filename)
.await
.unwrap();
let formatted_metadata = form_metadata
.iter()
@ -1028,7 +1043,9 @@ mod tests {
let file = PathBuf::from("../../scripts/links/").join(raw_filename);
let filename = DistFilename::try_from_normalized_filename(raw_filename).unwrap();
let form_metadata = form_metadata(&file, &filename).await.unwrap();
let form_metadata = FormMetadata::read_from_file(&file, &filename)
.await
.unwrap();
let formatted_metadata = form_metadata
.iter()