feat: enable display of error messages
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by psinghal20 · committed by David Peter · parent 27caa33 · Browse files · 2 files·+46 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+46 added−22 removed
- other:+46 added−22 removed
Provenance
- Commit
2ebef2d- Tree
e1cafc5- Author
psinghal20 <psinghal20@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
27caa33
2 files changed+46 added−22 removed
modified:src/exec/job.rs+11 added-3 removed
7 unmodified lines8 9 use std::path::PathBuf;10 use std::sync::mpsc::Receiver;11 use std::sync::{Arc, Mutex};12-12+use::walk::WorkerResult;13+use::internal::error;14 use super::CommandTemplate;15 16 /// An event loop that listens for inputs from the `rx` receiver. Each received input will17 /// generate a command with the supplied command template. The generated command will then18 /// be executed, and this process will continue until the receiver's sender has closed.18-pub fn job(rx: Arc<Mutex<Receiver<PathBuf>>>, cmd: Arc<CommandTemplate>, out_perm: Arc<Mutex<()>>) {19+pub fn job(rx: Arc<Mutex<Receiver<WorkerResult>>>, cmd: Arc<CommandTemplate>, out_perm: Arc<Mutex<()>>) {20 loop {21 // Create a lock on the shared receiver for this thread.22 let lock = rx.lock().unwrap();23 24 // Obtain the next path from the receiver, else if the channel25 // has closed, exit from the loop26 let value: PathBuf = match lock.recv() {26- Ok(value) => value,27+ Ok(value) => {28+ match value {29+ WorkerResult::Entry(val) => val,30+ WorkerResult::Error(err) => {31+ error(&format!("{}", err));32+ }33+ }34+ },35 Err(_) => break,36 };37 38 // Drop the lock so that other threads can read from the the receiver.5 unmodified linesmodified:src/walk.rs+35 added-19 removed
34 unmodified lines35 /// Receiver is directly printing results to the output.36 Streaming,37 }38 39+/// The Worker threads can result in a valid entry having PathBuf or an error.40+pub enum WorkerResult {41+ Entry(PathBuf),42+ Error(ignore::Error)43+}44+45 /// Recursively scan the given search path for files / pathnames matching the pattern.46 ///47 /// If the `--exec` argument was supplied, this will create a thread pool for executing48 /// jobs in parallel from a given command line and the discovered paths. Otherwise, each112 unmodified lines161 let max_buffer_time = rx_config162 .max_buffer_time163 .unwrap_or_else(|| time::Duration::from_millis(100));164 165+ for worker_result in rx {166+ match worker_result {159- for value in rx {160- match mode {161- ReceiverMode::Buffering => {162- buffer.push(value);167+ WorkerResult::Entry(value) => {168+ match mode {169+ ReceiverMode::Buffering => {170+ buffer.push(value);171 164- // Have we reached the maximum buffer size or maximum buffering time?165- if buffer.len() > MAX_BUFFER_LENGTH166- || time::Instant::now() - start > max_buffer_time167- {168- // Flush the buffer169- for v in &buffer {170- output::print_entry(v, &rx_config, &receiver_wtq);171- }172- buffer.clear();172+ // Have we reached the maximum buffer size or maximum buffering time?173+ if buffer.len() > MAX_BUFFER_LENGTH174+ || time::Instant::now() - start > max_buffer_time175+ {176+ // Flush the buffer177+ for v in &buffer {178+ output::print_entry(v, &rx_config, &receiver_wtq);179+ }180+ buffer.clear();181 174- // Start streaming175- mode = ReceiverMode::Streaming;182+ // Start streaming183+ mode = ReceiverMode::Streaming;184+ }185+ }186+ ReceiverMode::Streaming => {187+ output::print_entry(&value, &rx_config, &receiver_wtq);188+ }189 }190 }178- ReceiverMode::Streaming => {179- output::print_entry(&value, &rx_config, &receiver_wtq);191+ WorkerResult::Error(err) => {192+ error(&format!("{}", err));193 }194 }195 }196 21 unmodified lines218 }219 220 let entry = match entry_o {221 Ok(e) => e,209- Err(_) => return ignore::WalkState::Continue,222+ Err(err) => {223+ tx_thread.send(WorkerResult::Error(err)).unwrap();224+ return ignore::WalkState::Continue;225+ }226 };227 228 if entry.depth() == 0 {229 return ignore::WalkState::Continue;69 unmodified lines299 300 if let Some(search_str) = search_str_o {301 if pattern.is_match(&*search_str) {302 // TODO: take care of the unwrap call287- tx_thread.send(entry_path.to_owned()).unwrap()303+ tx_thread.send(WorkerResult::Entry(entry_path.to_owned())).unwrap()304 }305 }306 307 ignore::WalkState::Continue14 unmodified lines