From 69d2791a43f887a86c35d06e32d6852dbf17b61a Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 19 Jan 2024 17:30:17 -0500 Subject: [PATCH] Remove URL clone in requirements-txt parser (#1020) --- crates/pep508-rs/src/verbatim_url.rs | 10 ++++++++-- crates/requirements-txt/src/lib.rs | 9 ++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/crates/pep508-rs/src/verbatim_url.rs b/crates/pep508-rs/src/verbatim_url.rs index 3607b46d3..7dcfa5615 100644 --- a/crates/pep508-rs/src/verbatim_url.rs +++ b/crates/pep508-rs/src/verbatim_url.rs @@ -37,7 +37,7 @@ impl VerbatimUrl { } /// Parse a URL from a path. - pub fn from_path(path: impl AsRef, working_dir: impl AsRef, given: String) -> Self { + pub fn from_path(path: impl AsRef, working_dir: impl AsRef) -> Self { // Expand any environment variables. let path = PathBuf::from(expand_env_vars(path.as_ref(), false).as_ref()); @@ -54,9 +54,15 @@ impl VerbatimUrl { // Convert to a URL. let url = Url::from_file_path(path).expect("path is absolute"); + Self { url, given: None } + } + + /// Set the verbatim representation of the URL. + #[must_use] + pub fn with_given(self, given: String) -> Self { Self { - url, given: Some(given), + ..self } } diff --git a/crates/requirements-txt/src/lib.rs b/crates/requirements-txt/src/lib.rs index bc5663755..3f1a8cbdf 100644 --- a/crates/requirements-txt/src/lib.rs +++ b/crates/requirements-txt/src/lib.rs @@ -155,10 +155,10 @@ impl ParsedEditableRequirement { if scheme == "file" { if let Some(path) = path.strip_prefix("//") { // Ex) `file:///home/ferris/project/scripts/...` - VerbatimUrl::from_path(path, working_dir, given.clone()) + VerbatimUrl::from_path(path, working_dir) } else { // Ex) `file:../editable/` - VerbatimUrl::from_path(path, working_dir, given.clone()) + VerbatimUrl::from_path(path, working_dir) } } else { // Ex) `https://...` @@ -168,7 +168,7 @@ impl ParsedEditableRequirement { } } else { // Ex) `../editable/` - VerbatimUrl::from_path(&given, working_dir, given.clone()) + VerbatimUrl::from_path(&given, working_dir) }; // Create a `PathBuf`. @@ -176,6 +176,9 @@ impl ParsedEditableRequirement { .to_file_path() .map_err(|()| RequirementsTxtParserError::InvalidEditablePath(given.clone()))?; + // Add the verbatim representation of the URL to the `VerbatimUrl`. + let url = url.with_given(given); + Ok(EditableRequirement { url, path }) } }