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
use std::env;
use std::path::{Path, PathBuf};
use ::config::manifest::{Manifest, ManifestLib};
pub fn get_root(given_root: Option<&str>) -> Result<PathBuf, String> {
let current_dir = env::current_dir().map_err(|e| format!("{}", e))?;
let root = match given_root {
Some(root) => {
let root = Path::new(root);
if root.is_absolute() {
root.to_path_buf()
} else {
current_dir.join(root)
}
}
None => current_dir,
};
if !root.join("Cargo.toml").is_file() {
return Err(format!(
"`{:?}` does not look like a Rust/Cargo project",
root
));
}
Ok(root)
}
pub fn find_entrypoint(current_dir: &Path, manifest: &Manifest) -> Result<PathBuf, String> {
let lib_rs = current_dir.join("src/lib.rs");
if lib_rs.exists() {
return Ok(lib_rs);
}
let main_rs = current_dir.join("src/main.rs");
if main_rs.exists() {
return Ok(main_rs);
}
if let Some(ManifestLib { path: ref lib, doc: true }) = manifest.lib {
return Ok(lib.to_path_buf())
}
if manifest.bin.len() > 0 {
let mut bin_list: Vec<_> = manifest.bin.iter()
.filter(|b| b.doc == true)
.map(|b| b.path.clone())
.collect();
if bin_list.len() > 1 {
let paths = bin_list.iter().map(|p| p.to_string_lossy()).collect::<Vec<_>>().join(", ");
return Err(format!("Multiple binaries found, choose one: [{}]", paths));
}
if let Some(bin) = bin_list.pop() {
return Ok(bin);
}
}
Err("No entrypoint found".to_owned())
}