Skip to content

Commit

Permalink
Use separate config properties for adapter enable/disable lists
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrenierejm committed Sep 25, 2024
1 parent c5dd740 commit b6da345
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
27 changes: 21 additions & 6 deletions src/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,18 @@ pub struct AdaptInfo {
/// (enabledAdapters, disabledAdapters)
type AdaptersTuple = (Vec<Arc<dyn FileAdapter>>, Vec<Arc<dyn FileAdapter>>);

pub fn get_all_adapters(custom_adapters: Option<Vec<CustomAdapterConfig>>) -> 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<Vec<CustomAdapterConfig>>,
adapters_enable: &[String],
adapters_disable: &[String],
) -> AdaptersTuple {
// order in descending priority
let mut adapters: Vec<Arc<dyn FileAdapter>> = vec![];
if let Some(custom_adapters) = custom_adapters {
Expand All @@ -134,9 +145,10 @@ pub fn get_all_adapters(custom_adapters: Option<Vec<CustomAdapterConfig>>) -> 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)
})
}

/**
Expand All @@ -149,9 +161,12 @@ pub fn get_all_adapters(custom_adapters: Option<Vec<CustomAdapterConfig>>) -> Ad
*/
pub fn get_adapters_filtered<T: AsRef<str>>(
custom_adapters: Option<Vec<CustomAdapterConfig>>,
adapters_enable: &[String],
adapters_disable: &[String],
adapter_names: &[T],
) -> Result<Vec<Arc<dyn FileAdapter>>> {
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()
Expand All @@ -168,9 +183,9 @@ pub fn get_adapters_filtered<T: AsRef<str>>(
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
Expand Down
13 changes: 11 additions & 2 deletions src/bin/rga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn FileAdapter>| {
Expand Down Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,27 @@ 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,
require_delimiter = true
)]
pub adapters: Vec<String>,

/// 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<String>,

/// 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<String>,

#[serde(default, skip_serializing_if = "is_default")]
#[structopt(flatten)]
pub cache: CacheConfig,
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/preproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ async fn choose_adapter(
archive_recursion_depth: i32,
inp: &mut (impl AsyncBufRead + Unpin),
) -> Result<Option<(Arc<dyn FileAdapter>, 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()
Expand Down

0 comments on commit b6da345

Please sign in to comment.