use std::collections::HashMap;
pub fn parse_robots(txt: &str) -> HashMap<&str, Vec<&str>> {
let mut map: HashMap<&str, Vec<&str>> = HashMap::new();
let mut active_agents: Vec<&str> = Vec::new();
let mut was_user = false;
let txt = txt.lines().filter(|x| !x.trim_start().starts_with('#'));
for line in txt {
if let Some((_, agent)) = line.trim().split_once("User-agent:") {
if was_user == false {
active_agents.clear();
}
active_agents.push(agent.trim());
was_user = true;
} else if let Some((_, disallow)) = line.trim().split_once("Disallow:") {
for a in &active_agents {
if let Some(entry) = map.get_mut(a) {
entry.push(disallow.trim());
} else {
map.insert(a, vec![disallow.trim()]);
}
}
was_user = false;
}
}
map
}