mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-20 20:48:43 +00:00
binstalk-registry: Use crates.io sparse index by default (#1314)
Fixed #1310 Also add rename `fetch_crate_cratesio` => `fetch_crate_cratesio_api` and put it behind a new feature `crates_io_api`. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
0a71b39c90
commit
b9adaa006f
3 changed files with 23 additions and 10 deletions
|
@ -35,9 +35,12 @@ url = "2.3.1"
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
|
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
|
||||||
toml_edit = { version = "0.19.11", features = ["serde"] }
|
toml_edit = { version = "0.19.11", features = ["serde"] }
|
||||||
|
binstalk-downloader = { version = "0.7.0", path = "../binstalk-downloader", default-features = false, features = ["rustls"] }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
git = ["simple-git"]
|
git = ["simple-git"]
|
||||||
|
|
||||||
|
crates_io_api = []
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
rustdoc-args = ["--cfg", "docsrs"]
|
rustdoc-args = ["--cfg", "docsrs"]
|
||||||
|
|
|
@ -105,7 +105,7 @@ async fn fetch_crate_cratesio_version_matched(
|
||||||
|
|
||||||
/// Find the crate by name, get its latest stable version matches `version_req`,
|
/// Find the crate by name, get its latest stable version matches `version_req`,
|
||||||
/// retrieve its Cargo.toml and infer all its bins.
|
/// retrieve its Cargo.toml and infer all its bins.
|
||||||
pub async fn fetch_crate_cratesio(
|
pub async fn fetch_crate_cratesio_api(
|
||||||
client: Client,
|
client: Client,
|
||||||
name: &str,
|
name: &str,
|
||||||
version_req: &VersionReq,
|
version_req: &VersionReq,
|
||||||
|
|
|
@ -33,8 +33,10 @@ mod git_registry;
|
||||||
#[cfg(feature = "git")]
|
#[cfg(feature = "git")]
|
||||||
pub use git_registry::GitRegistry;
|
pub use git_registry::GitRegistry;
|
||||||
|
|
||||||
|
#[cfg(any(feature = "crates_io_api", test))]
|
||||||
mod crates_io_registry;
|
mod crates_io_registry;
|
||||||
pub use crates_io_registry::fetch_crate_cratesio;
|
#[cfg(any(feature = "crates_io_api", test))]
|
||||||
|
pub use crates_io_registry::fetch_crate_cratesio_api;
|
||||||
|
|
||||||
mod sparse_registry;
|
mod sparse_registry;
|
||||||
pub use sparse_registry::SparseRegistry;
|
pub use sparse_registry::SparseRegistry;
|
||||||
|
@ -100,18 +102,21 @@ impl From<CargoTomlError> for RegistryError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum Registry {
|
pub enum Registry {
|
||||||
#[default]
|
|
||||||
CratesIo,
|
|
||||||
|
|
||||||
Sparse(Arc<SparseRegistry>),
|
Sparse(Arc<SparseRegistry>),
|
||||||
|
|
||||||
#[cfg(feature = "git")]
|
#[cfg(feature = "git")]
|
||||||
Git(GitRegistry),
|
Git(GitRegistry),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Registry {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::crates_io_sparse_registry()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, ThisError)]
|
#[derive(Debug, ThisError)]
|
||||||
#[error("Invalid registry `{src}`, {inner}")]
|
#[error("Invalid registry `{src}`, {inner}")]
|
||||||
pub struct InvalidRegistryError {
|
pub struct InvalidRegistryError {
|
||||||
|
@ -138,6 +143,13 @@ enum InvalidRegistryErrorInner {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Registry {
|
impl Registry {
|
||||||
|
/// Return a crates.io sparse registry
|
||||||
|
pub fn crates_io_sparse_registry() -> Self {
|
||||||
|
Self::Sparse(Arc::new(SparseRegistry::new(
|
||||||
|
Url::parse("https://index.crates.io/").unwrap(),
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
|
||||||
fn from_str_inner(s: &str) -> Result<Self, InvalidRegistryErrorInner> {
|
fn from_str_inner(s: &str) -> Result<Self, InvalidRegistryErrorInner> {
|
||||||
if let Some(s) = s.strip_prefix("sparse+") {
|
if let Some(s) = s.strip_prefix("sparse+") {
|
||||||
let url = Url::parse(s)?;
|
let url = Url::parse(s)?;
|
||||||
|
@ -170,7 +182,6 @@ impl Registry {
|
||||||
version_req: &VersionReq,
|
version_req: &VersionReq,
|
||||||
) -> Result<Manifest<Meta>, RegistryError> {
|
) -> Result<Manifest<Meta>, RegistryError> {
|
||||||
match self {
|
match self {
|
||||||
Self::CratesIo => fetch_crate_cratesio(client, crate_name, version_req).await,
|
|
||||||
Self::Sparse(sparse_registry) => {
|
Self::Sparse(sparse_registry) => {
|
||||||
sparse_registry
|
sparse_registry
|
||||||
.fetch_crate_matched(client, crate_name, version_req)
|
.fetch_crate_matched(client, crate_name, version_req)
|
||||||
|
@ -222,7 +233,7 @@ mod test {
|
||||||
async fn test_crates_io_sparse_registry() {
|
async fn test_crates_io_sparse_registry() {
|
||||||
let client = create_client().await;
|
let client = create_client().await;
|
||||||
|
|
||||||
let sparse_registry: Registry = "sparse+https://index.crates.io/".parse().unwrap();
|
let sparse_registry: Registry = Registry::crates_io_sparse_registry();
|
||||||
assert!(
|
assert!(
|
||||||
matches!(sparse_registry, Registry::Sparse(_)),
|
matches!(sparse_registry, Registry::Sparse(_)),
|
||||||
"{:?}",
|
"{:?}",
|
||||||
|
@ -236,8 +247,7 @@ mod test {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let manifest_from_cratesio_api = Registry::default()
|
let manifest_from_cratesio_api = fetch_crate_cratesio_api(client, crate_name, version_req)
|
||||||
.fetch_crate_matched(client, crate_name, version_req)
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue