From 257f34907ac3ad7023e6dbfe0c968d03566bd909 Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Wed, 15 Mar 2023 08:30:00 -0700 Subject: [PATCH 1/2] fix issue where level wasn't being read from config --- src/render/context/mod.rs | 28 +++++++++++++++++++++------- src/utils.rs | 14 ++++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/render/context/mod.rs b/src/render/context/mod.rs index e05ca8ae..c543d884 100644 --- a/src/render/context/mod.rs +++ b/src/render/context/mod.rs @@ -3,7 +3,7 @@ use super::{ order::SortType, }; use clap::{ - parser::ValueSource, ArgMatches, CommandFactory, Error as ClapError, FromArgMatches, Parser, + parser::ValueSource, ArgMatches, CommandFactory, Error as ClapError, FromArgMatches, Parser, Id, }; use ignore::overrides::{Override, OverrideBuilder}; use std::{ @@ -154,22 +154,36 @@ impl Context { } }; - for id in user_args.ids() { - let id_str = id.as_str(); + let mut ids = user_args + .ids() + .map(Id::as_str) + .collect::>(); + ids.extend( + config_args + .ids() + .map(Id::as_str) + .collect::>() + ); + + ids = crate::utils::uniq(ids); + + for id in ids { // Don't look at me... my shame.. - if id_str == "Context" { + if id == "Context" { continue; } - if let Some(user_arg) = user_args.value_source(id_str) { + if let Some(user_arg) = user_args.value_source(id) { match user_arg { // prioritize the user arg if user provided a command line argument - ValueSource::CommandLine => pick_args_from(id_str, &user_args), + ValueSource::CommandLine => pick_args_from(id, &user_args), // otherwise prioritize argument from the config - _ => pick_args_from(id_str, &config_args), + _ => pick_args_from(id, &config_args), } + } else { + pick_args_from(id, &config_args) } } diff --git a/src/utils.rs b/src/utils.rs index 7760f5e5..852a2ff1 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,7 @@ +use std::{borrow::ToOwned, cmp::Eq, collections::HashSet, hash::Hash}; + #[macro_export] +/// Ruby-like way to crate a hashmap. macro_rules! hash { ( $( $k:expr => $v:expr ),* ) => { { @@ -8,3 +11,14 @@ macro_rules! hash { } }; } + +/// Ensure every item in a `Vec` is unique. +pub fn uniq(items: Vec) -> Vec +where + T: Eq + Hash + ToOwned, + ::Owned: Hash + Eq, +{ + let mut set = HashSet::new(); + + items.into_iter().filter(|item| set.insert(item.to_owned())).collect::>() +} From 650e86d95915ff8e468a6b832e3c2be6fb54f1fe Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Wed, 15 Mar 2023 08:31:35 -0700 Subject: [PATCH 2/2] cargo fmt --- src/render/context/mod.rs | 16 ++++------------ src/utils.rs | 5 ++++- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/render/context/mod.rs b/src/render/context/mod.rs index c543d884..07839a0b 100644 --- a/src/render/context/mod.rs +++ b/src/render/context/mod.rs @@ -3,7 +3,7 @@ use super::{ order::SortType, }; use clap::{ - parser::ValueSource, ArgMatches, CommandFactory, Error as ClapError, FromArgMatches, Parser, Id, + parser::ValueSource, ArgMatches, CommandFactory, Error as ClapError, FromArgMatches, Id, Parser, }; use ignore::overrides::{Override, OverrideBuilder}; use std::{ @@ -154,17 +154,9 @@ impl Context { } }; - let mut ids = user_args - .ids() - .map(Id::as_str) - .collect::>(); - - ids.extend( - config_args - .ids() - .map(Id::as_str) - .collect::>() - ); + let mut ids = user_args.ids().map(Id::as_str).collect::>(); + + ids.extend(config_args.ids().map(Id::as_str).collect::>()); ids = crate::utils::uniq(ids); diff --git a/src/utils.rs b/src/utils.rs index 852a2ff1..18c49601 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -20,5 +20,8 @@ where { let mut set = HashSet::new(); - items.into_iter().filter(|item| set.insert(item.to_owned())).collect::>() + items + .into_iter() + .filter(|item| set.insert(item.to_owned())) + .collect::>() }