From 046b0574dc68115112a58b5ef87fb664cf9e06a4 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Mon, 1 Oct 2018 20:46:59 +0200 Subject: [PATCH] Rename error => print_error_and_exit and introduce print_error --- src/exec/job.rs | 4 ++-- src/internal.rs | 10 +++++++--- src/main.rs | 15 ++++++++------- src/walk.rs | 12 ++++++------ 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/exec/job.rs b/src/exec/job.rs index 3300691..18a5cef 100644 --- a/src/exec/job.rs +++ b/src/exec/job.rs @@ -7,7 +7,7 @@ // according to those terms. use super::CommandTemplate; -use internal::error; +use internal::print_error_and_exit; use std::path::PathBuf; use std::sync::mpsc::Receiver; use std::sync::{Arc, Mutex}; @@ -31,7 +31,7 @@ pub fn job( Ok(value) => match value { WorkerResult::Entry(val) => val, WorkerResult::Error(err) => { - error(&format!("{}", err)); + print_error_and_exit(&format!("{}", err)); } }, Err(_) => break, diff --git a/src/internal.rs b/src/internal.rs index 263571d..78bf1ac 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -7,7 +7,6 @@ // according to those terms. use std::ffi::OsString; -use std::io::Write; use std::path::PathBuf; use std::process; use std::time; @@ -174,9 +173,14 @@ pub struct FdOptions { pub size_constraints: Vec, } +/// Print error message to stderr. +pub fn print_error(message: &str) { + eprintln!("{}", message); +} + /// Print error message to stderr and exit with status `1`. -pub fn error(message: &str) -> ! { - writeln!(&mut ::std::io::stderr(), "{}", message).expect("Failed writing to stderr"); +pub fn print_error_and_exit(message: &str) -> ! { + print_error(message); process::exit(1); } diff --git a/src/main.rs b/src/main.rs index defcde5..69d8656 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,8 @@ use regex::{RegexBuilder, RegexSetBuilder}; use exec::CommandTemplate; use internal::{ - error, pattern_has_uppercase_char, transform_args_with_exec, FdOptions, FileTypes, SizeFilter, + pattern_has_uppercase_char, print_error_and_exit, transform_args_with_exec, FdOptions, + FileTypes, SizeFilter, }; use lscolors::LsColors; @@ -52,7 +53,7 @@ fn main() { // Get the current working directory let current_dir = Path::new("."); if !fshelper::is_dir(current_dir) { - error("Error: could not get current directory."); + print_error_and_exit("Error: could not get current directory."); } // Get one or more root directories to search. @@ -61,7 +62,7 @@ fn main() { .map(|path| { let path_buffer = PathBuf::from(path); if !fshelper::is_dir(&path_buffer) { - error(&format!( + print_error_and_exit(&format!( "Error: '{}' is not a directory.", path_buffer.to_string_lossy() )); @@ -89,7 +90,7 @@ fn main() { && pattern.contains(std::path::MAIN_SEPARATOR) && fshelper::is_dir(Path::new(pattern)) { - error(&format!( + print_error_and_exit(&format!( "Error: The search pattern '{pattern}' contains a path-separation character ('{sep}') \ and will not lead to any search results.\n\n\ If you want to search for all files inside the '{pattern}' directory, use a match-all pattern:\n\n \ @@ -142,7 +143,7 @@ fn main() { if let Some(f) = SizeFilter::from_string(sf) { return f; } - error(&format!("Error: {} is not a valid size constraint.", sf)); + print_error_and_exit(&format!("Error: {} is not a valid size constraint.", sf)); }) .collect() }) @@ -211,7 +212,7 @@ fn main() { .build() { Ok(re) => re, - Err(err) => error(err.description()), + Err(err) => print_error_and_exit(err.description()), } }), command, @@ -232,7 +233,7 @@ fn main() { .build() { Ok(re) => walk::scan(&dir_vec, Arc::new(re), Arc::new(config)), - Err(err) => error( + Err(err) => print_error_and_exit( format!( "{}\nHint: You can use the '--fixed-strings' option to search for a \ literal string instead of a regular expression", diff --git a/src/walk.rs b/src/walk.rs index 444db47..29672e7 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -10,7 +10,7 @@ extern crate ctrlc; use exec; use fshelper; -use internal::{error, FdOptions, EXITCODE_SIGINT, MAX_BUFFER_LENGTH}; +use internal::{print_error, print_error_and_exit, FdOptions, EXITCODE_SIGINT, MAX_BUFFER_LENGTH}; use output; use std::error::Error; @@ -60,11 +60,11 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc, config: Arc) { for pattern in &config.exclude_patterns { let res = override_builder.add(pattern); if res.is_err() { - error(&format!("Error: malformed exclude pattern '{}'", pattern)); + print_error_and_exit(&format!("Error: malformed exclude pattern '{}'", pattern)); } } let overrides = override_builder.build().unwrap_or_else(|_| { - error("Mismatch in exclude patterns"); + print_error_and_exit("Mismatch in exclude patterns"); }); let mut walker = WalkBuilder::new(first_path_buf.as_path()); @@ -89,7 +89,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc, config: Arc) { match err { ignore::Error::Partial(_) => (), _ => { - error(&format!( + print_error_and_exit(&format!( "Error while parsing custom ignore file '{}': {}.", ignore_file.to_string_lossy(), err.description() @@ -189,7 +189,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc, config: Arc) { } } WorkerResult::Error(err) => { - error(&format!("{}", err)); + print_error(&format!("{}", err)); } } } @@ -291,7 +291,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc, config: Arc) { let search_str_o = if config.search_full_path { match fshelper::path_absolute_form(entry_path) { Ok(path_abs_buf) => Some(path_abs_buf.to_string_lossy().into_owned().into()), - Err(_) => error("Error: unable to get full path."), + Err(_) => print_error_and_exit("Error: unable to get full path."), } } else { entry_path.file_name().map(|f| f.to_string_lossy())