Authentication service & Send bounds for async
This commit is contained in:
@ -5,14 +5,12 @@ use derive_more::{Deref, DerefMut};
|
|||||||
#[derive(Deref, DerefMut)]
|
#[derive(Deref, DerefMut)]
|
||||||
pub struct Authenticated(User);
|
pub struct Authenticated(User);
|
||||||
|
|
||||||
#[allow(async_fn_in_trait)]
|
|
||||||
pub trait AuthenticationRepository {
|
pub trait AuthenticationRepository {
|
||||||
async fn get_user(&self, get: Get) -> Result<Option<User>>;
|
fn get_user(&self, get: Get) -> impl Future<Output = Result<Option<User>>> + Send;
|
||||||
async fn create_user(&self, new: New) -> Result<User>;
|
fn create_user(&self, new: New) -> impl Future<Output = Result<User>> + Send;
|
||||||
async fn start_session(&self, user: User) -> Result<Authenticated>;
|
fn start_session(&self, user: User) -> impl Future<Output = Result<Authenticated>> + Send;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub enum Get {
|
pub enum Get {
|
||||||
Name(Name),
|
Name(Name),
|
||||||
Email(Email),
|
Email(Email),
|
||||||
@ -34,8 +32,9 @@ use std::marker::PhantomData;
|
|||||||
|
|
||||||
pub struct AuthenticationAdapter<D, C, UR>
|
pub struct AuthenticationAdapter<D, C, UR>
|
||||||
where
|
where
|
||||||
D: Connect<Connection = C>,
|
C: Send,
|
||||||
UR: UserRepository<C>,
|
D: Connect<Connection = C> + Sync,
|
||||||
|
UR: UserRepository<C> + Sync,
|
||||||
{
|
{
|
||||||
driver: D,
|
driver: D,
|
||||||
_user_repository: PhantomData<UR>,
|
_user_repository: PhantomData<UR>,
|
||||||
@ -43,8 +42,9 @@ where
|
|||||||
|
|
||||||
impl<D, C, UR> AuthenticationAdapter<D, C, UR>
|
impl<D, C, UR> AuthenticationAdapter<D, C, UR>
|
||||||
where
|
where
|
||||||
D: Connect<Connection = C>,
|
C: Send,
|
||||||
UR: UserRepository<C>,
|
D: Connect<Connection = C> + Sync,
|
||||||
|
UR: UserRepository<C> + Sync,
|
||||||
{
|
{
|
||||||
pub const fn new(driver: D) -> Self {
|
pub const fn new(driver: D) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -56,8 +56,9 @@ where
|
|||||||
|
|
||||||
impl<D, C, UR> AuthenticationRepository for AuthenticationAdapter<D, C, UR>
|
impl<D, C, UR> AuthenticationRepository for AuthenticationAdapter<D, C, UR>
|
||||||
where
|
where
|
||||||
D: Connect<Connection = C>,
|
C: Send,
|
||||||
UR: UserRepository<C>,
|
D: Connect<Connection = C> + Sync,
|
||||||
|
UR: UserRepository<C> + Sync,
|
||||||
{
|
{
|
||||||
async fn get_user(&self, get: Get) -> Result<Option<User>> {
|
async fn get_user(&self, get: Get) -> Result<Option<User>> {
|
||||||
let c = self.driver.open_connection().await?;
|
let c = self.driver.open_connection().await?;
|
||||||
@ -83,4 +84,3 @@ where
|
|||||||
Ok(Authenticated(user))
|
Ok(Authenticated(user))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,9 +4,15 @@ use sqlx::{Executor, MySql};
|
|||||||
|
|
||||||
pub struct BaseAdapter;
|
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
|
impl<E> crate::port::CRUD<E> for BaseAdapter
|
||||||
where
|
where
|
||||||
|
E: Send,
|
||||||
for<'a> &'a E: Executor<'a, Database = MySql>,
|
for<'a> &'a E: Executor<'a, Database = MySql>,
|
||||||
{
|
{
|
||||||
type New = New;
|
type New = New;
|
||||||
|
@ -4,9 +4,15 @@ use sqlx::{Executor, MySql};
|
|||||||
|
|
||||||
pub struct PackageAdapter;
|
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
|
impl<E> crate::port::CRUD<E> for PackageAdapter
|
||||||
where
|
where
|
||||||
|
E: Send,
|
||||||
for<'a> &'a E: Executor<'a, Database = MySql>,
|
for<'a> &'a E: Executor<'a, Database = MySql>,
|
||||||
{
|
{
|
||||||
type New = New;
|
type New = New;
|
||||||
|
@ -4,9 +4,15 @@ use sqlx::{Executor, MySql};
|
|||||||
|
|
||||||
pub struct UserAdapter;
|
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
|
impl<E> crate::port::CRUD<E> for UserAdapter
|
||||||
where
|
where
|
||||||
|
E: Send,
|
||||||
for<'a> &'a E: Executor<'a, Database = MySql>,
|
for<'a> &'a E: Executor<'a, Database = MySql>,
|
||||||
{
|
{
|
||||||
type New = New;
|
type New = New;
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
|
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
||||||
#[allow(async_fn_in_trait)]
|
|
||||||
pub trait Atomic {
|
pub trait Atomic {
|
||||||
type Transaction<'a>;
|
type Transaction<'a>;
|
||||||
|
|
||||||
async fn start_transaction(&mut self) -> Result<Self::Transaction<'_>>;
|
fn start_transaction(&mut self) -> impl Future<Output = Result<Self::Transaction<'_>>> + Send;
|
||||||
async fn abort_transaction(transaction: Self::Transaction<'_>) -> Result;
|
fn abort_transaction(transaction: Self::Transaction<'_>) -> impl Future<Output = Result> + Send;
|
||||||
async fn commit_transaction(transaction: Self::Transaction<'_>) -> Result;
|
fn commit_transaction(transaction: Self::Transaction<'_>) -> impl Future<Output = Result> + Send;
|
||||||
}
|
}
|
||||||
|
|
||||||
use sqlx::Connection;
|
use sqlx::Connection;
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
|
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
||||||
#[allow(async_fn_in_trait)]
|
|
||||||
pub trait Connect {
|
pub trait Connect {
|
||||||
type Connection;
|
type Connection;
|
||||||
|
|
||||||
async fn open_connection(&self) -> Result<Self::Connection>;
|
fn open_connection(&self) -> impl Future<Output = Result<Self::Connection>> + Send;
|
||||||
async fn close_connection(connection: Self::Connection) -> Result;
|
fn close_connection(connection: Self::Connection) -> impl Future<Output = Result> + Send;
|
||||||
}
|
}
|
||||||
|
|
||||||
use sqlx::Connection;
|
use sqlx::Connection;
|
||||||
|
@ -2,17 +2,3 @@ pub mod adapter;
|
|||||||
pub mod atomic;
|
pub mod atomic;
|
||||||
pub mod connect;
|
pub mod connect;
|
||||||
pub mod port;
|
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 {}
|
|
||||||
|
|
||||||
|
@ -1,20 +1,25 @@
|
|||||||
pub type Result<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
|
pub type Result<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
|
||||||
|
|
||||||
#[allow(async_fn_in_trait)]
|
|
||||||
pub trait CRUD<C> {
|
pub trait CRUD<C> {
|
||||||
type New;
|
type New;
|
||||||
type Unique;
|
type Unique;
|
||||||
type Update;
|
type Update;
|
||||||
type Existing;
|
type Existing;
|
||||||
|
|
||||||
async fn create(connection: &mut C, data: Self::New) -> Result<Self::Existing>;
|
fn create(
|
||||||
async fn read(connection: &C, data: Self::Unique) -> Result<Option<Self::Existing>>;
|
connection: &mut C,
|
||||||
async fn update(
|
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,
|
connection: &mut C,
|
||||||
existing: &mut Self::Existing,
|
existing: &mut Self::Existing,
|
||||||
data: Self::Update,
|
data: Self::Update,
|
||||||
) -> Result;
|
) -> impl Future<Output = Result> + Send;
|
||||||
async fn delete(connection: &mut C, data: Self::Unique) -> Result;
|
fn delete(connection: &mut C, data: Self::Unique) -> impl Future<Output = Result> + Send;
|
||||||
}
|
}
|
||||||
|
|
||||||
trait CharLength {
|
trait CharLength {
|
||||||
|
@ -4,7 +4,6 @@ pub use super::{CRUD, Result};
|
|||||||
pub use chrono::{DateTime, Utc};
|
pub use chrono::{DateTime, Utc};
|
||||||
use derive_more::{Deref, Into};
|
use derive_more::{Deref, Into};
|
||||||
|
|
||||||
#[allow(async_fn_in_trait)]
|
|
||||||
pub trait BaseRepository<C>:
|
pub trait BaseRepository<C>:
|
||||||
CRUD<C, New = New, Unique = u64, Update = Field, Existing = Base>
|
CRUD<C, New = New, Unique = u64, Update = Field, Existing = Base>
|
||||||
{
|
{
|
||||||
|
@ -4,7 +4,6 @@ pub use super::{CRUD, Result, base::Base};
|
|||||||
pub use chrono::{DateTime, Utc};
|
pub use chrono::{DateTime, Utc};
|
||||||
use derive_more::{Deref, Into};
|
use derive_more::{Deref, Into};
|
||||||
|
|
||||||
#[allow(async_fn_in_trait)]
|
|
||||||
pub trait PackageRepository<C>:
|
pub trait PackageRepository<C>:
|
||||||
CRUD<C, New = New, Update = Field, Unique = Unique, Existing = Package>
|
CRUD<C, New = New, Update = Field, Unique = Unique, Existing = Package>
|
||||||
{
|
{
|
||||||
|
@ -4,7 +4,6 @@ pub use super::{CRUD, Result};
|
|||||||
pub use chrono::{DateTime, Utc};
|
pub use chrono::{DateTime, Utc};
|
||||||
use derive_more::{Deref, Into};
|
use derive_more::{Deref, Into};
|
||||||
|
|
||||||
#[allow(async_fn_in_trait)]
|
|
||||||
pub trait UserRepository<C>:
|
pub trait UserRepository<C>:
|
||||||
CRUD<C, New = New, Update = Field, Unique = Unique, Existing = User>
|
CRUD<C, New = New, Update = Field, Unique = Unique, Existing = User>
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user