Skip to content

Commit

Permalink
feat(domains): add generic service impls
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbchron committed Dec 8, 2024
1 parent e5fbf09 commit 46cd637
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crates/auth-domain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! authentication, and authorization logic.
use core::fmt;
use std::sync::Arc;

use axum_login::{AuthUser, AuthnBackend};
use hex::{health, Hexagonal};
Expand All @@ -11,6 +12,9 @@ use models::{
};
use repos::{FetchModelByIndexError, FetchModelError, ModelRepository};

/// A dynamic [`AuthDomainService`] trait object.
pub type DynAuthDomainService = Arc<Box<dyn AuthDomainService>>;

/// An error that occurs during user creation.
#[derive(Debug, thiserror::Error, miette::Diagnostic)]
pub enum CreateUserError {
Expand Down Expand Up @@ -63,6 +67,42 @@ pub trait AuthDomainService: Hexagonal {
) -> Result<Option<User>, AuthenticationError>;
}

// smart pointer impl
#[async_trait::async_trait]
impl<T, I> AuthDomainService for T
where
T: std::ops::Deref<Target = I> + Hexagonal + Sized,
I: AuthDomainService + ?Sized,
{
async fn fetch_user_by_id(
&self,
id: models::UserRecordId,
) -> Result<Option<User>, FetchModelError> {
I::fetch_user_by_id(self, id).await
}

async fn fetch_user_by_email(
&self,
email: models::EmailAddress,
) -> Result<Option<User>, FetchModelByIndexError> {
I::fetch_user_by_email(self, email).await
}

async fn user_signup(
&self,
req: UserCreateRequest,
) -> Result<User, CreateUserError> {
I::user_signup(self, req).await
}

async fn user_authenticate(
&self,
creds: UserAuthCredentials,
) -> Result<Option<User>, AuthenticationError> {
I::user_authenticate(self, creds).await
}
}

/// The canonical implementation of the [`AuthDomainService`].
pub struct AuthDomainServiceCanonical<
UR: ModelRepository<Model = User, ModelCreateRequest = UserCreateRequest>,
Expand Down
18 changes: 18 additions & 0 deletions crates/prime-domain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,21 @@ pub trait PrimeDomainService: Hexagonal {
/// Produce a list of all [`Photo`]s.
async fn enumerate_photos(&self) -> Result<Vec<Photo>>;
}

#[async_trait::async_trait]
impl<T, I> PrimeDomainService for T
where
T: std::ops::Deref<Target = I> + Hexagonal + Sized,
I: PrimeDomainService + ?Sized,
{
async fn fetch_photo_by_id(
&self,
id: PhotoRecordId,
) -> Result<Option<Photo>, FetchModelError> {
self.deref().fetch_photo_by_id(id).await
}

async fn enumerate_photos(&self) -> Result<Vec<Photo>> {
self.deref().enumerate_photos().await
}
}

0 comments on commit 46cd637

Please sign in to comment.