use std::collections::{HashMap, HashSet};
use tdlib_tl_parser::tl::{Category, Definition, Type};
pub(crate) struct Metadata<'a> {
recursing_defs: HashSet<&'a String>,
defs_with_type: HashMap<&'a String, Vec<&'a Definition>>,
}
impl<'a> Metadata<'a> {
pub fn new(definitions: &'a [Definition]) -> Self {
let mut metadata = Self {
recursing_defs: HashSet::new(),
defs_with_type: HashMap::new(),
};
definitions
.iter()
.filter(|d| d.category == Category::Types)
.for_each(|d| {
if d.params.iter().any(|p| p.ty.name == d.ty.name) {
metadata.recursing_defs.insert(&d.name);
}
metadata
.defs_with_type
.entry(&d.ty.name)
.or_insert_with(Vec::new)
.push(d);
});
metadata
}
pub fn is_recursive_def(&self, def: &Definition) -> bool {
self.recursing_defs.contains(&def.name)
}
pub fn defs_with_type(&self, ty: &'a Type) -> &Vec<&Definition> {
&self.defs_with_type[&ty.name]
}
}