|
| 1 | +use std::{ |
| 2 | + any::{Any, TypeId}, |
| 3 | + collections::HashMap, |
| 4 | + sync::Arc, |
| 5 | +}; |
| 6 | + |
| 7 | +#[derive(thiserror::Error, Debug)] |
| 8 | +pub enum RepositoryError { |
| 9 | + #[error("Internal error: {0}")] |
| 10 | + Internal(String), |
| 11 | +} |
| 12 | + |
| 13 | +#[async_trait::async_trait] |
| 14 | +pub trait Repository<T>: Send + Sync { |
| 15 | + async fn get(&self, key: String) -> Result<Option<T>, RepositoryError>; |
| 16 | + async fn list(&self) -> Result<Vec<T>, RepositoryError>; |
| 17 | + async fn set(&self, key: String, value: T) -> Result<(), RepositoryError>; |
| 18 | + async fn remove(&self, key: String) -> Result<(), RepositoryError>; |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(Default)] |
| 22 | +pub struct RepositoryMap { |
| 23 | + stores: HashMap<TypeId, Box<dyn Any + Send + Sync>>, |
| 24 | +} |
| 25 | + |
| 26 | +impl std::fmt::Debug for RepositoryMap { |
| 27 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 28 | + f.debug_struct("RepositoryMap") |
| 29 | + .field("stores", &self.stores.keys()) |
| 30 | + .finish() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl RepositoryMap { |
| 35 | + pub fn new() -> Self { |
| 36 | + RepositoryMap { |
| 37 | + stores: HashMap::new(), |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + pub fn insert<T: 'static>(&mut self, value: Arc<dyn Repository<T>>) { |
| 42 | + self.stores.insert(TypeId::of::<T>(), Box::new(value)); |
| 43 | + } |
| 44 | + |
| 45 | + pub fn get<T: 'static>(&self) -> Option<Arc<dyn Repository<T>>> { |
| 46 | + self.stores |
| 47 | + .get(&TypeId::of::<T>()) |
| 48 | + .and_then(|boxed| boxed.downcast_ref::<Arc<dyn Repository<T>>>()) |
| 49 | + .map(Arc::clone) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[cfg(test)] |
| 54 | +mod tests { |
| 55 | + use super::*; |
| 56 | + |
| 57 | + macro_rules! impl_repository { |
| 58 | + ($name:ident, $ty:ty) => { |
| 59 | + #[async_trait::async_trait] |
| 60 | + impl Repository<$ty> for $name { |
| 61 | + async fn get(&self, _key: String) -> Result<Option<$ty>, RepositoryError> { |
| 62 | + Ok(Some(self.0.clone())) |
| 63 | + } |
| 64 | + async fn list(&self) -> Result<Vec<$ty>, RepositoryError> { |
| 65 | + unimplemented!() |
| 66 | + } |
| 67 | + async fn set(&self, _key: String, _value: $ty) -> Result<(), RepositoryError> { |
| 68 | + unimplemented!() |
| 69 | + } |
| 70 | + async fn remove(&self, _key: String) -> Result<(), RepositoryError> { |
| 71 | + unimplemented!() |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + #[derive(PartialEq, Eq, Debug)] |
| 78 | + struct TestA(usize); |
| 79 | + #[derive(PartialEq, Eq, Debug)] |
| 80 | + struct TestB(String); |
| 81 | + #[derive(PartialEq, Eq, Debug)] |
| 82 | + struct TestC(Vec<u8>); |
| 83 | + |
| 84 | + impl_repository!(TestA, usize); |
| 85 | + impl_repository!(TestB, String); |
| 86 | + impl_repository!(TestC, Vec<u8>); |
| 87 | + |
| 88 | + #[tokio::test] |
| 89 | + async fn test_repository_map() { |
| 90 | + let a = Arc::new(TestA(145832)); |
| 91 | + let b = Arc::new(TestB("test".to_string())); |
| 92 | + let c = Arc::new(TestC(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])); |
| 93 | + |
| 94 | + let mut map = RepositoryMap::new(); |
| 95 | + |
| 96 | + async fn get<T: 'static>(map: &RepositoryMap) -> Option<T> { |
| 97 | + map.get::<T>().unwrap().get(String::new()).await.unwrap() |
| 98 | + } |
| 99 | + |
| 100 | + assert!(map.get::<usize>().is_none()); |
| 101 | + assert!(map.get::<String>().is_none()); |
| 102 | + assert!(map.get::<Vec<u8>>().is_none()); |
| 103 | + |
| 104 | + map.insert(a.clone()); |
| 105 | + assert_eq!(get(&map).await, Some(a.0)); |
| 106 | + assert!(map.get::<String>().is_none()); |
| 107 | + assert!(map.get::<Vec<u8>>().is_none()); |
| 108 | + |
| 109 | + map.insert(b.clone()); |
| 110 | + assert_eq!(get(&map).await, Some(a.0)); |
| 111 | + assert_eq!(get(&map).await, Some(b.0.clone())); |
| 112 | + assert!(map.get::<Vec<u8>>().is_none()); |
| 113 | + |
| 114 | + map.insert(c.clone()); |
| 115 | + assert_eq!(get(&map).await, Some(a.0)); |
| 116 | + assert_eq!(get(&map).await, Some(b.0.clone())); |
| 117 | + assert_eq!(get(&map).await, Some(c.0.clone())); |
| 118 | + } |
| 119 | +} |
0 commit comments