Rename error => print_error_and_exit and introduce print_error

This commit is contained in:
sharkdp 2018-10-01 20:46:59 +02:00 committed by David Peter
parent 6407dc4eb4
commit 046b0574dc
4 changed files with 23 additions and 18 deletions

View file

@ -7,7 +7,7 @@
// according to those terms. // according to those terms.
use super::CommandTemplate; use super::CommandTemplate;
use internal::error; use internal::print_error_and_exit;
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};
@ -31,7 +31,7 @@ pub fn job(
Ok(value) => match value { Ok(value) => match value {
WorkerResult::Entry(val) => val, WorkerResult::Entry(val) => val,
WorkerResult::Error(err) => { WorkerResult::Error(err) => {
error(&format!("{}", err)); print_error_and_exit(&format!("{}", err));
} }
}, },
Err(_) => break, Err(_) => break,

View file

@ -7,7 +7,6 @@
// according to those terms. // according to those terms.
use std::ffi::OsString; use std::ffi::OsString;
use std::io::Write;
use std::path::PathBuf; use std::path::PathBuf;
use std::process; use std::process;
use std::time; use std::time;
@ -174,9 +173,14 @@ pub struct FdOptions {
pub size_constraints: Vec<SizeFilter>, pub size_constraints: Vec<SizeFilter>,
} }
/// Print error message to stderr.
pub fn print_error(message: &str) {
eprintln!("{}", message);
}
/// Print error message to stderr and exit with status `1`. /// Print error message to stderr and exit with status `1`.
pub fn error(message: &str) -> ! { pub fn print_error_and_exit(message: &str) -> ! {
writeln!(&mut ::std::io::stderr(), "{}", message).expect("Failed writing to stderr"); print_error(message);
process::exit(1); process::exit(1);
} }

View file

@ -38,7 +38,8 @@ use regex::{RegexBuilder, RegexSetBuilder};
use exec::CommandTemplate; use exec::CommandTemplate;
use internal::{ 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; use lscolors::LsColors;
@ -52,7 +53,7 @@ fn main() {
// Get the current working directory // Get the current working directory
let current_dir = Path::new("."); let current_dir = Path::new(".");
if !fshelper::is_dir(current_dir) { 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. // Get one or more root directories to search.
@ -61,7 +62,7 @@ fn main() {
.map(|path| { .map(|path| {
let path_buffer = PathBuf::from(path); let path_buffer = PathBuf::from(path);
if !fshelper::is_dir(&path_buffer) { if !fshelper::is_dir(&path_buffer) {
error(&format!( print_error_and_exit(&format!(
"Error: '{}' is not a directory.", "Error: '{}' is not a directory.",
path_buffer.to_string_lossy() path_buffer.to_string_lossy()
)); ));
@ -89,7 +90,7 @@ fn main() {
&& pattern.contains(std::path::MAIN_SEPARATOR) && pattern.contains(std::path::MAIN_SEPARATOR)
&& fshelper::is_dir(Path::new(pattern)) && fshelper::is_dir(Path::new(pattern))
{ {
error(&format!( print_error_and_exit(&format!(
"Error: The search pattern '{pattern}' contains a path-separation character ('{sep}') \ "Error: The search pattern '{pattern}' contains a path-separation character ('{sep}') \
and will not lead to any search results.\n\n\ 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 \ 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) { if let Some(f) = SizeFilter::from_string(sf) {
return f; 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() .collect()
}) })
@ -211,7 +212,7 @@ fn main() {
.build() .build()
{ {
Ok(re) => re, Ok(re) => re,
Err(err) => error(err.description()), Err(err) => print_error_and_exit(err.description()),
} }
}), }),
command, command,
@ -232,7 +233,7 @@ fn main() {
.build() .build()
{ {
Ok(re) => walk::scan(&dir_vec, Arc::new(re), Arc::new(config)), Ok(re) => walk::scan(&dir_vec, Arc::new(re), Arc::new(config)),
Err(err) => error( Err(err) => print_error_and_exit(
format!( format!(
"{}\nHint: You can use the '--fixed-strings' option to search for a \ "{}\nHint: You can use the '--fixed-strings' option to search for a \
literal string instead of a regular expression", literal string instead of a regular expression",

View file

@ -10,7 +10,7 @@ extern crate ctrlc;
use exec; use exec;
use fshelper; 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 output;
use std::error::Error; use std::error::Error;
@ -60,11 +60,11 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
for pattern in &config.exclude_patterns { for pattern in &config.exclude_patterns {
let res = override_builder.add(pattern); let res = override_builder.add(pattern);
if res.is_err() { 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(|_| { 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()); let mut walker = WalkBuilder::new(first_path_buf.as_path());
@ -89,7 +89,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
match err { match err {
ignore::Error::Partial(_) => (), ignore::Error::Partial(_) => (),
_ => { _ => {
error(&format!( print_error_and_exit(&format!(
"Error while parsing custom ignore file '{}': {}.", "Error while parsing custom ignore file '{}': {}.",
ignore_file.to_string_lossy(), ignore_file.to_string_lossy(),
err.description() err.description()
@ -189,7 +189,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
} }
} }
WorkerResult::Error(err) => { WorkerResult::Error(err) => {
error(&format!("{}", err)); print_error(&format!("{}", err));
} }
} }
} }
@ -291,7 +291,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
let search_str_o = if config.search_full_path { let search_str_o = if config.search_full_path {
match fshelper::path_absolute_form(entry_path) { match fshelper::path_absolute_form(entry_path) {
Ok(path_abs_buf) => Some(path_abs_buf.to_string_lossy().into_owned().into()), 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 { } else {
entry_path.file_name().map(|f| f.to_string_lossy()) entry_path.file_name().map(|f| f.to_string_lossy())