From a999303d2fb0ccabb4f4fdf952f5708a42605af7 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 27 Aug 2024 14:46:39 -0400 Subject: [PATCH] Use `PathBuf` types in `Source` enum (#6708) --- .../uv-distribution/src/metadata/lowering.rs | 18 +++++++++++----- crates/uv-workspace/src/pyproject.rs | 21 ++++++++----------- uv.schema.json | 4 ++-- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/crates/uv-distribution/src/metadata/lowering.rs b/crates/uv-distribution/src/metadata/lowering.rs index 3d81a1a6a..26f465d23 100644 --- a/crates/uv-distribution/src/metadata/lowering.rs +++ b/crates/uv-distribution/src/metadata/lowering.rs @@ -257,6 +257,8 @@ pub enum LoweringError { EditableFile(String), #[error(transparent)] ParsedUrl(#[from] ParsedUrlError), + #[error("Path must be UTF-8: `{0}`")] + NonUtf8Path(PathBuf), #[error(transparent)] // Function attaches the context RelativeTo(io::Error), } @@ -264,7 +266,7 @@ pub enum LoweringError { /// Convert a Git source into a [`RequirementSource`]. fn git_source( git: &Url, - subdirectory: Option, + subdirectory: Option, rev: Option, tag: Option, branch: Option, @@ -282,7 +284,10 @@ fn git_source( if let Some(rev) = reference.as_str() { url.set_path(&format!("{}@{}", url.path(), rev)); } - if let Some(subdirectory) = &subdirectory { + if let Some(subdirectory) = subdirectory.as_ref() { + let subdirectory = subdirectory + .to_str() + .ok_or_else(|| LoweringError::NonUtf8Path(subdirectory.clone()))?; url.set_fragment(Some(&format!("subdirectory={subdirectory}"))); } let url = VerbatimUrl::from_url(url); @@ -294,17 +299,20 @@ fn git_source( repository, reference, precise: None, - subdirectory: subdirectory.map(PathBuf::from), + subdirectory, }) } /// Convert a URL source into a [`RequirementSource`]. -fn url_source(url: Url, subdirectory: Option) -> Result { +fn url_source(url: Url, subdirectory: Option) -> Result { let mut verbatim_url = url.clone(); if verbatim_url.fragment().is_some() { return Err(LoweringError::ForbiddenFragment(url)); } - if let Some(subdirectory) = &subdirectory { + if let Some(subdirectory) = subdirectory.as_ref() { + let subdirectory = subdirectory + .to_str() + .ok_or_else(|| LoweringError::NonUtf8Path(subdirectory.clone()))?; verbatim_url.set_fragment(Some(subdirectory)); } diff --git a/crates/uv-workspace/src/pyproject.rs b/crates/uv-workspace/src/pyproject.rs index 89c4af682..eafc6d78d 100644 --- a/crates/uv-workspace/src/pyproject.rs +++ b/crates/uv-workspace/src/pyproject.rs @@ -314,7 +314,7 @@ pub enum Source { /// The repository URL (without the `git+` prefix). git: Url, /// The path to the directory with the `pyproject.toml`, if it's not in the archive root. - subdirectory: Option, + subdirectory: Option, // Only one of the three may be used; we'll validate this later and emit a custom error. rev: Option, tag: Option, @@ -331,13 +331,13 @@ pub enum Source { url: Url, /// For source distributions, the path to the directory with the `pyproject.toml`, if it's /// not in the archive root. - subdirectory: Option, + subdirectory: Option, }, /// The path to a dependency, either a wheel (a `.whl` file), source distribution (a `.zip` or /// `.tar.gz` file), or source tree (i.e., a directory containing a `pyproject.toml` or /// `setup.py` file in the root). Path { - path: String, + path: PathBuf, /// `false` by default. editable: Option, }, @@ -355,12 +355,12 @@ pub enum Source { /// A catch-all variant used to emit precise error messages when deserializing. CatchAll { git: String, - subdirectory: Option, + subdirectory: Option, rev: Option, tag: Option, branch: Option, url: String, - patch: String, + path: PathBuf, index: String, workspace: bool, }, @@ -437,16 +437,13 @@ impl Source { editable, path: relative_to(&install_path, root) .or_else(|_| std::path::absolute(&install_path)) - .map_err(SourceError::Absolute)? - .to_str() - .ok_or_else(|| SourceError::NonUtf8Path(install_path))? - .to_string(), + .map_err(SourceError::Absolute)?, }, RequirementSource::Url { subdirectory, url, .. } => Source::Url { url: url.to_url(), - subdirectory: subdirectory.map(|path| path.to_string_lossy().into_owned()), + subdirectory, }, RequirementSource::Git { repository, @@ -470,7 +467,7 @@ impl Source { tag, branch, git: repository, - subdirectory: subdirectory.map(|path| path.to_string_lossy().into_owned()), + subdirectory, } } else { Source::Git { @@ -478,7 +475,7 @@ impl Source { tag, branch, git: repository, - subdirectory: subdirectory.map(|path| path.to_string_lossy().into_owned()), + subdirectory, } } } diff --git a/uv.schema.json b/uv.schema.json index da3e127c8..c9efb1b5e 100644 --- a/uv.schema.json +++ b/uv.schema.json @@ -1252,7 +1252,7 @@ "required": [ "git", "index", - "patch", + "path", "url", "workspace" ], @@ -1269,7 +1269,7 @@ "index": { "type": "string" }, - "patch": { + "path": { "type": "string" }, "rev": {