use crate::ast::*;
use std::collections::BTreeMap;
use std::ops::Index;
#[derive(Default, Clone)]
pub struct Env {
pub(crate) env: BTreeMap<Path, ModuleEnv>,
}
#[derive(Clone)]
pub struct ModuleEnv {
pub(crate) module: BTreeMap<Ident, ModSymbol>,
#[cfg_attr(not(feature = "hir"), allow(unused))]
pub(crate) attrs: Attrs,
}
impl Env {
pub(crate) fn insert(&mut self, path: Path, module: ModuleEnv) {
self.env.insert(path, module);
}
pub fn get(&self, path: &Path, name: &str) -> Option<&ModSymbol> {
self.env.get(path).and_then(|m| m.module.get(name))
}
pub fn iter_items(&self) -> impl Iterator<Item = (&Path, &Ident, &ModSymbol)> + '_ {
self.env
.iter()
.flat_map(|(k, v)| v.module.iter().map(move |v2| (k, v2.0, v2.1)))
}
pub fn iter_modules(&self) -> impl Iterator<Item = (&Path, &ModuleEnv)> + '_ {
self.env.iter()
}
}
impl ModuleEnv {
pub(crate) fn new(attrs: Attrs) -> Self {
Self {
module: Default::default(),
attrs,
}
}
pub(crate) fn insert(&mut self, name: Ident, symbol: ModSymbol) -> Option<ModSymbol> {
self.module.insert(name, symbol)
}
pub fn get(&self, name: &str) -> Option<&ModSymbol> {
self.module.get(name)
}
pub fn iter(&self) -> impl Iterator<Item = (&Ident, &ModSymbol)> + '_ {
self.module.iter()
}
pub fn names(&self) -> impl Iterator<Item = &Ident> + '_ {
self.module.keys()
}
pub fn items(&self) -> impl Iterator<Item = &ModSymbol> + '_ {
self.module.values()
}
}
impl Index<&Path> for Env {
type Output = ModuleEnv;
fn index(&self, i: &Path) -> &ModuleEnv {
&self.env[i]
}
}
impl Index<&str> for ModuleEnv {
type Output = ModSymbol;
fn index(&self, i: &str) -> &ModSymbol {
&self.module[i]
}
}