chore: identity: use common error type for cmds

This commit is contained in:
Aravinth Manivannan 2024-05-17 23:58:05 +05:30
parent 4b5f8733ce
commit 09d3e5331c
Signed by: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 18 additions and 15 deletions

View file

@ -6,17 +6,26 @@ use argon2_creds::CredsError;
use derive_more::Display;
use serde::{Deserialize, Serialize};
pub type RegisterUserCommandResult<V> = Result<V, RegisterUserCommandError>;
pub type IdentityResult<V> = Result<V, IdentityError>;
#[derive(Debug, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum RegisterUserCommandError {
pub enum IdentityError {
UsernameExists,
EmailExists,
}
pub type IdentityCommandResult<V> = Result<V, IdentityCommandError>;
#[derive(Debug, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum IdentityCommandError {
BadUsername(String),
BadEmail,
BadPassowrd(String),
PasswordsDontMatch,
WrongPassword,
}
impl From<CredsError> for RegisterUserCommandError {
impl From<CredsError> for IdentityCommandError {
fn from(v: CredsError) -> Self {
match v {
CredsError::ProfainityError => Self::BadUsername(v.to_string()),

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use super::error::*;
use super::*;
use derive_getters::Getters;
use serde::{Deserialize, Serialize};
@ -20,12 +20,12 @@ impl RegisterUserCommand {
password: String,
confirm_password: String,
config: &argon2_creds::Config,
) -> RegisterUserCommandResult<Self> {
) -> IdentityCommandResult<Self> {
let username = config.username(&username)?;
config.email(&email)?;
if password != confirm_password {
return Err(RegisterUserCommandError::PasswordsDontMatch);
return Err(IdentityCommandError::PasswordsDontMatch);
}
let hashed_password: String = config.password(&password)?;
@ -62,7 +62,7 @@ mod tests {
&config,
)
.err(),
Some(RegisterUserCommandError::BadEmail)
Some(IdentityCommandError::BadEmail)
);
assert!(matches!(
@ -74,7 +74,7 @@ mod tests {
&config,
)
.err(),
Some(RegisterUserCommandError::BadUsername(_))
Some(IdentityCommandError::BadUsername(_))
));
assert_eq!(
@ -86,7 +86,7 @@ mod tests {
&config,
)
.err(),
Some(RegisterUserCommandError::PasswordsDontMatch)
Some(IdentityCommandError::PasswordsDontMatch)
);
}
}

View file

@ -3,7 +3,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pub mod command;
pub mod error;
pub mod events;
pub mod service;

View file

@ -2,9 +2,6 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// - Hash password
// - Add user record
// - Send verification email
// - TODO: If UID == 0; set admin
use derive_builder::Builder;
@ -70,8 +67,6 @@ impl RegisterUserUseCase for RegisterUserService {
.await
.unwrap();
// TODO: send mail verification link
Ok(events::UserRegisteredEventBuilder::default()
.username(cmd.username().into())
.email(cmd.email().into())