A library for writing host-specific, single-binary configuration management and deployment tools
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.

36 lines
975 B

7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. use std::collections::HashMap;
  2. use resources::Resource;
  3. use symbols::Symbol;
  4. pub trait SymbolRepository<'a> {
  5. fn get_symbol(&'a self, resource: &Resource) -> Option<Box<dyn Symbol + 'a>>;
  6. }
  7. impl<'a, C> SymbolRepository<'a> for C
  8. where
  9. C: Fn(&Resource) -> Option<Box<dyn Symbol + 'a>>,
  10. {
  11. fn get_symbol(&'a self, resource: &Resource) -> Option<Box<dyn Symbol + 'a>> {
  12. self(resource)
  13. }
  14. }
  15. pub struct DispatchingSymbolRepository<'a> {
  16. repositories: HashMap<&'a str, Box<dyn SymbolRepository<'a> + 'a>>,
  17. }
  18. impl<'a> DispatchingSymbolRepository<'a> {
  19. pub fn new(repositories: HashMap<&'a str, Box<dyn SymbolRepository<'a> + 'a>>) -> Self {
  20. DispatchingSymbolRepository { repositories }
  21. }
  22. }
  23. impl<'a> SymbolRepository<'a> for DispatchingSymbolRepository<'a> {
  24. fn get_symbol(&'a self, resource: &Resource) -> Option<Box<dyn Symbol + 'a>> {
  25. self
  26. .repositories
  27. .get(resource.get_type())
  28. .and_then(|repo| repo.get_symbol(resource))
  29. }
  30. }