-
Notifications
You must be signed in to change notification settings - Fork 522
Expand file tree
/
Copy pathcsv_reader.rs
More file actions
57 lines (50 loc) · 1.73 KB
/
csv_reader.rs
File metadata and controls
57 lines (50 loc) · 1.73 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::env::current_dir;
use std::fs::File;
use std::io::{BufRead, BufReader, Lines, Error};
use std::io::ErrorKind::InvalidInput;
use std::path::PathBuf;
pub struct CSVReader {
iter: Lines<BufReader<File>>,
}
impl CSVReader {
pub fn new(csv_name: &str) -> Result<CSVReader, Error> {
// Assumes we're called from <open-location-code root>/rust
let project_root = current_dir()?;
let olc_root: PathBuf = match project_root.file_name().and_then(|n| n.to_str()) {
Some("rust") => project_root
.parent()
.map(|p| p.to_path_buf())
.ok_or_else(|| Error::new(InvalidInput, "Could not find project root parent")),
Some("_main") => Ok(project_root.clone()),
_ => {
return Err(Error::new(InvalidInput, format!(
"Expected current dir to end with 'rust' or '_main', got {:?}",
project_root
)));
}
}?;
let csv_path = olc_root.join("test_data").join(csv_name);
let file = File::open(&csv_path).map_err(|e| {
Error::new(e.kind(), format!(
"Failed to open CSV file at {:?}: {} (Current dir: {:?})",
csv_path, e, project_root
))
})?;
Ok(CSVReader {
iter: BufReader::new(file).lines(),
})
}
}
impl Iterator for CSVReader {
type Item = String;
fn next(&mut self) -> Option<String> {
// Iterate lines in the CSV file, dropping empty & comment lines
while let Some(Ok(s)) = self.iter.next() {
if s.is_empty() || s.starts_with("#") {
continue;
}
return Some(s);
}
None
}
}