use crate::query::field_path::FieldPath;
use std::collections::HashMap;
use std::collections::HashSet;
#[derive(Debug)]
pub struct PathTree {
roots: HashSet<String>,
nodes: HashMap<String, HashSet<String>>,
}
impl PathTree {
pub fn new() -> Self {
PathTree {
roots: HashSet::new(),
nodes: HashMap::new(),
}
}
pub fn roots(&self) -> &HashSet<String> {
&self.roots
}
pub fn nodes(&self, name: &str) -> Option<&HashSet<String>> {
self.nodes.get(name)
}
pub fn insert(&mut self, path: &FieldPath) {
let mut parents = path.step_up().skip(1);
for a in path.step_up() {
match parents.next() {
Some(p) => {
let j = self
.nodes
.entry(p.as_str().to_string())
.or_insert_with(HashSet::new);
j.insert(a.as_str().to_string());
if self.nodes.contains_key(a.as_str()) {
break;
}
}
None => {
self.roots.insert(a.as_str().to_string());
break;
}
}
}
}
}