1use std::error::Error;
2
3pub trait CSVParse{
4 fn parse_elem(&mut self, path: &str) -> Result<(), Box<dyn Error>>;
5}
6
7impl CSVParse for Vec<Vec<String>> {
8 fn parse_elem(&mut self, path: &str) -> Result<(), Box<dyn Error>> {
9 let reader = csv::Reader::from_path(path)?;
10 self.clear();
11 for result in reader.into_records() {
12 self.push(result?.deserialize::<Vec<String>>(None)?);
13 }
14 Ok(())
15 }
16}
17
18impl CSVParse for Vec<Vec<f32>> {
19 fn parse_elem(&mut self, path: &str) -> Result<(), Box<dyn Error>> {
20 self.clear();
21 let mut parse_str: Vec<Vec<String>> = vec![];
22 parse_str.parse_elem(path)?;
23
24 for row in parse_str.iter(){
25 let mut row_f32: Vec<f32> = vec![];
26 for col in row.iter(){
27 if let Ok(num) = col.parse::<f32>(){
28 row_f32.push(num);
29 }else{
30 row_f32.push(0.0);
31 }
32 }
33 self.push(row_f32);
34 }
35
36 Ok(())
37 }
38}