Last active
January 20, 2020 04:24
-
-
Save jnschaeffer/31ccaa4a6c58202603ffe3842323f306 to your computer and use it in GitHub Desktop.
Indego CSV parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use std::error::Error; | |
use std::process; | |
use csv::Reader; | |
use serde::Deserialize; | |
#[derive(Debug, Deserialize)] | |
enum PassholderType { | |
Indego30, | |
Indego365, | |
IndegoFlex, | |
#[serde(rename(deserialize = "Day Pass"))] | |
DayPass, | |
} | |
#[derive(Debug, Deserialize)] | |
#[serde(rename_all = "lowercase")] | |
enum BikeType { | |
Standard, | |
Electric, | |
} | |
#[derive(Debug, Deserialize)] | |
struct Record { | |
trip_id: u32, | |
duration: u32, | |
start_time: String, | |
end_time: String, | |
start_station: u32, | |
start_lat: Option<f64>, | |
start_lon: Option<f64>, | |
end_station: u32, | |
end_lat: Option<f64>, | |
end_lon: Option<f64>, | |
bike_id: u32, | |
passholder_type: PassholderType, | |
bike_type: BikeType, | |
} | |
fn load_records(path: String) -> Result<Vec<Record>, Box<dyn Error>> { | |
let mut rdr = Reader::from_path(path)?; | |
let mut vec = Vec::new(); | |
for result in rdr.deserialize() { | |
let record: Record = result?; | |
vec.push(record); | |
} | |
Ok(vec) | |
} | |
fn main() { | |
let path = env::args().nth(1).expect("no path given"); | |
match load_records(path) { | |
Err(err) => { | |
println!("error: {:?}", err); | |
process::exit(1); | |
} | |
Ok(records) => { | |
println!("got {:?} records", records.len()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment