diff --git a/Cargo.lock b/Cargo.lock index 650c663..891b4ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3148,7 +3148,7 @@ dependencies = [ [[package]] name = "redacter" -version = "0.11.0" +version = "0.11.1" dependencies = [ "anyhow", "arboard", diff --git a/Cargo.toml b/Cargo.toml index 8e57fb2..798f19c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "redacter" -version = "0.11.0" +version = "0.11.1" edition = "2021" authors = ["Abdulla Abdurakhmanov "] license = "Apache-2.0" diff --git a/src/commands/copy_command.rs b/src/commands/copy_command.rs index e1fb6dc..f04724f 100644 --- a/src/commands/copy_command.rs +++ b/src/commands/copy_command.rs @@ -3,7 +3,7 @@ use crate::file_converters::FileConverters; use crate::file_systems::{DetectFileSystem, FileSystemConnection, FileSystemRef}; use crate::file_tools::{FileMatcher, FileMatcherResult, FileMimeOverride}; use crate::redacters::{ - Redacter, RedacterBaseOptions, RedacterOptions, RedacterThrottler, Redacters, StreamRedacter, + RedacterBaseOptions, RedacterOptions, RedacterThrottler, Redacters, StreamRedacter, }; use crate::reporter::AppReporter; use crate::AppResult; @@ -252,7 +252,7 @@ async fn transfer_and_redact_file< source_fs: &mut SFS, destination_fs: &mut DFS, options: &CopyCommandOptions, - redacter: &Option<(RedacterBaseOptions, Vec)>, + redacter: &Option<(RedacterBaseOptions, Vec>)>, file_converters: &FileConverters<'a>, redacter_throttler: &mut Option, ) -> AppResult { @@ -352,7 +352,7 @@ async fn redact_upload_file< source_reader: S, dest_file_ref: &FileSystemRef, options: &CopyCommandOptions, - redacter_with_options: &(RedacterBaseOptions, Vec), + redacter_with_options: &(RedacterBaseOptions, Vec>), file_converters: &FileConverters<'a>, redacter_throttler: &mut Option, ) -> AppResult { @@ -363,11 +363,11 @@ async fn redact_upload_file< .file_mime_override .override_for_file_ref(dest_file_ref.clone()); - let (redact_plan, supported_redacters) = stream_redacter + let redact_plan = stream_redacter .create_redact_plan(redacters, &dest_file_ref_overridden) .await?; - if !supported_redacters.is_empty() { + if !redact_plan.supported_redacters.is_empty() { if let Some(ref mut throttler) = redacter_throttler { *throttler = throttler.update(Instant::now()); let delay = throttler.delay(); @@ -386,12 +386,7 @@ async fn redact_upload_file< } } match stream_redacter - .redact_stream( - source_reader, - redact_plan, - &supported_redacters, - &dest_file_ref_overridden, - ) + .redact_stream(source_reader, redact_plan, &dest_file_ref_overridden) .await { Ok(redacted_result) diff --git a/src/redacters/stream_redacter.rs b/src/redacters/stream_redacter.rs index c5c3c6d..62e314e 100644 --- a/src/redacters/stream_redacter.rs +++ b/src/redacters/stream_redacter.rs @@ -24,10 +24,11 @@ pub struct StreamRedacter<'a> { bar: &'a ProgressBar, } -pub struct StreamRedactPlan { +pub struct StreamRedactPlan<'a> { pub apply_pdf_image_converter: bool, pub apply_ocr: bool, pub leave_data_table_as_text: bool, + pub supported_redacters: Vec<&'a Redacters<'a>>, } impl<'a> StreamRedacter<'a> { @@ -45,24 +46,24 @@ impl<'a> StreamRedacter<'a> { pub async fn create_redact_plan( &'a self, - redacters: &'a Vec, + redacters: &'a Vec>, file_ref: &FileSystemRef, - ) -> AppResult<(StreamRedactPlan, Vec<&'a impl Redacter>)> { + ) -> AppResult { let mut stream_redact_plan = StreamRedactPlan { apply_pdf_image_converter: false, apply_ocr: false, leave_data_table_as_text: false, + supported_redacters: vec![], }; // Supports natively - let mut supported_redacters = Vec::new(); for redacter in redacters { let supported_options = redacter.redact_support(file_ref).await?; if supported_options == RedactSupport::Supported { - supported_redacters.push(redacter); + stream_redact_plan.supported_redacters.push(redacter); } } - if supported_redacters.is_empty() { + if stream_redact_plan.supported_redacters.is_empty() { match &file_ref.media_type { Some(file_ref_media) => { // Supports with conversion @@ -75,10 +76,10 @@ impl<'a> StreamRedacter<'a> { }) .await?; if supported_options == RedactSupport::Supported { - supported_redacters.push(redacter); + stream_redact_plan.supported_redacters.push(redacter); } } - if !supported_redacters.is_empty() { + if !stream_redact_plan.supported_redacters.is_empty() { stream_redact_plan.leave_data_table_as_text = true; } } else if self.file_converters.pdf_image_converter.is_some() @@ -92,15 +93,17 @@ impl<'a> StreamRedacter<'a> { }) .await?; if supported_options == RedactSupport::Supported { - supported_redacters.push(redacter); + stream_redact_plan.supported_redacters.push(redacter); } } - if !supported_redacters.is_empty() { + if !stream_redact_plan.supported_redacters.is_empty() { stream_redact_plan.apply_pdf_image_converter = true; } - if supported_redacters.is_empty() && self.file_converters.ocr.is_some() { + if stream_redact_plan.supported_redacters.is_empty() + && self.file_converters.ocr.is_some() + { for redacter in redacters { let supported_options = redacter .redact_support(&FileSystemRef { @@ -109,10 +112,10 @@ impl<'a> StreamRedacter<'a> { }) .await?; if supported_options == RedactSupport::Supported { - supported_redacters.push(redacter); + stream_redact_plan.supported_redacters.push(redacter); } } - if !supported_redacters.is_empty() { + if !stream_redact_plan.supported_redacters.is_empty() { stream_redact_plan.apply_pdf_image_converter = true; stream_redact_plan.apply_ocr = true; } @@ -128,10 +131,10 @@ impl<'a> StreamRedacter<'a> { }) .await?; if supported_options == RedactSupport::Supported { - supported_redacters.push(redacter); + stream_redact_plan.supported_redacters.push(redacter); } } - if !supported_redacters.is_empty() { + if !stream_redact_plan.supported_redacters.is_empty() { stream_redact_plan.apply_ocr = true; } } @@ -140,7 +143,7 @@ impl<'a> StreamRedacter<'a> { } } - Ok((stream_redact_plan, supported_redacters)) + Ok(stream_redact_plan) } pub async fn redact_stream< @@ -148,8 +151,7 @@ impl<'a> StreamRedacter<'a> { >( &'a self, input: S, - redact_plan: StreamRedactPlan, - redacters: &[&'a impl Redacter], + redact_plan: StreamRedactPlan<'a>, file_ref: &FileSystemRef, ) -> AppResult { let mut redacted = self @@ -157,7 +159,7 @@ impl<'a> StreamRedacter<'a> { .await?; let mut number_of_redactions = 0; - for (index, redacter) in redacters.iter().enumerate() { + for (index, redacter) in redact_plan.supported_redacters.iter().enumerate() { let width = " ".repeat(index); if redact_plan.apply_pdf_image_converter { match ( @@ -272,7 +274,7 @@ impl<'a> StreamRedacter<'a> { redacter_base_options: &RedacterBaseOptions, input: S, file_ref: &FileSystemRef, - redact_plan: &StreamRedactPlan, + redact_plan: &StreamRedactPlan<'a>, ) -> AppResult { match file_ref.media_type { Some(ref mime)