From abc51419885031125500178d51ccc70e04e94889 Mon Sep 17 00:00:00 2001 From: Marco Ieni <11428655+MarcoIeni@users.noreply.github.com> Date: Wed, 30 Dec 2020 00:36:42 +0100 Subject: refactor main in order to call process::exit once (#480) * refactor main in order to call process::exit once Co-authored-by: tl-marco-ieni Co-authored-by: Dan Davison --- src/main.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 75b813af..cad56b93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,6 @@ mod syntect_color; mod tests; use std::io::{self, ErrorKind, Read, Write}; -use std::mem; use std::path::PathBuf; use std::process; @@ -53,19 +52,20 @@ pub mod errors { } #[cfg(not(tarpaulin_include))] -fn main() -> std::io::Result<()> { +/// `Ok` of the `Result` contains with the exit code value +fn run_app() -> std::io::Result { let assets = HighlightingAssets::new(); let opt = cli::Opt::from_args_and_git_config(&mut git_config::GitConfig::try_create(), assets); if opt.list_languages { list_languages()?; - process::exit(0); + return Ok(0); } else if opt.list_syntax_themes { list_syntax_themes()?; - process::exit(0); + return Ok(0); } else if opt.show_syntax_themes { show_syntax_themes()?; - process::exit(0); + return Ok(0); } let _show_config = opt.show_config; @@ -75,7 +75,7 @@ fn main() -> std::io::Result<()> { let stdout = io::stdout(); let mut stdout = stdout.lock(); show_config(&config, &mut stdout)?; - process::exit(0); + return Ok(0); } let mut output_type = OutputType::from_mode(config.paging_mode, None, &config).unwrap(); @@ -88,17 +88,23 @@ fn main() -> std::io::Result<()> { &config, &mut writer, ); - mem::drop(output_type); - process::exit(exit_code); + return Ok(exit_code); } if let Err(error) = delta(io::stdin().lock().byte_lines(), &mut writer, &config) { match error.kind() { - ErrorKind::BrokenPipe => process::exit(0), + ErrorKind::BrokenPipe => return Ok(0), _ => eprintln!("{}", error), } }; - Ok(()) + Ok(0) +} + +#[cfg(not(tarpaulin_include))] +fn main() -> std::io::Result<()> { + let exit_code = run_app()?; + // when you call process::exit, no destructors are called, so we want to do it only once, here + process::exit(exit_code); } /// Run `diff -u` on the files provided on the command line and display the output. -- cgit v1.2.3