1use std::io::{Error, ErrorKind};
2
3use serde::de::DeserializeOwned;
4
5pub fn read<D>(path: &str, has_header: bool) -> Result<Vec<D>, std::io::Error>
10where
11 D: DeserializeOwned,
12{
13 let mut result = Vec::new();
15 let mut rdr = csv::ReaderBuilder::new()
17 .has_headers(has_header)
18 .from_path(path)?;
19 for row in rdr.deserialize() {
20 let row: D = row?;
21 result.push(row);
22 }
23
24 Ok(result)
25}
26
27pub fn read_header<D>(path: &str) -> Result<D, std::io::Error>
31where
32 D: DeserializeOwned,
33{
34 let mut rdr = csv::ReaderBuilder::new()
36 .has_headers(false)
37 .from_path(path)?;
38 let row = rdr
40 .deserialize::<D>()
41 .into_iter()
42 .next()
43 .ok_or(Error::new(ErrorKind::Other, "No data found"))?;
44 let row: D = row?;
45 Ok(row)
46}