From b29c82da90bf3843e2a551c36cd156185794b505 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Tue, 23 Dec 2025 12:34:46 +0100 Subject: Initial commit --- src/config.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/config.rs (limited to 'src/config.rs') 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>> { + 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

(filename: P) -> io::Result>> +where + P: AsRef, +{ + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} -- cgit v1.2.3-70-g09d2