use std::env;
use std::path::PathBuf;
use rust_util::XResult;
pub enum Builder {
Cargo,
Unknown,
}
impl Builder {
pub fn from(opt_current_dir: &Option<String>) -> XResult<Self> {
inner_get_builder(opt_current_dir)
}
pub fn get_name(&self) -> Option<String> {
match self {
Builder::Cargo => Some("cargo".into()),
Builder::Unknown => None,
}
}
}
fn inner_get_builder(opt_current_dir: &Option<String>) -> XResult<Builder> {
let work_dir = get_work_dir(opt_current_dir)?;
let work_dir_path_buf = PathBuf::from(&work_dir);
let check_files_exists = |f: &str| -> bool {
let mut work_dir_path_buf_f = work_dir_path_buf.clone();
work_dir_path_buf_f.push(f);
work_dir_path_buf_f.exists()
};
if check_files_exists("Cargo.toml") {
return Ok(Builder::Cargo);
}
Ok(Builder::Unknown)
}
pub fn get_work_dir(opt_current_dir: &Option<String>) -> XResult<String> {
match opt_current_dir {
Some(dir) => Ok(dir.clone()), None => match env::current_dir() {
Ok(dir) => match dir.as_path().to_str() {
Some(dir) => Ok(dir.into()), None => Err(rust_util::new_box_error("Error in get current dir!")),
},
Err(e) => Err(rust_util::new_box_error(&format!("Error in get current dir: {}", e))),
},
}
}