1
0

Authentication service & Send bounds for async

This commit is contained in:
2025-02-01 11:51:58 +02:00
parent 517a81aa50
commit da50312a40
11 changed files with 49 additions and 45 deletions

View File

@ -5,14 +5,12 @@ use derive_more::{Deref, DerefMut};
#[derive(Deref, DerefMut)]
pub struct Authenticated(User);
#[allow(async_fn_in_trait)]
pub trait AuthenticationRepository {
async fn get_user(&self, get: Get) -> Result<Option<User>>;
async fn create_user(&self, new: New) -> Result<User>;
async fn start_session(&self, user: User) -> Result<Authenticated>;
fn get_user(&self, get: Get) -> impl Future<Output = Result<Option<User>>> + Send;
fn create_user(&self, new: New) -> impl Future<Output = Result<User>> + Send;
fn start_session(&self, user: User) -> impl Future<Output = Result<Authenticated>> + Send;
}
pub enum Get {
Name(Name),
Email(Email),
@ -34,8 +32,9 @@ use std::marker::PhantomData;
pub struct AuthenticationAdapter<D, C, UR>
where
D: Connect<Connection = C>,
UR: UserRepository<C>,
C: Send,
D: Connect<Connection = C> + Sync,
UR: UserRepository<C> + Sync,
{
driver: D,
_user_repository: PhantomData<UR>,
@ -43,8 +42,9 @@ where
impl<D, C, UR> AuthenticationAdapter<D, C, UR>
where
D: Connect<Connection = C>,
UR: UserRepository<C>,
C: Send,
D: Connect<Connection = C> + Sync,
UR: UserRepository<C> + Sync,
{
pub const fn new(driver: D) -> Self {
Self {
@ -56,8 +56,9 @@ where
impl<D, C, UR> AuthenticationRepository for AuthenticationAdapter<D, C, UR>
where
D: Connect<Connection = C>,
UR: UserRepository<C>,
C: Send,
D: Connect<Connection = C> + Sync,
UR: UserRepository<C> + Sync,
{
async fn get_user(&self, get: Get) -> Result<Option<User>> {
let c = self.driver.open_connection().await?;
@ -83,4 +84,3 @@ where
Ok(Authenticated(user))
}
}

View File

@ -4,9 +4,15 @@ use sqlx::{Executor, MySql};
pub struct BaseAdapter;
impl<E> BaseRepository<E> for BaseAdapter where for<'a> &'a E: Executor<'a, Database = MySql> {}
impl<E> BaseRepository<E> for BaseAdapter
where
E: Send,
for<'a> &'a E: Executor<'a, Database = MySql>,
{
}
impl<E> crate::port::CRUD<E> for BaseAdapter
where
E: Send,
for<'a> &'a E: Executor<'a, Database = MySql>,
{
type New = New;

View File

@ -4,9 +4,15 @@ use sqlx::{Executor, MySql};
pub struct PackageAdapter;
impl<E> PackageRepository<E> for PackageAdapter where for<'a> &'a E: Executor<'a, Database = MySql> {}
impl<E> PackageRepository<E> for PackageAdapter
where
E: Send,
for<'a> &'a E: Executor<'a, Database = MySql>,
{
}
impl<E> crate::port::CRUD<E> for PackageAdapter
where
E: Send,
for<'a> &'a E: Executor<'a, Database = MySql>,
{
type New = New;

View File

@ -4,9 +4,15 @@ use sqlx::{Executor, MySql};
pub struct UserAdapter;
impl<E> UserRepository<E> for UserAdapter where for<'a> &'a E: Executor<'a, Database = MySql> {}
impl<E> UserRepository<E> for UserAdapter
where
E: Send,
for<'a> &'a E: Executor<'a, Database = MySql>,
{
}
impl<E> crate::port::CRUD<E> for UserAdapter
where
E: Send,
for<'a> &'a E: Executor<'a, Database = MySql>,
{
type New = New;

View File

@ -1,12 +1,11 @@
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
#[allow(async_fn_in_trait)]
pub trait Atomic {
type Transaction<'a>;
async fn start_transaction(&mut self) -> Result<Self::Transaction<'_>>;
async fn abort_transaction(transaction: Self::Transaction<'_>) -> Result;
async fn commit_transaction(transaction: Self::Transaction<'_>) -> Result;
fn start_transaction(&mut self) -> impl Future<Output = Result<Self::Transaction<'_>>> + Send;
fn abort_transaction(transaction: Self::Transaction<'_>) -> impl Future<Output = Result> + Send;
fn commit_transaction(transaction: Self::Transaction<'_>) -> impl Future<Output = Result> + Send;
}
use sqlx::Connection;

View File

@ -1,11 +1,10 @@
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
#[allow(async_fn_in_trait)]
pub trait Connect {
type Connection;
async fn open_connection(&self) -> Result<Self::Connection>;
async fn close_connection(connection: Self::Connection) -> Result;
fn open_connection(&self) -> impl Future<Output = Result<Self::Connection>> + Send;
fn close_connection(connection: Self::Connection) -> impl Future<Output = Result> + Send;
}
use sqlx::Connection;

View File

@ -2,17 +2,3 @@ pub mod adapter;
pub mod atomic;
pub mod connect;
pub mod port;
// use garde::{Report, Unvalidated, Valid, Validate};
// pub trait IntoValid: Validate {
// fn into_valid(self) -> Result<Valid<Self>, Report>
// where
// Self: Sized,
// <Self as Validate>::Context: Default,
// {
// Unvalidated::new(self).validate()
// }
// }
// impl<T: garde::Validate> IntoValid for T {}

View File

@ -1,20 +1,25 @@
pub type Result<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
#[allow(async_fn_in_trait)]
pub trait CRUD<C> {
type New;
type Unique;
type Update;
type Existing;
async fn create(connection: &mut C, data: Self::New) -> Result<Self::Existing>;
async fn read(connection: &C, data: Self::Unique) -> Result<Option<Self::Existing>>;
async fn update(
fn create(
connection: &mut C,
data: Self::New,
) -> impl Future<Output = Result<Self::Existing>> + Send;
fn read(
connection: &C,
data: Self::Unique,
) -> impl Future<Output = Result<Option<Self::Existing>>> + Send;
fn update(
connection: &mut C,
existing: &mut Self::Existing,
data: Self::Update,
) -> Result;
async fn delete(connection: &mut C, data: Self::Unique) -> Result;
) -> impl Future<Output = Result> + Send;
fn delete(connection: &mut C, data: Self::Unique) -> impl Future<Output = Result> + Send;
}
trait CharLength {

View File

@ -4,7 +4,6 @@ pub use super::{CRUD, Result};
pub use chrono::{DateTime, Utc};
use derive_more::{Deref, Into};
#[allow(async_fn_in_trait)]
pub trait BaseRepository<C>:
CRUD<C, New = New, Unique = u64, Update = Field, Existing = Base>
{

View File

@ -4,7 +4,6 @@ pub use super::{CRUD, Result, base::Base};
pub use chrono::{DateTime, Utc};
use derive_more::{Deref, Into};
#[allow(async_fn_in_trait)]
pub trait PackageRepository<C>:
CRUD<C, New = New, Update = Field, Unique = Unique, Existing = Package>
{

View File

@ -4,7 +4,6 @@ pub use super::{CRUD, Result};
pub use chrono::{DateTime, Utc};
use derive_more::{Deref, Into};
#[allow(async_fn_in_trait)]
pub trait UserRepository<C>:
CRUD<C, New = New, Update = Field, Unique = Unique, Existing = User>
{