use std::path::PathBuf; use std::ffi::OsString; use std::collections::HashMap; //use crate::log; use crate::dvcs; use crate::db; use crate::db::{Entry,is_executable}; #[derive(Debug)] pub struct Task { pub name: OsString, pub bin: PathBuf, pub source: Option, pub dvcs: dvcs::Backend, pub config: HashMap, pub branch: Option, pub host: Option, pub cloned: bool, } impl Task { pub fn from_entry(entry: &Entry) -> Task { let source = entry.read_setting("source"); let cloned = source.clone().map_or(false, |_| { let mut path = entry.base_dir.clone(); path.push(format!(".{}", entry.name.to_str().expect("WTF"))); path.is_dir() }); Task { name: entry.name.clone(), bin: entry.path.clone(), source, dvcs: dvcs::from_setting(entry.read_setting("dvcs")), config: HashMap::new(), branch: entry.read_setting("branch"), host: entry.read_setting("host"), cloned, } } } // Takes an already instanced database (ie Vec) // to turn into a dictionary of Tasks pub fn from_entries(db: Vec) -> HashMap { let mut res: HashMap = HashMap::new(); for entry in db { let task = Task::from_entry(&entry); res.insert( task.name.clone().into_string().expect("Failed to convert"), task ); } return res; } /// Returns a hashmap of tasks, or std::io::Error /// Reads all entries in a directory pub fn from_dir(base_dir: &str) -> Result, std::io::Error> { Ok(from_entries( db::from(base_dir, is_executable)? )) } /// Returns a hashmap of tasks, or std::io::Error /// Reads entries in a given list from a directory, fails if a requested entry doesn't exist /// (does not load the whole folder) pub fn from_dir_and_list(basedir: &str, list: Vec) -> Result, db::Error> { let mut entries: HashMap = HashMap::new(); for item in list { if let Some(entry) = db::entry(&basedir, is_executable, &item) { entries.insert(item.clone(), Task::from_entry(&entry)); } else { return Err(db::Error::EntryNotFound(item.clone())) } } Ok( entries ) }