diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2025-12-23 12:34:46 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2025-12-23 12:34:46 +0100 |
| commit | b29c82da90bf3843e2a551c36cd156185794b505 (patch) | |
| tree | 1eecf8721bcb327c815bb3549247d22579909794 /src/config.rs | |
Initial commit
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 37 |
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()) +} |
