From d02aca6864436b5f8e3ee599dcf8d6d464644e0d Mon Sep 17 00:00:00 2001 From: southerntofu Date: Wed, 10 Jun 2020 13:04:35 +0200 Subject: [PATCH] Load tasks from positional arguments --- src/db.rs | 19 ++++++++++++++++++- src/main.rs | 10 +++------- src/task.rs | 20 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/db.rs b/src/db.rs index 21b462d..bc25616 100644 --- a/src/db.rs +++ b/src/db.rs @@ -4,7 +4,7 @@ use std::os::unix::fs::MetadataExt; use std::ffi::OsString; #[derive(Debug)] -pub enum DbError { +pub enum Error { EntryNotFound(String) } @@ -31,6 +31,23 @@ impl Entry { } } +/// Load single entry from folder +pub fn entry(basedir: &str, filter: impl Fn(&Path) -> bool, name: &str) -> Option { + let basepath = PathBuf::from(&basedir); + let path = basepath.clone().join(name); + + if !path.exists() || !filter(&path) { + return None; + } + + Some(Entry::new( + path.clone(), + path.file_name().expect("Failed to read file name").to_os_string(), + basepath + )) +} + +/// Loads entire database from folder pub fn from(path_name: &str, filter: impl Fn(&Path) -> bool) -> Result, std::io::Error> { let path = PathBuf::from(&path_name); diff --git a/src/main.rs b/src/main.rs index 27afcc5..c7f0b11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,18 +14,14 @@ fn main() -> Result<(), std::io::Error> { let home_dir = env::var("HOME").expect("$HOME not defined. WTF?"); let base_dir = format!("{}/.git-build", home_dir); - //let entries = db::Database::from(&base_dir, is_executable)?; - let tasks = task::from_dir(&base_dir).expect("Could not load DB"); - let cmd = cli::Cli::from_args(); - /* let tasks = if cmd.tasks.is_empty() { - db.tasks() + task::from_dir(&base_dir).expect("Could not load DB") } else { - db.tasks_from(cmd.tasks).expect("Could not process the lsit of tasks given") + task::from_dir_and_list(&base_dir, cmd.tasks).expect("Could not load given tasks") }; - */ + for (task_name, task) in tasks.iter() { let mut context = HashMap::new(); //context.insert("$i18n_task", task_name.to_str().expect("WTF")); diff --git a/src/task.rs b/src/task.rs index f2ab1c5..c7fa46e 100644 --- a/src/task.rs +++ b/src/task.rs @@ -55,8 +55,28 @@ pub fn from_entries(db: Vec) -> HashMap { 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 + ) +}