Skip to content
Snippets Groups Projects
Commit bc0527e3 authored by Administrator's avatar Administrator
Browse files

Better error handling, database file as input option

parent 8a5c2117
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ extern crate csv; ...@@ -4,7 +4,7 @@ extern crate csv;
use getopts::Options; use getopts::Options;
use std::{env,fs}; use std::{env,fs,io};
use std::error::Error; use std::error::Error;
use std::path::Path; use std::path::Path;
...@@ -49,8 +49,11 @@ fn print_usage(program: &str, opts: Options) { ...@@ -49,8 +49,11 @@ fn print_usage(program: &str, opts: Options) {
fn search<P: AsRef<Path>> (file_path: &Option<P>, city: &str) fn search<P: AsRef<Path>> (file_path: &Option<P>, city: &str)
-> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> { -> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> {
let mut found = vec![]; let mut found = vec![];
let file = try!(fs::File::open(file_path)); let input: Box<io::Read> = match *file_path {
let mut rdr = csv::Reader::from_reader(file); 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>() { for row in rdr.decode::<Row>() {
let row = row.unwrap(); let row = row.unwrap();
match row.population { match row.population {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment