2019-01-12 21:24:17 +00:00
|
|
|
use crate::export::Export;
|
2019-01-11 03:59:57 +00:00
|
|
|
use hashbrown::{hash_map::Entry, HashMap};
|
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
pub trait LikeNamespace {
|
2019-01-13 21:44:14 +00:00
|
|
|
fn get_export(&mut self, name: &str) -> Option<Export>;
|
2019-01-12 21:24:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
pub struct ImportObject {
|
|
|
|
map: HashMap<String, Box<dyn LikeNamespace>>,
|
2019-01-11 03:59:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
impl ImportObject {
|
2019-01-11 03:59:57 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
map: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
pub fn register<S, N>(&mut self, name: S, namespace: N) -> Option<Box<dyn LikeNamespace>>
|
2019-01-13 03:02:19 +00:00
|
|
|
where
|
|
|
|
S: Into<String>,
|
2019-01-21 22:43:04 +00:00
|
|
|
N: LikeNamespace + 'static,
|
2019-01-13 03:02:19 +00:00
|
|
|
{
|
2019-01-12 21:24:17 +00:00
|
|
|
match self.map.entry(name.into()) {
|
|
|
|
Entry::Vacant(empty) => {
|
|
|
|
empty.insert(Box::new(namespace));
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Entry::Occupied(mut occupied) => Some(occupied.insert(Box::new(namespace))),
|
|
|
|
}
|
2019-01-11 03:59:57 +00:00
|
|
|
}
|
2019-01-12 21:24:17 +00:00
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
pub fn get_namespace(&mut self, namespace: &str) -> Option<&mut (dyn LikeNamespace + 'static)> {
|
2019-01-13 21:44:14 +00:00
|
|
|
self.map
|
|
|
|
.get_mut(namespace)
|
|
|
|
.map(|namespace| &mut **namespace)
|
2019-01-11 03:59:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-13 04:48:21 +00:00
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
pub struct Namespace {
|
2019-01-13 21:44:14 +00:00
|
|
|
map: HashMap<String, Export>,
|
2019-01-13 04:48:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
impl Namespace {
|
2019-01-13 04:48:21 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
map: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert(&mut self, name: impl Into<String>, export: Export) -> Option<Export> {
|
|
|
|
self.map.insert(name.into(), export)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
impl LikeNamespace for Namespace {
|
2019-01-13 21:44:14 +00:00
|
|
|
fn get_export(&mut self, name: &str) -> Option<Export> {
|
2019-01-13 04:48:21 +00:00
|
|
|
self.map.get(name).cloned()
|
|
|
|
}
|
2019-01-13 21:44:14 +00:00
|
|
|
}
|