1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//! Commer-Separated-Variable file handling.

use crate::{data::Table, err::Error, file::Load};
use std::{
    fs::File,
    io::{BufRead, BufReader},
    path::Path,
    str::FromStr,
};

impl<T: FromStr> Load for Table<T> {
    #[inline]
    fn load_data(path: &Path) -> Result<Self, Error> {
        let lines: Vec<_> = BufReader::new(File::open(path)?)
            .lines()
            .map(Result::unwrap)
            .filter(|line| !line.starts_with("//"))
            .collect();

        let mut rows = Vec::with_capacity(lines.len());
        for mut line in lines {
            line.retain(|c| !c.is_whitespace());
            let row = line
                .split(',')
                .map(str::parse)
                .filter_map(Result::ok)
                .collect();
            rows.push(row);
        }

        Ok(Self::new(rows))
    }
}