Improve non-git error message (#3403)

The boxing changes are due to clippy
This commit is contained in:
konsti 2024-05-06 13:28:05 +02:00 committed by GitHub
parent d0c3146ef6
commit 098944fc7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 36 additions and 18 deletions

View file

@ -8,8 +8,12 @@ use uv_git::{GitSha, GitUrl};
#[derive(Debug, Error)]
pub enum ParsedUrlError {
#[error("Unsupported URL prefix `{prefix}` in URL: `{url}`")]
UnsupportedUrlPrefix { prefix: String, url: Url },
#[error("Unsupported URL prefix `{prefix}` in URL: `{url}` ({message})")]
UnsupportedUrlPrefix {
prefix: String,
url: Url,
message: &'static str,
},
#[error("Invalid path in file URL: `{0}`")]
InvalidFileUrl(Url),
#[error("Failed to parse Git reference from URL: `{0}`")]
@ -123,9 +127,25 @@ impl TryFrom<Url> for ParsedUrl {
if let Some((prefix, ..)) = url.scheme().split_once('+') {
match prefix {
"git" => Ok(Self::Git(ParsedGitUrl::try_from(url)?)),
"bzr" => Err(ParsedUrlError::UnsupportedUrlPrefix {
prefix: prefix.to_string(),
url: url.clone(),
message: "Bazaar is not supported",
}),
"hg" => Err(ParsedUrlError::UnsupportedUrlPrefix {
prefix: prefix.to_string(),
url: url.clone(),
message: "Mercurial is not supported",
}),
"svn" => Err(ParsedUrlError::UnsupportedUrlPrefix {
prefix: prefix.to_string(),
url: url.clone(),
message: "Subversion is not supported",
}),
_ => Err(ParsedUrlError::UnsupportedUrlPrefix {
prefix: prefix.to_string(),
url: url.clone(),
message: "Unknown scheme",
}),
}
} else if url.scheme().eq_ignore_ascii_case("file") {

View file

@ -41,7 +41,7 @@ impl Requirement {
}
/// Convert a [`pep508_rs::Requirement`] to a [`Requirement`].
pub fn from_pep508(requirement: pep508_rs::Requirement) -> Result<Self, ParsedUrlError> {
pub fn from_pep508(requirement: pep508_rs::Requirement) -> Result<Self, Box<ParsedUrlError>> {
let source = match requirement.version_or_url {
None => RequirementSource::Registry {
specifier: VersionSpecifiers::empty(),

View file

@ -59,7 +59,7 @@ impl UnresolvedRequirement {
}
/// Return the version specifier or URL for the requirement.
pub fn source(&self) -> Result<Cow<'_, RequirementSource>, ParsedUrlError> {
pub fn source(&self) -> Result<Cow<'_, RequirementSource>, Box<ParsedUrlError>> {
// TODO(konsti): This is a bad place to raise errors, we should have parsed the url earlier.
match self {
Self::Named(requirement) => Ok(Cow::Borrowed(&requirement.source)),