From 92106a053f87d44d5dba28a628aaeecba8417b5a Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sun, 1 Dec 2019 13:11:13 +0200 Subject: Better error handling --- src/bin/day_1.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/bin/day_1.rs b/src/bin/day_1.rs index b9a6c18..f40237d 100644 --- a/src/bin/day_1.rs +++ b/src/bin/day_1.rs @@ -2,6 +2,7 @@ use derive_more; use std::io; use std::io::prelude::*; use std::iter; +use std::process; use structopt::StructOpt; @@ -21,10 +22,19 @@ fn main() { let stdin = io::stdin(); let opt = Opt::from_args(); - let input = stdin - .lock() - .lines() - .map(|l| l.unwrap().parse::().unwrap()); + let input = stdin.lock().lines().map(|l| match l { + Ok(s) => match s.parse::() { + Ok(module) => module, + Err(e) => { + eprintln!("Invalid input \"{}\": {}", s, e); + process::exit(1); + } + }, + Err(e) => { + eprintln!("Error reading input: {}", e); + process::exit(1); + } + }); println!("{}", fuel_required(input, opt.include_fuel_weight)) } -- cgit v1.2.3