diff options
author | Ryan Geary <rtgnj42@gmail.com> | 2020-03-17 13:41:44 -0400 |
---|---|---|
committer | Ryan Geary <rtgnj42@gmail.com> | 2020-03-17 13:43:49 -0400 |
commit | 41e060680ea153fa8f245efc53f870bbb80faf89 (patch) | |
tree | 57504935f69df1cd8ed21a4ef758bb1fece34933 /src | |
parent | dfe4c2cad0e35996136f320b675265ce223bb326 (diff) |
Handle failure to open input file
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index dbefafa..9bea756 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::fs::File; use std::io::{self, Read, Write}; +use std::process; use structopt::StructOpt; #[macro_use] @@ -17,7 +18,14 @@ fn main() { let config = Config::new(opt); let read = match &config.opt.input { - Some(f) => Box::new(File::open(f).expect("Could not open file")) as Box<dyn Read>, + Some(f) => match File::open(f) { + Ok(fh) => Box::new(fh) as Box<dyn Read>, + Err(e) => { + eprintln!("Failed to open file: {}", e); + // exit code of 3 means failure to open input file + process::exit(3); + } + }, None => Box::new(io::stdin()) as Box<dyn Read>, }; |