cargo_readme/config/
project.rs1use std::env;
2use std::path::{Path, PathBuf};
3
4use crate::config::manifest::{Manifest, ManifestLib};
5
6pub fn get_root(given_root: Option<&str>) -> Result<PathBuf, String> {
12 let current_dir = env::current_dir().map_err(|e| format!("{}", e))?;
13 let root = match given_root {
14 Some(root) => {
15 let root = Path::new(root);
16 if root.is_absolute() {
17 root.to_path_buf()
18 } else {
19 current_dir.join(root)
20 }
21 }
22 None => current_dir,
23 };
24
25 if !root.join("Cargo.toml").is_file() {
26 return Err(format!(
27 "`{:?}` does not look like a Rust/Cargo project",
28 root
29 ));
30 }
31
32 Ok(root)
33}
34
35pub fn find_entrypoint(current_dir: &Path, manifest: &Manifest) -> Result<PathBuf, String> {
44 let lib_rs = current_dir.join("src/lib.rs");
46 if lib_rs.exists() {
47 return Ok(lib_rs);
48 }
49
50 let main_rs = current_dir.join("src/main.rs");
52 if main_rs.exists() {
53 return Ok(main_rs);
54 }
55
56 if let Some(ManifestLib {
58 path: ref lib,
59 doc: true,
60 }) = manifest.lib
61 {
62 return Ok(lib.to_path_buf());
63 }
64
65 if manifest.bin.len() > 0 {
67 let mut bin_list: Vec<_> = manifest
68 .bin
69 .iter()
70 .filter(|b| b.doc == true)
71 .map(|b| b.path.clone())
72 .collect();
73
74 if bin_list.len() > 1 {
75 let paths = bin_list
76 .iter()
77 .map(|p| p.to_string_lossy())
78 .collect::<Vec<_>>()
79 .join(", ");
80 return Err(format!("Multiple binaries found, choose one: [{}]", paths));
81 }
82
83 if let Some(bin) = bin_list.pop() {
84 return Ok(bin);
85 }
86 }
87
88 Err("No entrypoint found".to_owned())
90}