mod git;
mod local;
mod registry;
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 = "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<()>;
fn purge_from_cache(&self, workspace: &Workspace) -> anyhow::Result<()>;
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> anyhow::Result<()>;
}
enum CrateType {
Registry(registry::RegistryCrate),
Git(git::GitRepo),
Local(local::Local),
}
pub struct Crate(CrateType);
impl Crate {
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,
))))
}
#[cfg(feature = "git-registries")]
pub fn git_registry(registry: GitRegistry, name: &str, version: &str) -> Self {
Crate(CrateType::Registry(registry::RegistryCrate::new(
registry::Registry::Git(registry),
name,
version,
)))
}
#[cfg(feature = "git-registries")]
pub fn registry(registry: AlternativeRegistry, name: &str, version: &str) -> Self {
Self::git_registry(registry, name, version)
}
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")
}
pub fn git(url: &str) -> Self {
Crate(CrateType::Git(git::GitRepo::new(url)))
}
pub fn local(path: &Path) -> Self {
Crate(CrateType::Local(local::Local::new(path)))
}
#[cfg_attr(
feature = "tracing",
tracing::instrument(skip_all, fields(krate = %self))
)]
pub fn fetch(&self, workspace: &Workspace) -> anyhow::Result<()> {
self.as_trait().fetch(workspace)
}
#[cfg_attr(
feature = "tracing",
tracing::instrument(skip_all, fields(krate = %self))
)]
pub fn purge_from_cache(&self, workspace: &Workspace) -> anyhow::Result<()> {
self.as_trait().purge_from_cache(workspace)
}
pub fn git_commit(&self, workspace: &Workspace) -> Option<String> {
if let CrateType::Git(repo) = &self.0 {
repo.git_commit(workspace)
} else {
None
}
}
#[cfg_attr(
feature = "tracing",
tracing::instrument(skip_all, fields(krate = %self, dest = %dest.display()))
)]
pub fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> anyhow::Result<()> {
if dest.exists() {
info!(
"crate source directory {} already exists, cleaning it up",
dest.display()
);
crate::utils::remove_dir_all(dest)?;
}
self.as_trait().copy_source_to(workspace, dest)
}
fn as_trait(&self) -> &dyn CrateTrait {
match &self.0 {
CrateType::Registry(krate) => krate,
CrateType::Git(repo) => repo,
CrateType::Local(local) => local,
}
}
}
impl std::fmt::Display for Crate {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.as_trait())
}
}