-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmod.rs
100 lines (86 loc) · 3.16 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
mod git;
mod local;
mod registry;
use crate::Workspace;
use failure::Error;
use log::info;
use std::path::Path;
trait CrateTrait: std::fmt::Display {
fn fetch(&self, workspace: &Workspace) -> Result<(), Error>;
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error>;
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<(), Error>;
}
enum CrateType {
Registry(registry::RegistryCrate),
Git(git::GitRepo),
Local(local::Local),
}
/// A Rust crate that can be used with rustwide.
pub struct Crate(CrateType);
impl Crate {
/// Load a crate from specified registry.
pub fn registry(registry_index: &str, name: &str, version: &str) -> Self {
Crate(CrateType::Registry(registry::RegistryCrate::new(
registry::Registry::Alternative(registry::AlternativeRegistry::new(registry_index)),
name,
version,
)))
}
/// Load a crate from the [crates.io registry](https://crates.io).
pub fn crates_io(name: &str, version: &str) -> Self {
Crate(CrateType::Registry(registry::RegistryCrate::new(
registry::Registry::CratesIo,
name,
version,
)))
}
/// 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 {
Crate(CrateType::Git(git::GitRepo::new(url)))
}
/// Load a crate from a directory in the local filesystem.
pub fn local(path: &Path) -> Self {
Crate(CrateType::Local(local::Local::new(path)))
}
/// Fetch the crate's source code and cache it in the workspace. This method will reach out to
/// the network for some crate types.
pub fn fetch(&self, workspace: &Workspace) -> Result<(), Error> {
self.as_trait().fetch(workspace)
}
/// Remove the cached copy of this crate. The method will do nothing if the crate isn't cached.
pub fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
self.as_trait().purge_from_cache(workspace)
}
/// Get this crate's git commit. This method is best-effort, and currently works just for git
/// crates. If the commit can't be retrieved `None` will be returned.
pub fn git_commit(&self, workspace: &Workspace) -> Option<String> {
if let CrateType::Git(repo) = &self.0 {
repo.git_commit(workspace)
} else {
None
}
}
pub(crate) fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<(), Error> {
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())
}
}