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()) }