Skip to content

Instantly share code, notes, and snippets.

@tarqd
Forked from jnschaeffer/indego.rs
Last active January 20, 2020 05:35
Show Gist options
  • Save tarqd/e447f9354aa7e739fc900841cb8c9af7 to your computer and use it in GitHub Desktop.
Save tarqd/e447f9354aa7e739fc900841cb8c9af7 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>> {
Reader::from_path(path)?
.deserialize()
.collect::<Result<Vec<Record>, _ >>()
.map_err(From::from)
}
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