Skip to content

Instantly share code, notes, and snippets.

@jnschaeffer
Last active January 20, 2020 04:24
Show Gist options
  • Save jnschaeffer/31ccaa4a6c58202603ffe3842323f306 to your computer and use it in GitHub Desktop.
Save jnschaeffer/31ccaa4a6c58202603ffe3842323f306 to your computer and use it in GitHub Desktop.
Indego CSV parsing
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