From b6da345d8353a3d404788b013f950b0149bfed3d Mon Sep 17 00:00:00 2001 From: Joseph LaFreniere Date: Wed, 25 Sep 2024 00:15:50 -0400 Subject: [PATCH] Use separate config properties for adapter enable/disable lists --- src/adapters.rs | 27 +++++++++++++++++++++------ src/bin/rga.rs | 13 +++++++++++-- src/config.rs | 17 +++++++++++++++-- src/preproc.rs | 7 ++++++- 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/adapters.rs b/src/adapters.rs index f1f36f9..7c19375 100644 --- a/src/adapters.rs +++ b/src/adapters.rs @@ -109,7 +109,18 @@ pub struct AdaptInfo { /// (enabledAdapters, disabledAdapters) type AdaptersTuple = (Vec>, Vec>); -pub fn get_all_adapters(custom_adapters: Option>) -> AdaptersTuple { +/// ``` +/// # use ripgrep_all::adapters::get_all_adapters; +/// let enable = &[]; +/// let disable = &[String::from("ffmpeg")]; +/// let (_, disabled) = get_all_adapters(None, enable, disable); +/// assert!(!disabled.is_empty()) +/// ``` +pub fn get_all_adapters( + custom_adapters: Option>, + adapters_enable: &[String], + adapters_disable: &[String], +) -> AdaptersTuple { // order in descending priority let mut adapters: Vec> = vec![]; if let Some(custom_adapters) = custom_adapters { @@ -134,9 +145,10 @@ pub fn get_all_adapters(custom_adapters: Option>) -> Ad ); adapters.extend(internal_adapters); - adapters - .into_iter() - .partition(|e| !e.metadata().disabled_by_default) + adapters.into_iter().partition(|e| { + !adapters_disable.contains(&e.metadata().name) + && (adapters_enable.contains(&e.metadata().name) || !e.metadata().disabled_by_default) + }) } /** @@ -149,9 +161,12 @@ pub fn get_all_adapters(custom_adapters: Option>) -> Ad */ pub fn get_adapters_filtered>( custom_adapters: Option>, + adapters_enable: &[String], + adapters_disable: &[String], adapter_names: &[T], ) -> Result>> { - let (def_enabled_adapters, def_disabled_adapters) = get_all_adapters(custom_adapters); + let (def_enabled_adapters, def_disabled_adapters) = + get_all_adapters(custom_adapters, adapters_enable, adapters_disable); let adapters = if !adapter_names.is_empty() { let adapters_map: HashMap<_, _> = def_enabled_adapters .iter() @@ -168,9 +183,9 @@ pub fn get_adapters_filtered>( name = &name[1..]; adapters = def_enabled_adapters.clone(); } else if i == 0 && (name.starts_with('+')) { + additive = true; name = &name[1..]; adapters = def_enabled_adapters.clone(); - additive = true; } if subtractive { let inx = adapters diff --git a/src/bin/rga.rs b/src/bin/rga.rs index c3ed99d..dfe5da0 100644 --- a/src/bin/rga.rs +++ b/src/bin/rga.rs @@ -12,7 +12,11 @@ use std::process::Command; use std::time::Instant; fn list_adapters(args: RgaConfig) -> Result<()> { - let (enabled_adapters, disabled_adapters) = get_all_adapters(args.custom_adapters); + let (enabled_adapters, disabled_adapters) = get_all_adapters( + args.custom_adapters, + &args.adapters_enable, + &args.adapters_disable, + ); println!("Adapters:\n"); let print = |adapter: std::sync::Arc| { @@ -87,7 +91,12 @@ fn main() -> anyhow::Result<()> { return Ok(()); } - let adapters = get_adapters_filtered(config.custom_adapters.clone(), &config.adapters)?; + let adapters = get_adapters_filtered( + config.custom_adapters.clone(), + &config.adapters_enable, + &config.adapters_disable, + &config.adapters, + )?; let pre_glob = if !config.accurate { let extensions = adapters diff --git a/src/config.rs b/src/config.rs index 709aadc..913a6e6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -130,7 +130,7 @@ pub struct RgaConfig { /// - "foo,bar" means use only adapters foo and bar. /// - "-bar,baz" means use all default adapters except for bar and baz. /// - "+bar,baz" means use all default adapters and also bar and baz. - #[serde(default, skip_serializing_if = "is_default")] + #[serde(skip)] // CLI only #[structopt( long = "--rga-adapters", require_equals = true, @@ -138,6 +138,19 @@ pub struct RgaConfig { )] pub adapters: Vec, + /// Additional adapters to enable in addition to any default adapters. + #[serde(default, skip_serializing_if = "is_default")] + #[structopt(skip)] // config file only + pub adapters_enable: Vec, + + /// Adapters to explicitly disable. + /// + /// Entries in this list will overrule those in `adapters_enable`; + /// if the same adapter is present in both lists it will be disabled. + #[serde(default, skip_serializing_if = "is_default")] + #[structopt(skip)] // config file only + pub adapters_disable: Vec, + #[serde(default, skip_serializing_if = "is_default")] #[structopt(flatten)] pub cache: CacheConfig, @@ -378,7 +391,7 @@ where ) })?; { - // readd values with [serde(skip)] + // read values with [serde(skip)] res.fzf_path = arg_matches.fzf_path; res.list_adapters = arg_matches.list_adapters; res.print_config_schema = arg_matches.print_config_schema; diff --git a/src/preproc.rs b/src/preproc.rs index 32f3fa8..aff21ee 100644 --- a/src/preproc.rs +++ b/src/preproc.rs @@ -32,7 +32,12 @@ async fn choose_adapter( archive_recursion_depth: i32, inp: &mut (impl AsyncBufRead + Unpin), ) -> Result, FileMatcher, ActiveAdapters)>> { - let active_adapters = get_adapters_filtered(config.custom_adapters.clone(), &config.adapters)?; + let active_adapters = get_adapters_filtered( + config.custom_adapters.clone(), + &config.adapters_enable, + &config.adapters_disable, + &config.adapters, + )?; let adapters = adapter_matcher(&active_adapters, config.accurate)?; let filename = filepath_hint .file_name()