feat: enable display of error messages

This commit is contained in:
psinghal20 2018-09-30 18:31:23 +05:30 committed by David Peter
parent 27caa33729
commit 2ebef2d46f
2 changed files with 46 additions and 22 deletions

View file

@ -9,13 +9,14 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::mpsc::Receiver; use std::sync::mpsc::Receiver;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use::walk::WorkerResult;
use::internal::error;
use super::CommandTemplate; use super::CommandTemplate;
/// An event loop that listens for inputs from the `rx` receiver. Each received input will /// An event loop that listens for inputs from the `rx` receiver. Each received input will
/// generate a command with the supplied command template. The generated command will then /// generate a command with the supplied command template. The generated command will then
/// be executed, and this process will continue until the receiver's sender has closed. /// be executed, and this process will continue until the receiver's sender has closed.
pub fn job(rx: Arc<Mutex<Receiver<PathBuf>>>, cmd: Arc<CommandTemplate>, out_perm: Arc<Mutex<()>>) { pub fn job(rx: Arc<Mutex<Receiver<WorkerResult>>>, cmd: Arc<CommandTemplate>, out_perm: Arc<Mutex<()>>) {
loop { loop {
// Create a lock on the shared receiver for this thread. // Create a lock on the shared receiver for this thread.
let lock = rx.lock().unwrap(); let lock = rx.lock().unwrap();
@ -23,7 +24,14 @@ pub fn job(rx: Arc<Mutex<Receiver<PathBuf>>>, cmd: Arc<CommandTemplate>, out_per
// Obtain the next path from the receiver, else if the channel // Obtain the next path from the receiver, else if the channel
// has closed, exit from the loop // has closed, exit from the loop
let value: PathBuf = match lock.recv() { let value: PathBuf = match lock.recv() {
Ok(value) => value, Ok(value) => {
match value {
WorkerResult::Entry(val) => val,
WorkerResult::Error(err) => {
error(&format!("{}", err));
}
}
},
Err(_) => break, Err(_) => break,
}; };

View file

@ -36,6 +36,12 @@ enum ReceiverMode {
Streaming, Streaming,
} }
/// The Worker threads can result in a valid entry having PathBuf or an error.
pub enum WorkerResult {
Entry(PathBuf),
Error(ignore::Error)
}
/// Recursively scan the given search path for files / pathnames matching the pattern. /// Recursively scan the given search path for files / pathnames matching the pattern.
/// ///
/// If the `--exec` argument was supplied, this will create a thread pool for executing /// If the `--exec` argument was supplied, this will create a thread pool for executing
@ -156,7 +162,9 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
.max_buffer_time .max_buffer_time
.unwrap_or_else(|| time::Duration::from_millis(100)); .unwrap_or_else(|| time::Duration::from_millis(100));
for value in rx { for worker_result in rx {
match worker_result {
WorkerResult::Entry(value) => {
match mode { match mode {
ReceiverMode::Buffering => { ReceiverMode::Buffering => {
buffer.push(value); buffer.push(value);
@ -180,6 +188,11 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
} }
} }
} }
WorkerResult::Error(err) => {
error(&format!("{}", err));
}
}
}
// If we have finished fast enough (faster than max_buffer_time), we haven't streamed // If we have finished fast enough (faster than max_buffer_time), we haven't streamed
// anything to the console, yet. In this case, sort the results and print them: // anything to the console, yet. In this case, sort the results and print them:
@ -206,7 +219,10 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
let entry = match entry_o { let entry = match entry_o {
Ok(e) => e, Ok(e) => e,
Err(_) => return ignore::WalkState::Continue, Err(err) => {
tx_thread.send(WorkerResult::Error(err)).unwrap();
return ignore::WalkState::Continue;
}
}; };
if entry.depth() == 0 { if entry.depth() == 0 {
@ -284,7 +300,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
if let Some(search_str) = search_str_o { if let Some(search_str) = search_str_o {
if pattern.is_match(&*search_str) { if pattern.is_match(&*search_str) {
// TODO: take care of the unwrap call // TODO: take care of the unwrap call
tx_thread.send(entry_path.to_owned()).unwrap() tx_thread.send(WorkerResult::Entry(entry_path.to_owned())).unwrap()
} }
} }