Remove URL clone in requirements-txt parser (#1020)

This commit is contained in:
Charlie Marsh 2024-01-19 17:30:17 -05:00 committed by GitHub
parent b3954f2449
commit 69d2791a43
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 5 deletions

View file

@ -37,7 +37,7 @@ impl VerbatimUrl {
}
/// Parse a URL from a path.
pub fn from_path(path: impl AsRef<str>, working_dir: impl AsRef<Path>, given: String) -> Self {
pub fn from_path(path: impl AsRef<str>, working_dir: impl AsRef<Path>) -> 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
}
}

View file

@ -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 })
}
}