diff --git a/crates/binstalk-registry/Cargo.toml b/crates/binstalk-registry/Cargo.toml index c0fc653e..91f0c4ab 100644 --- a/crates/binstalk-registry/Cargo.toml +++ b/crates/binstalk-registry/Cargo.toml @@ -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"] diff --git a/crates/binstalk-registry/src/crates_io_registry.rs b/crates/binstalk-registry/src/crates_io_registry.rs index ce8bbdcd..179b91fd 100644 --- a/crates/binstalk-registry/src/crates_io_registry.rs +++ b/crates/binstalk-registry/src/crates_io_registry.rs @@ -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, diff --git a/crates/binstalk-registry/src/lib.rs b/crates/binstalk-registry/src/lib.rs index f3cebc85..30923f6a 100644 --- a/crates/binstalk-registry/src/lib.rs +++ b/crates/binstalk-registry/src/lib.rs @@ -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 for RegistryError { } } -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug)] #[non_exhaustive] pub enum Registry { - #[default] - CratesIo, - Sparse(Arc), #[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 { if let Some(s) = s.strip_prefix("sparse+") { let url = Url::parse(s)?; @@ -170,7 +182,6 @@ impl Registry { version_req: &VersionReq, ) -> Result, 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();