Archived
2
0

Detach authentication

This commit is contained in:
2025-02-12 09:04:19 +02:00
parent 30e9d55585
commit 5e6c9aec98
16 changed files with 278 additions and 66 deletions
+1 -1
View File
@@ -102,7 +102,7 @@ impl TryFrom<String> for Name {
fn try_from(value: String) -> Result<Self, Self::Error> {
#[derive(Validate)]
#[garde(transparent)]
struct Username<'a>(#[garde(ascii, length(chars, min = 2, max = 31))] &'a str);
struct Username<'a>(#[garde(alphanumeric, length(chars, min = 2, max = 31))] &'a str);
match Username(value.as_str()).validate() {
Ok(()) => (),
+2 -2
View File
@@ -1,8 +1,8 @@
pub mod authentication;
pub mod search;
pub use authentication::{
Authenticated, AuthenticationAdapter, AuthenticationContract, AuthenticationRepository,
AuthenticationService,
};
// pub
pub use search::{Search, SearchAdapter, SearchContract, SearchRepository, SearchService};
+9
View File
@@ -0,0 +1,9 @@
pub mod adapter;
pub mod contract;
pub mod repository;
pub mod service;
pub use adapter::*;
pub use contract::*;
pub use repository::*;
pub use service::*;
+43
View File
@@ -0,0 +1,43 @@
use data::search::*;
use data::{Connect, Result};
use std::marker::PhantomData;
pub struct SearchAdapter<D, C, UR>
where
C: Send,
D: Connect<Connection = C> + Sync,
UR: SearchRepository<C> + Sync,
{
driver: D,
_search_repository: PhantomData<UR>,
}
impl<D, C, UR> SearchAdapter<D, C, UR>
where
C: Send,
D: Connect<Connection = C> + Sync,
UR: SearchRepository<C> + Sync,
{
pub const fn new(driver: D) -> Self {
Self {
driver,
_search_repository: PhantomData,
}
}
}
impl<D, C, SR> super::SearchRepository for SearchAdapter<D, C, SR>
where
C: Send, //+ Sync,
D: Connect<Connection = C> + Sync,
SR: SearchRepository<C> + Sync,
{
async fn search(&self, data: Data) -> Result<Vec<Entry>> {
let c = self.driver.open_connection().await?;
let result = SR::search(&c, data).await?;
D::close_connection(c).await?;
Ok(result)
}
}
+60
View File
@@ -0,0 +1,60 @@
use data::{BoxDynError, search};
pub use data::{
Result, Validation,
search::{Mode, Order, Entry},
};
use derive_more::{Deref, Into};
use garde::Validate;
pub trait SearchContract: Send {
fn search(&self, data: Data) -> impl Future<Output = Result<Vec<Entry>>> + Send;
}
pub struct Data {
pub mode: Mode,
pub order: Order,
pub search: Search,
pub limit: u16,
pub exact: bool,
pub ascending: bool,
}
impl From<Data> for search::Data {
fn from(value: Data) -> Self {
Self {
mode: value.mode,
order: value.order,
search: value.search.into(),
limit: value.limit,
exact: value.exact,
ascending: value.ascending,
}
}
}
pub type ReturnError<T = String> = (T, BoxDynError);
#[derive(Clone, Deref, Into)]
pub struct Search(search::Search);
impl AsRef<str> for Search {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl TryFrom<String> for Search {
type Error = ReturnError;
fn try_from(value: String) -> Result<Self, Self::Error> {
#[derive(Validate)]
#[garde(transparent)]
struct Check<'a>(#[garde(ascii, length(chars, min = 1, max = 255))] &'a str);
match Check(value.as_str()).validate() {
Ok(()) => (),
Err(e) => return Err((value, e.into())),
}
Ok(Self(search::Search::new(value)?))
}
}
+6
View File
@@ -0,0 +1,6 @@
use data::Result;
use data::search::{Data, Entry};
pub trait SearchRepository {
fn search(&self, data: Data) -> impl Future<Output = Result<Vec<Entry>>> + Send;
}
+27
View File
@@ -0,0 +1,27 @@
use super::{Data, Result, SearchContract, SearchRepository};
use data::search;
pub struct SearchService<R>
where
R: SearchRepository,
{
pub(crate) repository: R,
}
impl<R> SearchService<R>
where
R: SearchRepository,
{
pub const fn new(repository: R) -> Self {
Self { repository }
}
}
impl<R> SearchContract for SearchService<R>
where
R: SearchRepository + Send + Sync,
{
async fn search(&self, data: Data) -> Result<Vec<search::Entry>> {
self.repository.search(data.into()).await
}
}