Add print_error macros to replace functions

This patch replaces the `print_error` and `print_error_and_exit`
functions with equivalent macros.
This commit is contained in:
Josh Leeb-du Toit 2018-10-03 23:53:59 +10:00 committed by David Peter
parent abe8aa55c0
commit 984f04f142
5 changed files with 45 additions and 39 deletions

View file

@ -30,10 +30,10 @@ pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {
let _ = stderr.lock().write_all(&output.stderr);
}
Err(ref why) if why.kind() == io::ErrorKind::NotFound => {
eprintln!("fd: execution error: command not found");
print_error!("fd: execution error: command not found");
}
Err(why) => {
eprintln!("fd: execution error: {}", why);
print_error!("fd: execution error: {}", why);
}
}
}

View file

@ -7,7 +7,6 @@
// according to those terms.
use super::CommandTemplate;
use internal::print_error;
use std::path::PathBuf;
use std::sync::mpsc::Receiver;
use std::sync::{Arc, Mutex};
@ -30,7 +29,7 @@ pub fn job(
let value: PathBuf = match lock.recv() {
Ok(WorkerResult::Entry(val)) => val,
Ok(WorkerResult::Error(err)) => {
print_error(&format!("{}", err));
print_error!("{}", err);
continue;
}
Err(_) => break,

View file

@ -8,7 +8,6 @@
use std::ffi::OsString;
use std::path::PathBuf;
use std::process;
use std::time::{self, SystemTime};
use exec::CommandTemplate;
@ -199,15 +198,15 @@ pub struct FdOptions {
pub time_constraints: Vec<TimeFilter>,
}
/// Print error message to stderr.
pub fn print_error(message: &str) {
eprintln!("{}", message);
macro_rules! print_error {
($($arg:tt)*) => (eprintln!($($arg)*))
}
/// Print error message to stderr and exit with status `1`.
pub fn print_error_and_exit(message: &str) -> ! {
print_error(message);
process::exit(1);
macro_rules! print_error_and_exit {
($($arg:tt)*) => {
print_error!($($arg)*);
::std::process::exit(1);
};
}
/// Determine if a regex pattern contains a literal uppercase character.

View file

@ -20,11 +20,13 @@ extern crate num_cpus;
extern crate regex;
extern crate regex_syntax;
#[macro_use]
mod internal;
mod app;
mod exec;
mod exit_codes;
pub mod fshelper;
mod internal;
pub mod lscolors;
mod output;
mod walk;
@ -40,8 +42,8 @@ use regex::{RegexBuilder, RegexSetBuilder};
use exec::CommandTemplate;
use internal::{
pattern_has_uppercase_char, print_error_and_exit, transform_args_with_exec, FdOptions,
FileTypes, SizeFilter, TimeFilter,
pattern_has_uppercase_char, transform_args_with_exec, FdOptions, FileTypes, SizeFilter,
TimeFilter,
};
use lscolors::LsColors;
@ -55,7 +57,7 @@ fn main() {
// Get the current working directory
let current_dir = Path::new(".");
if !fshelper::is_dir(current_dir) {
print_error_and_exit("Error: could not get current directory.");
print_error_and_exit!("Error: could not get current directory.");
}
// Get one or more root directories to search.
@ -64,10 +66,10 @@ fn main() {
.map(|path| {
let path_buffer = PathBuf::from(path);
if !fshelper::is_dir(&path_buffer) {
print_error_and_exit(&format!(
print_error_and_exit!(
"Error: '{}' is not a directory.",
path_buffer.to_string_lossy()
));
);
}
path_buffer
})
@ -92,7 +94,7 @@ fn main() {
&& pattern.contains(std::path::MAIN_SEPARATOR)
&& fshelper::is_dir(Path::new(pattern))
{
print_error_and_exit(&format!(
print_error_and_exit!(
"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 \
@ -101,7 +103,7 @@ fn main() {
fd --full-path '{pattern}'",
pattern = pattern,
sep = std::path::MAIN_SEPARATOR,
));
);
}
// Treat pattern as literal string if '--fixed-strings' is used
@ -145,7 +147,7 @@ fn main() {
if let Some(f) = SizeFilter::from_string(sf) {
return f;
}
print_error_and_exit(&format!("Error: {} is not a valid size constraint.", sf));
print_error_and_exit!("Error: {} is not a valid size constraint.", sf);
})
.collect()
})
@ -157,14 +159,14 @@ fn main() {
if let Some(f) = TimeFilter::after(&now, t) {
time_constraints.push(f);
} else {
print_error_and_exit(&format!("Error: {} is not a valid time.", t));
print_error_and_exit!("Error: {} is not a valid time.", t);
}
}
if let Some(t) = matches.value_of("changed-before") {
if let Some(f) = TimeFilter::before(&now, t) {
time_constraints.push(f);
} else {
print_error_and_exit(&format!("Error: {} is not a valid time.", t));
print_error_and_exit!("Error: {} is not a valid time.", t);
}
}
@ -231,7 +233,9 @@ fn main() {
.build()
{
Ok(re) => re,
Err(err) => print_error_and_exit(err.description()),
Err(err) => {
print_error_and_exit!("{}", err.description());
}
}
}),
command,
@ -253,13 +257,12 @@ fn main() {
.build()
{
Ok(re) => walk::scan(&dir_vec, Arc::new(re), Arc::new(config)),
Err(err) => print_error_and_exit(
format!(
Err(err) => {
print_error_and_exit!(
"{}\nHint: You can use the '--fixed-strings' option to search for a \
literal string instead of a regular expression",
err.description()
)
.as_str(),
),
);
}
}
}

View file

@ -11,7 +11,7 @@ extern crate ctrlc;
use exec;
use exit_codes::ExitCode;
use fshelper;
use internal::{print_error, print_error_and_exit, FdOptions, MAX_BUFFER_LENGTH};
use internal::{FdOptions, MAX_BUFFER_LENGTH};
use output;
use std::error::Error;
@ -61,11 +61,11 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
for pattern in &config.exclude_patterns {
let res = override_builder.add(pattern);
if res.is_err() {
print_error_and_exit(&format!("Error: malformed exclude pattern '{}'", pattern));
print_error_and_exit!("Error: malformed exclude pattern '{}'", pattern);
}
}
let overrides = override_builder.build().unwrap_or_else(|_| {
print_error_and_exit("Mismatch in exclude patterns");
print_error_and_exit!("Mismatch in exclude patterns");
});
let mut walker = WalkBuilder::new(first_path_buf.as_path());
@ -90,11 +90,14 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
match err {
ignore::Error::Partial(_) => (),
_ => {
print_error(&format!(
"Error while parsing custom ignore file '{}': {}.",
ignore_file.to_string_lossy(),
err.description()
));
print_error!(
"{}",
format!(
"Error while parsing custom ignore file '{}': {}.",
ignore_file.to_string_lossy(),
err.description()
)
);
}
}
}
@ -190,7 +193,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
}
}
WorkerResult::Error(err) => {
print_error(&format!("{}", err));
print_error!("{}", err);
}
}
}
@ -310,7 +313,9 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
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(_) => print_error_and_exit("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())