Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Impl support for alternative registries #1184

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/bin/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
};

use binstalk::{
drivers::Registry,
helpers::remote,
manifests::cargo_toml_binstall::PkgFmt,
ops::resolve::{CrateName, VersionReqExt},
Expand Down Expand Up @@ -222,6 +223,10 @@ pub struct Args {
#[clap(help_heading = "Options", long, alias = "roots")]
pub root: Option<PathBuf>,

/// The URL of the registry index to use
#[clap(help_heading = "Options", long)]
pub index: Option<Registry>,

/// This option will be passed through to all `cargo-install` invocations.
///
/// It will require `Cargo.lock` to be up to date.
Expand Down
2 changes: 1 addition & 1 deletion crates/bin/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub fn install_crates(
client,
gh_api_client,
jobserver_client,
crates_io_rate_limit: Default::default(),
registry: args.index.unwrap_or_default(),
});

// Destruct args before any async function to reduce size of the future
Expand Down
7 changes: 7 additions & 0 deletions crates/binstalk-downloader/src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ pub struct HttpError {
err: reqwest::Error,
}

impl HttpError {
/// Returns true if the error is from [`Response::error_for_status`].
pub fn is_status(&self) -> bool {
self.err.is_status()
}
}

#[derive(Debug)]
struct Inner {
client: reqwest::Client,
Expand Down
4 changes: 4 additions & 0 deletions crates/binstalk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ normalize-path = { version = "0.2.1", path = "../normalize-path" }
once_cell = "1.18.0"
semver = { version = "1.0.17", features = ["serde"] }
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.99"
strum = "0.25.0"
target-lexicon = { version = "0.12.8", features = ["std"] }
tempfile = "3.5.0"
Expand All @@ -41,6 +42,9 @@ tracing = "0.1.37"
url = { version = "2.3.1", features = ["serde"] }
xz2 = "0.1.7"

[dev-dependencies]
toml_edit = { version = "0.19.11", features = ["serde"] }

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.48.0", features = ["Win32_Storage_FileSystem", "Win32_Foundation"] }

Expand Down
10 changes: 8 additions & 2 deletions crates/binstalk/src/drivers.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
mod crates_io;
pub use crates_io::fetch_crate_cratesio;
mod registry;
pub use registry::{
fetch_crate_cratesio, CratesIoRateLimit, InvalidRegistryError, Registry, RegistryError,
SparseRegistry,
};

#[cfg(feature = "git")]
pub use registry::GitRegistry;
252 changes: 252 additions & 0 deletions crates/binstalk/src/drivers/registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
use std::{str::FromStr, sync::Arc};

use cargo_toml::Manifest;
use compact_str::CompactString;
use leon::{ParseError, RenderError};
use miette::Diagnostic;
use semver::VersionReq;
use serde_json::Error as JsonError;
use thiserror::Error as ThisError;

use crate::{
errors::BinstallError,
helpers::remote::{Client, Error as RemoteError, Url, UrlParseError},
manifests::cargo_toml_binstall::Meta,
};

#[cfg(feature = "git")]
use crate::helpers::git::{GitUrl, GitUrlParseError};

mod vfs;

mod visitor;

mod common;
use common::*;

#[cfg(feature = "git")]
mod git_registry;
#[cfg(feature = "git")]
pub use git_registry::GitRegistry;

mod crates_io_registry;
pub use crates_io_registry::{fetch_crate_cratesio, CratesIoRateLimit};

mod sparse_registry;
pub use sparse_registry::SparseRegistry;

#[derive(Debug, ThisError, Diagnostic)]
#[diagnostic(severity(error), code(binstall::cargo_registry))]
#[non_exhaustive]
pub enum RegistryError {
#[error(transparent)]
Remote(#[from] RemoteError),

#[error("{0} is not found")]
#[diagnostic(
help("Check that the crate name you provided is correct.\nYou can also search for a matching crate at: https://lib.rs/search?q={0}")
)]
NotFound(CompactString),

#[error(transparent)]
Json(#[from] JsonError),

#[error("Failed to parse dl config: {0}")]
ParseDlConfig(#[from] ParseError),

#[error("Failed to render dl config: {0}")]
RenderDlConfig(#[from] RenderError),
}

#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Registry {
CratesIo(Arc<CratesIoRateLimit>),

Sparse(Arc<SparseRegistry>),

#[cfg(feature = "git")]
Git(GitRegistry),
}

impl Default for Registry {
fn default() -> Self {
Self::CratesIo(Default::default())
}
}

#[derive(Debug, ThisError)]
#[error("Invalid registry `{src}`, {inner}")]
pub struct InvalidRegistryError {
src: CompactString,
#[source]
inner: InvalidRegistryErrorInner,
}

#[derive(Debug, ThisError)]
enum InvalidRegistryErrorInner {
#[cfg(feature = "git")]
#[error("failed to parse git url {0}")]
GitUrlParseErr(#[from] Box<GitUrlParseError>),

#[error("failed to parse sparse registry url: {0}")]
UrlParseErr(#[from] UrlParseError),

#[error("expected protocol http(s), actual protocl {0}")]
InvalidScheme(CompactString),

#[cfg(not(feature = "git"))]
#[error("git registry not supported")]
GitRegistryNotSupported,
}

impl Registry {
fn from_str_inner(s: &str) -> Result<Self, InvalidRegistryErrorInner> {
if let Some(s) = s.strip_prefix("sparse+") {
let url = Url::parse(s)?;

let scheme = url.scheme();
if scheme != "http" && scheme != "https" {
Err(InvalidRegistryErrorInner::InvalidScheme(scheme.into()))
} else {
Ok(Self::Sparse(Arc::new(SparseRegistry::new(url))))
}
} else {
#[cfg(not(feature = "git"))]
{
Err(InvalidRegistryErrorInner::GitRegistryNotSupported)
}
#[cfg(feature = "git")]
{
let url = GitUrl::from_str(s).map_err(Box::new)?;
Ok(Self::Git(GitRegistry::new(url)))
}
}
}

/// Fetch the latest crate with `crate_name` and with version matching
/// `version_req`.
pub async fn fetch_crate_matched(
&self,
client: Client,
crate_name: &str,
version_req: &VersionReq,
) -> Result<Manifest<Meta>, BinstallError> {
match self {
Self::CratesIo(rate_limit) => {
fetch_crate_cratesio(client, crate_name, version_req, rate_limit).await
}
Self::Sparse(sparse_registry) => {
sparse_registry
.fetch_crate_matched(client, crate_name, version_req)
.await
}
#[cfg(feature = "git")]
Self::Git(git_registry) => {
git_registry
.fetch_crate_matched(client, crate_name, version_req)
.await
}
}
}
}

impl FromStr for Registry {
type Err = InvalidRegistryError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_str_inner(s).map_err(|inner| InvalidRegistryError {
src: s.into(),
inner,
})
}
}

#[cfg(test)]
mod test {
use std::time::Duration;

use toml_edit::ser::to_string;

use super::*;

/// Mark this as an async fn so that you won't accidentally use it in
/// sync context.
async fn create_client() -> Client {
Client::new(
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")),
None,
Duration::from_millis(10),
1.try_into().unwrap(),
[],
)
.unwrap()
}

#[tokio::test]
async fn test_crates_io_sparse_registry() {
let client = create_client().await;

let sparse_registry: Registry = "sparse+https://index.crates.io/".parse().unwrap();
assert!(
matches!(sparse_registry, Registry::Sparse(_)),
"{:?}",
sparse_registry
);

let crate_name = "cargo-binstall";
let version_req = &VersionReq::parse("=1.0.0").unwrap();
let manifest_from_sparse = sparse_registry
.fetch_crate_matched(client.clone(), crate_name, version_req)
.await
.unwrap();

let manifest_from_cratesio_api = Registry::default()
.fetch_crate_matched(client, crate_name, version_req)
.await
.unwrap();

let serialized_manifest_from_sparse = to_string(&manifest_from_sparse).unwrap();
let serialized_manifest_from_cratesio_api = to_string(&manifest_from_cratesio_api).unwrap();

assert_eq!(
serialized_manifest_from_sparse,
serialized_manifest_from_cratesio_api
);
}

#[cfg(feature = "git")]
#[tokio::test]
async fn test_crates_io_git_registry() {
let client = create_client().await;

let git_registry: Registry = "https://github.com/rust-lang/crates.io-index"
.parse()
.unwrap();
assert!(
matches!(git_registry, Registry::Git(_)),
"{:?}",
git_registry
);

let crate_name = "cargo-binstall";
let version_req = &VersionReq::parse("=1.0.0").unwrap();
let manifest_from_git = git_registry
.fetch_crate_matched(client.clone(), crate_name, version_req)
.await
.unwrap();

let manifest_from_cratesio_api = Registry::default()
.fetch_crate_matched(client, crate_name, version_req)
.await
.unwrap();

let serialized_manifest_from_git = to_string(&manifest_from_git).unwrap();
let serialized_manifest_from_cratesio_api = to_string(&manifest_from_cratesio_api).unwrap();

assert_eq!(
serialized_manifest_from_git,
serialized_manifest_from_cratesio_api
);
}
}
Loading