Rename error => print_error_and_exit and introduce print_error
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by David Peter · parent 6407dc4 · Browse files · 4 files·+23 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+23 added−18 removed
- other:+23 added−18 removed
Provenance
- Commit
046b057- Tree
cf5a75d- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
6407dc4
4 files changed+23 added−18 removed
modified:src/exec/job.rs+2 added-2 removed
5 unmodified lines6 // notice may not be copied, modified, or distributed except7 // according to those terms.8 9 use super::CommandTemplate;10-use internal::error;10+use internal::print_error_and_exit;11 use std::path::PathBuf;12 use std::sync::mpsc::Receiver;13 use std::sync::{Arc, Mutex};14 use walk::WorkerResult;15 unmodified lines30 let value: PathBuf = match lock.recv() {31 Ok(value) => match value {32 WorkerResult::Entry(val) => val,33 WorkerResult::Error(err) => {34- error(&format!("{}", err));34+ print_error_and_exit(&format!("{}", err));35 }36 },37 Err(_) => break,38 };7 unmodified linesmodified:src/internal.rs+7 added-3 removed
5 unmodified lines6 // notice may not be copied, modified, or distributed except7 // according to those terms.8 9 use std::ffi::OsString;10-use std::io::Write;10 use std::path::PathBuf;11 use std::process;12 use std::time;13 158 unmodified lines172 /// The given constraints on the size of returned files173 pub size_constraints: Vec<SizeFilter>,174 }175 176+/// Print error message to stderr.177+pub fn print_error(message: &str) {178+ eprintln!("{}", message);179+}180+181 /// Print error message to stderr and exit with status `1`.178-pub fn error(message: &str) -> ! {179- writeln!(&mut ::std::io::stderr(), "{}", message).expect("Failed writing to stderr");182+pub fn print_error_and_exit(message: &str) -> ! {183+ print_error(message);184 process::exit(1);185 }186 187 /// Determine if a regex pattern contains a literal uppercase character.298 unmodified linesmodified:src/main.rs+8 added-7 removed
36 unmodified lines37 use regex::{RegexBuilder, RegexSetBuilder};38 39 use exec::CommandTemplate;40 use internal::{41- error, pattern_has_uppercase_char, transform_args_with_exec, FdOptions, FileTypes, SizeFilter,41+ pattern_has_uppercase_char, print_error_and_exit, transform_args_with_exec, FdOptions,42+ FileTypes, SizeFilter,43 };44 use lscolors::LsColors;45 46 fn main() {5 unmodified lines52 53 // Get the current working directory54 let current_dir = Path::new(".");55 if !fshelper::is_dir(current_dir) {55- error("Error: could not get current directory.");56+ print_error_and_exit("Error: could not get current directory.");57 }58 59 // Get one or more root directories to search.60 let mut dir_vec: Vec<_> = match matches.values_of("path") {61 Some(paths) => paths62 .map(|path| {63 let path_buffer = PathBuf::from(path);64 if !fshelper::is_dir(&path_buffer) {64- error(&format!(65+ print_error_and_exit(&format!(66 "Error: '{}' is not a directory.",67 path_buffer.to_string_lossy()68 ));69 }19 unmodified lines89 if !matches.is_present("full-path")90 && pattern.contains(std::path::MAIN_SEPARATOR)91 && fshelper::is_dir(Path::new(pattern))92 {92- error(&format!(93+ print_error_and_exit(&format!(94 "Error: The search pattern '{pattern}' contains a path-separation character ('{sep}') \95 and will not lead to any search results.\n\n\96 If you want to search for all files inside the '{pattern}' directory, use a match-all pattern:\n\n \97 fd . '{pattern}'\n\n\44 unmodified lines142 v.map(|sf| {143 if let Some(f) = SizeFilter::from_string(sf) {144 return f;145 }145- error(&format!("Error: {} is not a valid size constraint.", sf));146+ print_error_and_exit(&format!("Error: {} is not a valid size constraint.", sf));147 })148 .collect()149 })150 .unwrap_or_else(|| vec![]);60 unmodified lines211 .case_insensitive(true)212 .build()213 {214 Ok(re) => re,214- Err(err) => error(err.description()),215+ Err(err) => print_error_and_exit(err.description()),216 }217 }),218 command,219 exclude_patterns: matches12 unmodified lines232 .dot_matches_new_line(true)233 .build()234 {235 Ok(re) => walk::scan(&dir_vec, Arc::new(re), Arc::new(config)),235- Err(err) => error(236+ Err(err) => print_error_and_exit(237 format!(238 "{}\nHint: You can use the '--fixed-strings' option to search for a \239 literal string instead of a regular expression",240 err.description()5 unmodified linesmodified:src/walk.rs+6 added-6 removed
8 unmodified lines9 extern crate ctrlc;10 11 use exec;12 use fshelper;13-use internal::{error, FdOptions, EXITCODE_SIGINT, MAX_BUFFER_LENGTH};13+use internal::{print_error, print_error_and_exit, FdOptions, EXITCODE_SIGINT, MAX_BUFFER_LENGTH};14 use output;15 16 use std::error::Error;17 use std::path::PathBuf;41 unmodified lines59 60 for pattern in &config.exclude_patterns {61 let res = override_builder.add(pattern);62 if res.is_err() {63- error(&format!("Error: malformed exclude pattern '{}'", pattern));63+ print_error_and_exit(&format!("Error: malformed exclude pattern '{}'", pattern));64 }65 }66 let overrides = override_builder.build().unwrap_or_else(|_| {67- error("Mismatch in exclude patterns");67+ print_error_and_exit("Mismatch in exclude patterns");68 });69 70 let mut walker = WalkBuilder::new(first_path_buf.as_path());71 walker16 unmodified lines88 if let Some(err) = result {89 match err {90 ignore::Error::Partial(_) => (),91 _ => {92- error(&format!(92+ print_error_and_exit(&format!(93 "Error while parsing custom ignore file '{}': {}.",94 ignore_file.to_string_lossy(),95 err.description()96 ));91 unmodified lines188 }189 }190 }191 WorkerResult::Error(err) => {192- error(&format!("{}", err));192+ print_error(&format!("{}", err));193 }194 }195 }196 93 unmodified lines290 291 let search_str_o = if config.search_full_path {292 match fshelper::path_absolute_form(entry_path) {293 Ok(path_abs_buf) => Some(path_abs_buf.to_string_lossy().into_owned().into()),294- Err(_) => error("Error: unable to get full path."),294+ Err(_) => print_error_and_exit("Error: unable to get full path."),295 }296 } else {297 entry_path.file_name().map(|f| f.to_string_lossy())298 };25 unmodified lines