-
-
Save tarqd/e447f9354aa7e739fc900841cb8c9af7 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>> { | |
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