1 use std::env;
2 use std::error::Error;
3 use std::ffi::OsString;
4 use std::fs::File;
5 use std::process;
6
run() -> Result<(), Box<dyn Error>>7 fn run() -> Result<(), Box<dyn Error>> {
8 let file_path = get_first_arg()?;
9 let file = File::open(file_path)?;
10 let mut rdr = csv::Reader::from_reader(file);
11 for result in rdr.records() {
12 let record = result?;
13 println!("{:?}", record);
14 }
15 Ok(())
16 }
17
18 /// Returns the first positional argument sent to this process. If there are no
19 /// positional arguments, then this returns an error.
get_first_arg() -> Result<OsString, Box<dyn Error>>20 fn get_first_arg() -> Result<OsString, Box<dyn Error>> {
21 match env::args_os().nth(1) {
22 None => Err(From::from("expected 1 argument, but got none")),
23 Some(file_path) => Ok(file_path),
24 }
25 }
26
main()27 fn main() {
28 if let Err(err) = run() {
29 println!("{}", err);
30 process::exit(1);
31 }
32 }
33