forked from fcsonline/drill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.rs
108 lines (88 loc) · 3 KB
/
reader.rs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::path::Path;
pub fn read_file(filepath: &str) -> String {
// Create a path to the desired file
let path = Path::new(filepath);
let display = path.display();
// Open the path in read-only mode, returns `io::Result<File>`
let mut file = match File::open(path) {
Err(why) => panic!("couldn't open {}: {}", display, why),
Ok(file) => file,
};
// Read the file contents into a string, returns `io::Result<usize>`
let mut content = String::new();
if let Err(why) = file.read_to_string(&mut content) {
panic!("couldn't read {}: {}", display, why);
}
content
}
pub fn read_file_as_yml(filepath: &str) -> Vec<yaml_rust::Yaml> {
let content = read_file(filepath);
yaml_rust::YamlLoader::load_from_str(content.as_str()).unwrap()
}
pub fn read_yaml_doc_accessor<'a>(doc: &'a yaml_rust::Yaml, accessor: Option<&str>) -> &'a Vec<yaml_rust::Yaml> {
if let Some(accessor_id) = accessor {
match doc[accessor_id].as_vec() {
Some(items) => items,
None => {
println!("Node missing on config: {accessor_id}");
println!("Exiting.");
std::process::exit(1)
}
}
} else {
doc.as_vec().unwrap()
}
}
pub fn read_file_as_yml_array(filepath: &str) -> yaml_rust::yaml::Array {
let path = Path::new(filepath);
let display = path.display();
let file = match File::open(path) {
Err(why) => panic!("couldn't open {}: {}", display, why),
Ok(file) => file,
};
let reader = BufReader::new(file);
let mut items = yaml_rust::yaml::Array::new();
for line in reader.lines() {
match line {
Ok(text) => {
items.push(yaml_rust::Yaml::String(text));
}
Err(e) => println!("error parsing line: {e:?}"),
}
}
items
}
// TODO: Try to split this fn into two
pub fn read_csv_file_as_yml(filepath: &str, quote: u8) -> yaml_rust::yaml::Array {
// Create a path to the desired file
let path = Path::new(filepath);
let display = path.display();
// Open the path in read-only mode, returns `io::Result<File>`
let file = match File::open(path) {
Err(why) => panic!("couldn't open {}: {}", display, why),
Ok(file) => file,
};
let mut rdr = csv::ReaderBuilder::new().has_headers(true).quote(quote).from_reader(file);
let mut items = yaml_rust::yaml::Array::new();
let headers = match rdr.headers() {
Err(why) => panic!("error parsing header: {:?}", why),
Ok(h) => h.clone(),
};
for result in rdr.records() {
match result {
Ok(record) => {
let mut linked_hash_map = linked_hash_map::LinkedHashMap::new();
for (i, header) in headers.iter().enumerate() {
let item_key = yaml_rust::Yaml::String(header.to_string());
let item_value = yaml_rust::Yaml::String(record.get(i).unwrap().to_string());
linked_hash_map.insert(item_key, item_value);
}
items.push(yaml_rust::Yaml::Hash(linked_hash_map));
}
Err(e) => println!("error parsing header: {e:?}"),
}
}
items
}