From 8dde022851152f44891e49e1054e96a214f49497 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Tue, 10 May 2022 23:49:01 +0530 Subject: [PATCH] feat: define interface for username registration" --- db/db-core/src/errors.rs | 9 +++++++++ db/db-core/src/lib.rs | 16 ++++++++++++++++ src/errors.rs | 7 ++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/db/db-core/src/errors.rs b/db/db-core/src/errors.rs index c3f18e23..3ecb09c3 100644 --- a/db/db-core/src/errors.rs +++ b/db/db-core/src/errors.rs @@ -26,6 +26,15 @@ pub enum DBError { /// errors that are specific to a database implementation #[error("{0}")] DBError(#[source] BoxDynError), + /// Username is taken + #[error("Username is taken")] + UsernameTaken, + /// Email is taken + #[error("Email is taken")] + EmailTaken, + /// Secret is taken + #[error("Secret is taken")] + SecretTaken, } /// Convenience type alias for grouping driver-specific errors diff --git a/db/db-core/src/lib.rs b/db/db-core/src/lib.rs index 1287379c..75fde83c 100644 --- a/db/db-core/src/lib.rs +++ b/db/db-core/src/lib.rs @@ -58,12 +58,28 @@ pub mod dev { pub use async_trait::async_trait; } +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +/// Data required to register a new user +pub struct Register<'a> { + /// username of new user + pub username: &'a str, + /// secret of new user + pub secret: &'a str, + /// hashed password of new use + pub hash: &'a str, + /// Optionally, email of new use + pub email: Option<&'a str>, +} + #[async_trait] /// mCaptcha's database requirements. To implement support for $Database, kindly implement this /// trait. pub trait MCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase { /// ping DB async fn ping(&self) -> bool; + + /// register a new user + async fn register(&self, p: &Register) -> DBResult<()>; } /// Trait to clone MCDatabase diff --git a/src/errors.rs b/src/errors.rs index 7283d971..c5ca2c28 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -197,7 +197,12 @@ impl From for ServiceError { impl From for ServiceError { #[cfg(not(tarpaulin_include))] fn from(e: DBError) -> ServiceError { - ServiceError::DBError(DBErrorWrapper(e)) + match e{ + DBError::UsernameTaken => ServiceError::UsernameTaken, + DBError::SecretTaken => ServiceError::InternalServerError, + DBError::EmailTaken => ServiceError::EmailTaken, + _ => ServiceError::DBError(DBErrorWrapper(e)) + } } }