Skip to content

Commit

Permalink
binstalk-registry: Use crates.io sparse index by default (#1314)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
NobodyXu committed Aug 24, 2023
1 parent 0a71b39 commit b9adaa0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
3 changes: 3 additions & 0 deletions crates/binstalk-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion crates/binstalk-registry/src/crates_io_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 19 additions & 9 deletions crates/binstalk-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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)?;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(_)),
"{:?}",
Expand All @@ -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();

Expand Down

0 comments on commit b9adaa0

Please sign in to comment.