From 636b56729be21a17a5520988f0d23f5d26e9a277 Mon Sep 17 00:00:00 2001 From: Thomas Otto Date: Thu, 14 Oct 2021 00:15:56 +0200 Subject: [PATCH] Add fatal() function to handle exiting on errors Panic when testing or exit with return value 2. --- src/config.rs | 6 ++---- src/main.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index fbd58044..5a013a1b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ use crate::cli; use crate::color; use crate::delta::State; use crate::env; +use crate::fatal; use crate::features::navigate; use crate::features::side_by_side; use crate::git_config::{GitConfig, GitConfigEntry}; @@ -193,10 +194,7 @@ impl From for Config { // Note that "default" is not documented Some("ansi") | Some("default") | None => BgFillMethod::TryAnsiSequence, Some("spaces") => BgFillMethod::Spaces, - _ => { - eprintln!("Invalid option for line-fill-method: Expected \"ansi\" or \"spaces\"."); - process::exit(1); - } + _ => fatal("Invalid option for line-fill-method: Expected \"ansi\" or \"spaces\"."), }; let navigate_regexp = if opt.navigate || opt.show_themes { diff --git a/src/main.rs b/src/main.rs index 1fdf9365..a3587341 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,19 @@ use crate::bat_utils::assets::{list_languages, HighlightingAssets}; use crate::bat_utils::output::OutputType; use crate::delta::delta; +pub fn fatal(errmsg: T) -> ! +where + T: AsRef + std::fmt::Display, +{ + #[cfg(not(test))] + { + eprintln!("{}", errmsg); + process::exit(2); + } + #[cfg(test)] + panic!("{}\n", errmsg); +} + pub mod errors { error_chain! { foreign_links {