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