I'm working on a template for importing and working with CSV files. I've got serde and csv working, but I've landed on the following type and I'm not sure putting a bunch of hashmaps inside a vec is the optimal approach. If it is, great! But something tells me this may end up being slow when processing and there might be a built in object that's better suited to this task.
Is this a reasonable object type or is there something better I should be using?
Vec<
Hashmap< String, Option<String> >
>
Constraints:
- I want to ignore but preserve the columns I don't use (otherwise I'd use an explicit struct for serde)
- I want to be able to work with a few columns based on their column name, irrespective of the column order. i.e. if someone changes the order but keeps the headers correct I still want it to work (thus the hashmap)
- I want to be able to iterate through the rows (thus the vec)
//snip /*uses csv, serde*/
pub fn csv_from_path(filename: &str) -> Result<Vec<HashMap<String,Option<String>>>,Box<Error>> {
let mut output: Vec<HashMap<String,Option<String>>> = Vec::new();
let mut digester = csv::Reader::from_path(filename)?;
for row in digester.deserialize() {
let record: HashMap<String,Option<String>> = row?;
output.push(record);
//println!("{:?}",record)
}
Ok(output)
}
//snip
I can post full source code, but I'm designing it as a template to be used for other projects so it's not really a minimal code example.