mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-24 22:30:03 +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]
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
|
||||
toml_edit = { version = "0.19.11", features = ["serde"] }
|
||||
binstalk-downloader = { version = "0.7.0", path = "../binstalk-downloader", default-features = false, features = ["rustls"] }
|
||||
|
||||
[features]
|
||||
git = ["simple-git"]
|
||||
|
||||
crates_io_api = []
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
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`,
|
||||
/// retrieve its Cargo.toml and infer all its bins.
|
||||
pub async fn fetch_crate_cratesio(
|
||||
pub async fn fetch_crate_cratesio_api(
|
||||
client: Client,
|
||||
name: &str,
|
||||
version_req: &VersionReq,
|
||||
|
|
|
@ -33,8 +33,10 @@ mod git_registry;
|
|||
#[cfg(feature = "git")]
|
||||
pub use git_registry::GitRegistry;
|
||||
|
||||
#[cfg(any(feature = "crates_io_api", test))]
|
||||
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;
|
||||
pub use sparse_registry::SparseRegistry;
|
||||
|
@ -100,18 +102,21 @@ impl From<CargoTomlError> for RegistryError {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
#[derive(Clone, Debug)]
|
||||
#[non_exhaustive]
|
||||
pub enum Registry {
|
||||
#[default]
|
||||
CratesIo,
|
||||
|
||||
Sparse(Arc<SparseRegistry>),
|
||||
|
||||
#[cfg(feature = "git")]
|
||||
Git(GitRegistry),
|
||||
}
|
||||
|
||||
impl Default for Registry {
|
||||
fn default() -> Self {
|
||||
Self::crates_io_sparse_registry()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, ThisError)]
|
||||
#[error("Invalid registry `{src}`, {inner}")]
|
||||
pub struct InvalidRegistryError {
|
||||
|
@ -138,6 +143,13 @@ enum InvalidRegistryErrorInner {
|
|||
}
|
||||
|
||||
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> {
|
||||
if let Some(s) = s.strip_prefix("sparse+") {
|
||||
let url = Url::parse(s)?;
|
||||
|
@ -170,7 +182,6 @@ impl Registry {
|
|||
version_req: &VersionReq,
|
||||
) -> Result<Manifest<Meta>, RegistryError> {
|
||||
match self {
|
||||
Self::CratesIo => fetch_crate_cratesio(client, crate_name, version_req).await,
|
||||
Self::Sparse(sparse_registry) => {
|
||||
sparse_registry
|
||||
.fetch_crate_matched(client, crate_name, version_req)
|
||||
|
@ -222,7 +233,7 @@ mod test {
|
|||
async fn test_crates_io_sparse_registry() {
|
||||
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!(
|
||||
matches!(sparse_registry, Registry::Sparse(_)),
|
||||
"{:?}",
|
||||
|
@ -236,8 +247,7 @@ mod test {
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
let manifest_from_cratesio_api = Registry::default()
|
||||
.fetch_crate_matched(client, crate_name, version_req)
|
||||
let manifest_from_cratesio_api = fetch_crate_cratesio_api(client, crate_name, version_req)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue