Skip to content
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
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["alternate-registries"]
default = ["git-registries"]

alternate-registries = ["dep:git2"]
git-registries = ["dep:git2"]
alternate-registries = ["git-registries"]
unstable = []
unstable-toolchain-ci = []
tracing = ["dep:tracing"]
Expand All @@ -35,6 +36,7 @@ flate2 = "1"
tar = "0.4.0"
percent-encoding = "2.1.0"
walkdir = "2.2"
url = "2.5"
toml = "1.1.2"
remove_dir_all = "1.0.0"
base64 = "0.23.0"
Expand All @@ -54,3 +56,4 @@ env_logger = "0.11.3"
rand = "0.10.0"
test-case = "3.3.1"
tiny_http = "0.12.0"
mockito = "1"
51 changes: 39 additions & 12 deletions src/crates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ mod git;
mod local;
mod registry;

use crate::Workspace;
use crate::{Workspace, crates::registry::CRATES_IO_SPARSE_INDEX};
use anyhow::{Context as _, Result};
use log::info;
use std::path::Path;
use url::Url;

#[cfg(feature = "alternate-registries")]
pub use registry::AlternativeRegistry;
#[cfg(feature = "git-registries")]
pub use registry::GitRegistry;
#[cfg(feature = "git-registries")]
pub use registry::GitRegistry as AlternativeRegistry;

trait CrateTrait: std::fmt::Display {
fn fetch(&self, workspace: &Workspace) -> anyhow::Result<()>;
Expand All @@ -25,25 +29,48 @@ enum CrateType {
pub struct Crate(CrateType);

impl Crate {
/// Load a crate from specified registry.
#[cfg(feature = "alternate-registries")]
pub fn registry(registry: AlternativeRegistry, name: &str, version: &str) -> Self {
Crate(CrateType::Registry(registry::RegistryCrate::new(
registry::Registry::Alternative(registry),
/// Load a crate from a sparse registry index.
///
/// `index` is the URL of the registry index, with or without Cargo's `sparse+` prefix.
/// Its `config.json` is cached in the workspace after the first fetch.
pub fn sparse_registry<U>(index: U, name: &str, version: &str) -> Result<Self>
where
U: TryInto<Url>,
<U as TryInto<Url>>::Error: std::error::Error + Send + Sync + 'static,
{
let index = index.try_into().context("invalid index url")?;

Ok(Crate(CrateType::Registry(registry::RegistryCrate::new(
registry::Registry::Sparse(registry::normalize_sparse_index(index)?),
name,
version,
)))
))))
}

/// Load a crate from the [crates.io registry](https://crates.io).
pub fn crates_io(name: &str, version: &str) -> Self {
/// Load a crate from a Git-indexed registry.
///
/// For an HTTP sparse index, use [`Crate::sparse_registry`].
#[cfg(feature = "git-registries")]
pub fn git_registry(registry: GitRegistry, name: &str, version: &str) -> Self {
Crate(CrateType::Registry(registry::RegistryCrate::new(
registry::Registry::CratesIo,
registry::Registry::Git(registry),
name,
version,
)))
}

/// Compatibility alias for [`Crate::git_registry`].
#[cfg(feature = "git-registries")]
pub fn registry(registry: AlternativeRegistry, name: &str, version: &str) -> Self {
Self::git_registry(registry, name, version)
}

/// Load a crate from the [crates.io registry](https://crates.io).
pub fn crates_io(name: &str, version: &str) -> Self {
Self::sparse_registry(CRATES_IO_SPARSE_INDEX.clone(), name, version)
.expect("we know crates.io index url is valid")
}

/// Load a crate from a git repository. The full URL needed to clone the repo has to be
/// provided.
pub fn git(url: &str) -> Self {
Expand Down
Loading