summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..6564c84
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,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() - 2].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())
+}