blob: 963b7335e56515e8c73b929f74d9e0e88aa6de8e (
plain)
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
|
use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
use std::path::PathBuf;
pub fn load_groups(path: &PathBuf) -> io::Result<HashMap<String, Vec<String>>> {
let lines = read_lines(path)?;
let mut groups = HashMap::new();
let mut group_name = String::new();
for mut line in lines.map_while(Result::ok) {
line.truncate(line.trim_end().len());
if line.starts_with("#") || line.is_empty() {
continue;
}
if line.starts_with("[") && line.ends_with("]") {
group_name = line[1..line.len() - 1].to_string();
if !groups.contains_key(&group_name) {
groups.insert(group_name.clone(), vec![]);
}
continue;
}
if group_name.is_empty() {
groups.insert(group_name.clone(), vec![]);
}
groups.get_mut(&group_name).unwrap().push(line);
}
Ok(groups)
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
|