use std::env;
use std::path::{Path, PathBuf};
use crate::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.is_empty() {
let mut bin_list: Vec<_> = manifest
.bin
.iter()
.filter(|b| b.doc)
.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())
}