frewsxcv / rust-crates-index

Rust library for retrieving and interacting with the crates.io index
https://docs.rs/crates-index/
Apache License 2.0
72 stars 37 forks source link

Auto-detect index configuration (sparse or not) #128

Open obi1kenobi opened 1 year ago

obi1kenobi commented 1 year ago

Thanks again for this awesome crate, cargo-semver-checks wouldn't have been possible without it! I have a question that I couldn't figure out the answer to by looking at the docs and some of the source code.

In this cargo-semver-checks issue, a user is using an index mirror that is sparse-only and therefore our usual trick of "ignore the sparse config and use the old protocol anyway" doesn't work. How should cargo-semver-checks load crate info so as to be maximally compatible with users' local configurations?

Ideally, there would be a way for cargo-semver-checks to just load a Crate struct for a given crate name without needing to check cargo index information before deciding between Index and SparseIndex. Is this possible already, or if not, is it feasible to implement?

Barring that, is there a good and reliable way within this crate to figure out whether the configured index is sparse or not? Asking because right now in cargo-semver-checks I don't know which of Index and SparseIndex to attempt to use when. I even don't mind an approach like "try making a sparse index, and if it doesn't work, try again with non-sparse" so long as we can get a predictable error variant to match on that corresponds to this case and this case alone (as opposed to matching on a generic Error::Io or Error::Git).

Thanks again for looking into this!

Byron commented 1 year ago

I don't know if it helps, but now there is a new function to obtain the local paths of (possibly non-existing) indices, git or sparse alike.

Until there are better ways which could be brainstormed here or downright contributed, maybe the new addition helps.