From bc0527e324fa2041d0346cf3c4a202f2d2d428d2 Mon Sep 17 00:00:00 2001 From: Anders Nilsson <andersn@control.lth.se> Date: Thu, 3 Dec 2015 13:30:13 +0100 Subject: [PATCH] Better error handling, database file as input option --- city-pop/src/main.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/city-pop/src/main.rs b/city-pop/src/main.rs index 90278a4..63d0715 100644 --- a/city-pop/src/main.rs +++ b/city-pop/src/main.rs @@ -4,7 +4,7 @@ extern crate csv; use getopts::Options; -use std::{env,fs}; +use std::{env,fs,io}; use std::error::Error; use std::path::Path; @@ -49,8 +49,11 @@ fn print_usage(program: &str, opts: Options) { fn search<P: AsRef<Path>> (file_path: &Option<P>, city: &str) -> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> { let mut found = vec![]; - let file = try!(fs::File::open(file_path)); - let mut rdr = csv::Reader::from_reader(file); + let input: Box<io::Read> = match *file_path { + None => Box::new(io::stdin()), + Some(ref file_path) => Box::new(try!(fs::File::open(file_path))), + }; + let mut rdr = csv::Reader::from_reader(input); for row in rdr.decode::<Row>() { let row = row.unwrap(); match row.population { -- GitLab