Skip to content

Commit

Permalink
Merge branch 'separate-disable-adapters'
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrenierejm committed Oct 1, 2024
2 parents 2b83bac + b6da345 commit 89edee1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
28 changes: 22 additions & 6 deletions src/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,18 @@ pub struct AdaptInfo {
/// (enabledAdapters, disabledAdapters)
type AdaptersTuple = (Vec<Arc<dyn FileAdapter>>, Vec<Arc<dyn FileAdapter>>);

/// ```
/// # use ripgrep_all::adapters::get_all_adapters;
/// let enable = &[];
/// let disable = &[String::from("ffmpeg")];
/// let (_, disabled) = get_all_adapters(None, None, enable, disable);
/// assert!(!disabled.is_empty())
/// ```
pub fn get_all_adapters(
custom_identifiers: Option<CustomIdentifiers>,
custom_adapters: Option<Vec<CustomAdapterConfig>>,
adapters_enable: &[String],
adapters_disable: &[String],
) -> AdaptersTuple {
// decompress
let mut bz2_extensions = strs(decompress::EXTENSIONS_BZ2);
Expand Down Expand Up @@ -287,9 +296,10 @@ pub fn get_all_adapters(
);
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 @@ -303,10 +313,16 @@ pub fn get_all_adapters(
pub fn get_adapters_filtered<T: AsRef<str>>(
custom_identifiers: Option<CustomIdentifiers>,
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_identifiers, custom_adapters);
let (def_enabled_adapters, def_disabled_adapters) = get_all_adapters(
custom_identifiers,
custom_adapters,
adapters_enable,
adapters_disable,
);
let adapters = if !adapter_names.is_empty() {
let adapters_map: HashMap<_, _> = def_enabled_adapters
.iter()
Expand All @@ -323,9 +339,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
10 changes: 8 additions & 2 deletions src/bin/rga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ use std::process::Command;
use std::time::Instant;

fn list_adapters(args: RgaConfig) -> Result<()> {
let (enabled_adapters, disabled_adapters) =
get_all_adapters(args.custom_identifiers, args.custom_adapters);
let (enabled_adapters, disabled_adapters) = get_all_adapters(
args.custom_identifiers,
args.custom_adapters,
&args.adapters_enable,
&args.adapters_disable,
);

println!("Adapters:\n");
let print = |adapter: std::sync::Arc<dyn FileAdapter>| {
Expand Down Expand Up @@ -89,6 +93,8 @@ fn main() -> anyhow::Result<()> {
let adapters = get_adapters_filtered(
config.custom_identifiers.clone(),
config.custom_adapters.clone(),
&config.adapters_enable,
&config.adapters_disable,
&config.adapters,
)?;

Expand Down
17 changes: 15 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,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 @@ -385,7 +398,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
2 changes: 2 additions & 0 deletions src/preproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ async fn choose_adapter(
let active_adapters = get_adapters_filtered(
config.custom_identifiers.clone(),
config.custom_adapters.clone(),
&config.adapters_enable,
&config.adapters_disable,
&config.adapters,
)?;
let adapters = adapter_matcher(&active_adapters, config.accurate)?;
Expand Down

0 comments on commit 89edee1

Please sign in to comment.