From 6ba09c84f70a8a85de51faced4269aa5f3f99913 Mon Sep 17 00:00:00 2001 From: Valerie Date: Tue, 2 Jul 2024 09:24:10 -0400 Subject: [PATCH] major refactor: code smell improvements, filename section is now an array --- Cargo.toml | 2 +- src/main.rs | 56 +++++++++++++++++++---------------------------------- src/util.rs | 17 +++++++++++++--- 3 files changed, 35 insertions(+), 40 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 514f571..3807c9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "open" -version = "1.0.0" +version = "2.0.0" authors = ["Valerie Wolfe "] edition = "2018" diff --git a/src/main.rs b/src/main.rs index e9b713f..cfbec89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,10 +6,12 @@ use std::{ }; use pico_args::Arguments; -use toml::value::{ Array, Value }; +use toml::value::Value; mod config; mod error; +mod util; + use config::Config; fn main() { @@ -33,17 +35,9 @@ fn main() { // path flag (-p / --path) if args.contains(["-p", "--path"]) { - let local = config.local_path; - let global = config.global_path; - if local.is_some() { - println!("{}", local.unwrap()); - return; - } - if global.is_some() { - println!("{}", global.unwrap()); - return; - } - error::no_configs(); + if let Some(local) = config.local_path { println!("{local}"); } + else if let Some(global) = config.global_path { println!("{global}"); } + else { error::no_configs(); } return; } @@ -54,16 +48,17 @@ fn main() { if !target.exists() { error::not_found(&target); } // get section - // ordering: filename -> type (ext/dir) + // ordering: filename -> type (ext/dir) -> default let mut section = None; // by exact filename - let filename = target.file_name(); - if filename.is_some() { - let filename_section = config.get(filename.unwrap().to_str().unwrap()); - if filename_section.is_some() { - section = filename_section; - } + if let Some(filename) = target.file_name() { + let array = config.get("filename"); + let matches: Vec; + if let Some(Value::Array(array)) = array { matches = util::matches(array, filename.to_string_lossy().into()); } + else { matches = Vec::new(); } + + section = if matches.len() > 0 { matches.get(0).cloned() } else { None }; } // handle types; dir first @@ -76,24 +71,13 @@ fn main() { // handle types; extensions second if section.is_none() { - let extension = target.extension(); - if extension.is_some() { - let extension = extension.unwrap().to_str(); + if let Some(extension) = target.extension() { + let array: Option = config.get("extension"); + let matches: Vec; + if let Some(Value::Array(array)) = array { matches = util::matches(array, extension.to_string_lossy().into()); } + else { matches = Vec::new(); } - // pull extension array and filter matches - let i_macrosection: Option = config.get("extension"); - let macrosection: Array = i_macrosection.unwrap().as_array().unwrap().to_owned(); - let matches = macrosection.iter().filter(|value| { - let table = value.as_table().unwrap(); - let i_target = table.get("match").unwrap(); - let target = i_target.as_str(); - target == extension - }).map(|value| value.to_owned() ); - - let sections: Vec = matches.collect(); - if sections.len() > 0 { - section = sections.get(0).cloned(); - } + section = if matches.len() > 0 { matches.get(0).cloned() } else { None }; } } diff --git a/src/util.rs b/src/util.rs index 069382e..1985b05 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,16 @@ -use std::{ - process::{ Command, Stdio } -}; +use toml::{ value::Array, Value }; + +/// gets array entries with matching "match" values. +pub fn matches(macrosection: Array, to_match: String) -> Vec { + macrosection.iter().filter(|value| { + if let Some(table) = value.as_table() { + match table.get("match").unwrap() { + Value::String(target) => *target == to_match, + Value::Array(values) => values.contains(&Value::String(to_match.clone())), + _ => false + } + } else { false } + }).map(|value| value.to_owned()).collect() +}