You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
926 B
30 lines
926 B
use std::collections::HashMap;
|
|
|
|
use symbols::Symbol;
|
|
use resources::Resource;
|
|
|
|
pub trait SymbolRepository<'a> {
|
|
fn get_symbol(&'a self, resource: &Resource) -> Option<Box<Symbol + 'a>>;
|
|
}
|
|
|
|
impl<'a, C> SymbolRepository<'a> for C where C: Fn(&Resource) -> Option<Box<Symbol + 'a>> {
|
|
fn get_symbol(&'a self, resource: &Resource) -> Option<Box<Symbol + 'a>> {
|
|
self(resource)
|
|
}
|
|
}
|
|
|
|
pub struct DispatchingSymbolRepository<'a> {
|
|
repositories: HashMap<&'a str, Box<SymbolRepository<'a> + 'a>>
|
|
}
|
|
|
|
impl<'a> DispatchingSymbolRepository<'a> {
|
|
pub fn new(repositories: HashMap<&'a str, Box<SymbolRepository<'a> + 'a>>) -> Self {
|
|
DispatchingSymbolRepository { repositories }
|
|
}
|
|
}
|
|
|
|
impl<'a> SymbolRepository<'a> for DispatchingSymbolRepository<'a> {
|
|
fn get_symbol(&'a self, resource: &Resource) -> Option<Box<Symbol + 'a>> {
|
|
self.repositories.get(resource.get_type()).and_then(|repo| repo.get_symbol(resource))
|
|
}
|
|
}
|