From 9c4c66362cfcab9e61a7b15dc6b2e18b82890457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Tue, 21 May 2024 09:05:45 +0200 Subject: [PATCH] . --- rear/src/admin/domain.rs | 86 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/rear/src/admin/domain.rs b/rear/src/admin/domain.rs index 2450603..45f56f7 100644 --- a/rear/src/admin/domain.rs +++ b/rear/src/admin/domain.rs @@ -350,11 +350,28 @@ pub mod repository { } } + pub trait PrimaryKeyType { + fn key_as_string(&self) -> String; + fn key_from_string(&self, s: String) -> Self; + } + + impl PrimaryKeyType for i64 { + fn key_as_string(&self) -> String { + self.to_string() + } + + fn key_from_string(&self, s: String) -> Self { + s.parse::().unwrap() as i64 + } + } + #[async_trait] pub trait AdminRepository: Send + Sync { + type Key: PrimaryKeyType; + async fn info(&self, context: &RepositoryContext) -> RepositoryInfo; async fn list(&self, context: &RepositoryContext) -> RepositoryList; - async fn get(&self, context: &RepositoryContext, id: LookupKey) -> Option; + async fn get(&self, context: &RepositoryContext, id: Self::Key) -> Option; async fn create( &mut self, context: &RepositoryContext, @@ -363,15 +380,76 @@ pub mod repository { async fn update( &mut self, context: &RepositoryContext, - id: LookupKey, + id: Self::Key, data: Value, ) -> Option; async fn replace( &mut self, context: &RepositoryContext, - id: LookupKey, + id: Self::Key, data: Value, ) -> Option; - async fn delete(&mut self, context: &RepositoryContext, id: LookupKey) -> Option; + async fn delete(&mut self, context: &RepositoryContext, id: Self::Key) -> Option; + } + + use std::any::Any; + + trait Key: Any { + fn as_any(&self) -> &dyn Any; + fn from_str(s: &str) -> Self + where + Self: Sized; + fn to_string(&self) -> String; + } + + impl Key for T { + fn as_any(&self) -> &dyn Any { + self + } + } + + impl Key for String { + fn from_str(s: &str) -> Self { + s.to_string() + } + + fn to_string(&self) -> String { + self.clone() + } + } + + impl Key for i64 { + fn from_str(s: &str) -> Self { + s.parse().expect("Invalid i64 string") + } + + fn to_string(&self) -> String { + self.to_string() + } + } + + #[async_trait] + pub trait ErasedAdminRepository: Send + Sync { + async fn info(&self, context: &RepositoryContext) -> RepositoryInfo; + async fn list(&self, context: &RepositoryContext) -> RepositoryList; + async fn get(&self, context: &RepositoryContext, id: &dyn Key) -> Option; + async fn create( + &mut self, + context: &RepositoryContext, + data: Value, + ) -> Option; + async fn update( + &mut self, + context: &RepositoryContext, + id: &dyn Key, + data: Value, + ) -> Option; + async fn replace( + &mut self, + context: &RepositoryContext, + id: &dyn Key, + data: Value, + ) -> Option; + async fn delete(&mut self, context: &RepositoryContext, id: &dyn Key) -> Option; } }