mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-06-14 14:46:37 +00:00
Fix Registery::crate_source
Match sparse/git crates.io registry to standardrised `CrateSource::cratesio_registry()`. Also optimize it to avoid unnecessary `.to_string()` and `Url::parse` for sparse registry. Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>
This commit is contained in:
parent
31a88f4124
commit
9298511d05
1 changed files with 24 additions and 23 deletions
|
@ -201,40 +201,41 @@ impl Registry {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get url of the regsitry
|
/// Get url of the regsitry
|
||||||
pub fn url(&self) -> impl fmt::Display + '_ {
|
pub fn url(&self) -> Result<MaybeOwned<'_, Url>, UrlParseError> {
|
||||||
#[cfg(feature = "git")]
|
|
||||||
return match self {
|
|
||||||
Registry::Git(registry) => either::Left(registry.url()),
|
|
||||||
Registry::Sparse(registry) => either::Right(registry.url()),
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(not(feature = "git"))]
|
|
||||||
match self {
|
match self {
|
||||||
Registry::Sparse(registry) => registry.url(),
|
#[cfg(feature = "git")]
|
||||||
|
Registry::Git(registry) => Url::parse(®istry.url().to_string()).map_ok(MaybeOwned::Owned),
|
||||||
|
Registry::Sparse(registry) => Ok(MaybeOwned::Borrowed(registry.url())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get crate source of this registry
|
/// Get crate source of this registry
|
||||||
pub fn crate_source(&self) -> Result<CrateSource, UrlParseError> {
|
pub fn crate_source(&self) -> Result<CrateSource, UrlParseError> {
|
||||||
let registry = self.to_string();
|
let registry = self.url()?;
|
||||||
Ok(if registry == "https://index.crates.io/" {
|
let source_type = match self {
|
||||||
CrateSource::cratesio_registry()
|
#[cfg(feature = "git")]
|
||||||
} else {
|
Registry::Git(_) => SourceType::Git,
|
||||||
CrateSource {
|
Registry::Sparse(_) => SourceType::Sparse,
|
||||||
source_type: match self {
|
};
|
||||||
#[cfg(feature = "git")]
|
|
||||||
Registry::Git(_) => SourceType::Git,
|
match (registry.as_str(), source_type) {
|
||||||
Registry::Sparse(_) => SourceType::Sparse,
|
("https://index.crates.io/", SourceType::Sparse) |
|
||||||
},
|
("https://github.com/rust-lang/crates.io-index", SourceType::Git) => CrateSource::cratesio_registry(),
|
||||||
url: MaybeOwned::Owned(Url::parse(®istry)?),
|
_ => CrateSource {
|
||||||
}
|
source_type,
|
||||||
})
|
url: MaybeOwned::Owned(registry.into_owned()),
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Registry {
|
impl fmt::Display for Registry {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
fmt::Display::fmt(&self.url(), f)
|
match self {
|
||||||
|
#[cfg(feature = "git")]
|
||||||
|
Registry::Git(registry) => fmt::Display::fmt(®istry.url(), fmt),
|
||||||
|
Registry::Sparse(registry) => fmt::Display::fmt(®istry.url(), fmt),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue