From 937b1a81acc71f639d7282224bbb2a31edb2651a Mon Sep 17 00:00:00 2001 From: phiresky Date: Sat, 5 Nov 2022 01:01:05 +0100 Subject: [PATCH] less duplication --- src/preproc.rs | 86 +++++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/src/preproc.rs b/src/preproc.rs index 7320ba6..661375d 100644 --- a/src/preproc.rs +++ b/src/preproc.rs @@ -14,7 +14,7 @@ use async_stream::stream; use log::*; use path_clean::PathClean; use std::sync::Arc; -// use postproc::PostprocPrefix; +use postproc::PostprocPrefix; use std::convert::TryInto; use std::io::Cursor; use std::path::Path; @@ -52,12 +52,11 @@ async fn choose_adapter( Ok(adapter.map(|e| (e.0, e.1, active_adapters))) } -async fn buf_choose_adapter( - ai: AdaptInfo, -) -> Result<( - AdaptInfo, - Option<(Arc, FileMatcher, ActiveAdapters)>, -)> { +enum Ret { + Recurse(AdaptInfo, Arc, FileMatcher, ActiveAdapters), + Passthrough(AdaptInfo), +} +async fn buf_choose_adapter(ai: AdaptInfo) -> Result { let mut inp = BufReader::with_capacity(1 << 16, ai.inp); let adapter = choose_adapter( &ai.config, @@ -66,36 +65,39 @@ async fn buf_choose_adapter( &mut inp, ) .await?; + let (a, b, c) = match adapter { + Some(x) => x, + None => { + // allow passthrough if the file is in an archive or accurate matching is enabled + // otherwise it should have been filtered out by rg pre-glob since rg can handle those better than us + let allow_cat = !ai.is_real_file || ai.config.accurate; + if allow_cat { + if ai.postprocess { + ( + Arc::new(PostprocPrefix {}) as Arc, + FileMatcher::Fast(FastFileMatcher::FileExtension("default".to_string())), + Vec::new(), + ) + } else { + return Ok(Ret::Passthrough(ai)); + } + } else { + return Err(format_err!( + "No adapter found for file {:?}, passthrough disabled.", + ai.filepath_hint + .file_name() + .ok_or_else(|| format_err!("Empty filename"))? + )); + } + } + }; let ai = AdaptInfo { inp: Box::pin(inp), ..ai }; - Ok((ai, adapter)) + Ok(Ret::Recurse(ai, a, b, c)) } -fn handle_no_adapter(ai: AdaptInfo) -> Result { - // allow passthrough if the file is in an archive or accurate matching is enabled - // otherwise it should have been filtered out by rg pre-glob since rg can handle those better than us - let allow_cat = !ai.is_real_file || ai.config.accurate; - if allow_cat { - if ai.postprocess { - panic!("not implemented"); - /* ( - Rc::new(PostprocPrefix {}) as Arc, - FileMatcher::Fast(FastFileMatcher::FileExtension("default".to_string())), // todo: separate enum value for this - )*/ - } else { - return Ok(ai); - } - } else { - return Err(format_err!( - "No adapter found for file {:?}, passthrough disabled.", - ai.filepath_hint - .file_name() - .ok_or_else(|| format_err!("Empty filename"))? - )); - } -} /** * preprocess a file as defined in `ai`. * @@ -111,9 +113,11 @@ pub async fn rga_preproc(ai: AdaptInfo) -> Result { // todo: figure out when using a bufreader is a good idea and when it is not // seems to be good for File::open() reads, but not sure about within archives (tar, zip) - let (ai, adapter) = buf_choose_adapter(ai).await?; - let Some((adapter, detection_reason, active_adapters)) = adapter else { - return handle_no_adapter(ai).map(|ai| ai.inp); + let (ai, adapter, detection_reason, active_adapters) = match buf_choose_adapter(ai).await? { + Ret::Recurse(ai, a, b, c) => (ai, a, b, c), + Ret::Passthrough(ai) => { + return Ok(ai.inp); + } }; let path_hint_copy = ai.filepath_hint.clone(); adapt_caching(ai, adapter, detection_reason, active_adapters) @@ -227,13 +231,15 @@ fn loop_adapt( let s = stream! { for await file in inp { - let (file, chosen_adapter) = buf_choose_adapter(file).await.expect("todo: handle"); - if let Some((adapter, detection_reason, active_adapters)) = chosen_adapter { - for await file in loop_adapt(adapter.as_ref(), detection_reason, file).expect("todo: handle") { - yield file; + match buf_choose_adapter(file).await.expect("todo: handle") { + Ret::Recurse(ai, adapter, detection_reason, active_adapters) => { + for await file in loop_adapt(adapter.as_ref(), detection_reason, file).expect("todo: handle") { + yield file; + } + } + Ret::Passthrough(ai) => { + yield ai; } - } else { - yield handle_no_adapter(file).expect("todo: handle"); } } };