Skip to content

Commit

Permalink
Separate create persona check from create persona operation
Browse files Browse the repository at this point in the history
  • Loading branch information
asonix committed Nov 4, 2018
1 parent 9b1388f commit 8efbdc3
Show file tree
Hide file tree
Showing 22 changed files with 235 additions and 457 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions aardwolf-actix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ log = "0.4"
r2d2 = "0.8"
r2d2-diesel = "1.0"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"

[dependencies.aardwolf-models]
Expand Down
24 changes: 13 additions & 11 deletions aardwolf-actix/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::marker::PhantomData;

use aardwolf_types::forms::traits::{DbAction, Validate};
use failure::Fail;
use aardwolf_types::{
error::AardwolfFail,
forms::traits::{DbAction, Validate},
};
use futures::future::Future;

use crate::{
Expand All @@ -11,20 +13,20 @@ use crate::{

pub trait Action<T, E>
where
E: Fail,
E: AardwolfFail,
{
fn action(self, state: AppConfig) -> Box<dyn Future<Item = T, Error = E> + Send>;
}

pub struct ValidateWrapper<V, T, E>(V, PhantomData<T>, PhantomData<E>)
where
V: Validate<T, E>,
E: Fail;
E: AardwolfFail;

impl<V, T, E> ValidateWrapper<V, T, E>
where
V: Validate<T, E>,
E: Fail,
E: AardwolfFail,
{
pub fn new(validate: V) -> Self {
ValidateWrapper(validate, PhantomData, PhantomData)
Expand All @@ -34,7 +36,7 @@ where
impl<V, T, E> From<V> for ValidateWrapper<V, T, E>
where
V: Validate<T, E>,
E: Fail,
E: AardwolfFail,
{
fn from(v: V) -> Self {
ValidateWrapper::new(v)
Expand All @@ -45,7 +47,7 @@ impl<V, T, E> Action<T, E> for ValidateWrapper<V, T, E>
where
V: Validate<T, E>,
T: Send + 'static,
E: Fail,
E: AardwolfFail,
{
fn action(self, _: AppConfig) -> Box<dyn Future<Item = T, Error = E> + Send> {
use futures::future::IntoFuture;
Expand All @@ -57,12 +59,12 @@ where
pub struct DbActionWrapper<D, T, E>(D, PhantomData<T>, PhantomData<E>)
where
D: DbAction<T, E>,
E: Fail;
E: AardwolfFail;

impl<D, T, E> DbActionWrapper<D, T, E>
where
D: DbAction<T, E>,
E: Fail,
E: AardwolfFail,
{
pub fn new(db_action: D) -> Self {
DbActionWrapper(db_action, PhantomData, PhantomData)
Expand All @@ -72,7 +74,7 @@ where
impl<D, T, E> From<D> for DbActionWrapper<D, T, E>
where
D: DbAction<T, E>,
E: Fail,
E: AardwolfFail,
{
fn from(d: D) -> Self {
DbActionWrapper::new(d)
Expand All @@ -83,7 +85,7 @@ impl<D, T, E> Action<T, DbActionError<E>> for DbActionWrapper<D, T, E>
where
D: DbAction<T, E> + Send + 'static,
T: Send + 'static,
E: Fail,
E: AardwolfFail,
{
fn action(
self,
Expand Down
47 changes: 12 additions & 35 deletions aardwolf-actix/src/db/perform_db_action.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use std::marker::PhantomData;

use aardwolf_types::{
error::{AardwolfError, AardwolfErrorKind},
forms::traits::DbAction,
};
use aardwolf_types::{error::AardwolfFail, forms::traits::DbAction};
use crate::actix::{Handler, MailboxError, Message};
use failure::Fail;

use crate::db::Db;

#[derive(Clone, Debug, Fail)]
#[derive(Clone, Debug, Fail, Serialize)]
pub enum DbActionError<E>
where
E: Fail,
E: AardwolfFail,
{
#[fail(display = "Error in action {}", _0)]
Action(#[cause] E),
Expand All @@ -24,11 +21,11 @@ where

impl<E> DbActionError<E>
where
E: Fail,
E: AardwolfFail,
{
pub fn map_err<F>(self) -> DbActionError<F>
where
F: Fail + From<E>,
F: AardwolfFail + From<E>,
{
match self {
DbActionError::Action(e) => DbActionError::Action(e.into()),
Expand All @@ -40,38 +37,18 @@ where

impl<E> From<MailboxError> for DbActionError<E>
where
E: Fail,
E: AardwolfFail,
{
fn from(_: MailboxError) -> Self {
DbActionError::Mailbox
}
}

impl<E> AardwolfError for DbActionError<E>
where
E: AardwolfError,
{
fn name(&self) -> &str {
"Database Action Error"
}

fn kind(&self) -> AardwolfErrorKind {
match *self {
DbActionError::Connection | DbActionError::Mailbox => {
AardwolfErrorKind::InternalServerError
}
DbActionError::Action(ref e) => e.kind(),
}
}

fn description(&self) -> String {
format!("{}", self)
}
}
impl<E> AardwolfFail for DbActionError<E> where E: AardwolfFail {}

impl<E> From<r2d2::Error> for DbActionError<E>
where
E: Fail,
E: AardwolfFail,
{
fn from(_: r2d2::Error) -> Self {
DbActionError::Connection
Expand All @@ -81,7 +58,7 @@ where
pub struct PerformDbAction<D, T, E>
where
D: DbAction<T, E>,
E: Fail,
E: AardwolfFail,
{
db_action: D,
item: PhantomData<T>,
Expand All @@ -91,7 +68,7 @@ where
impl<D, T, E> PerformDbAction<D, T, E>
where
D: DbAction<T, E>,
E: Fail,
E: AardwolfFail,
{
pub fn new(db_action: D) -> Self {
PerformDbAction {
Expand All @@ -105,7 +82,7 @@ where
impl<D, T, E> Message for PerformDbAction<D, T, E>
where
D: DbAction<T, E>,
E: Fail,
E: AardwolfFail,
T: 'static,
{
type Result = Result<T, DbActionError<E>>;
Expand All @@ -114,7 +91,7 @@ where
impl<D, T, E> Handler<PerformDbAction<D, T, E>> for Db
where
D: DbAction<T, E>,
E: Fail,
E: AardwolfFail,
T: 'static,
{
type Result = <PerformDbAction<D, T, E> as Message>::Result;
Expand Down
Loading

0 comments on commit 8efbdc3

Please sign in to comment.