cyrtophora

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 1bcc17bd5f823044936021baf3dace23bd4e0239
parent 700e7e04002afb058f5bd51c4c569ce97f434a10
Author: Jackson G. Kaindume <seestem@merely.tech>
Date:   Sun, 21 Aug 2022 14:32:37 +0200

fix make email field optional

Diffstat:
Msrc/account.rs | 26++++++++++++++++++--------
1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/account.rs b/src/account.rs @@ -4,33 +4,43 @@ use crate::validate::{Validate, ValidationError}; pub struct Account { /// The username of the user, also used as an unique identifier pub username: String, - /// The email address the user - pub email: String, /// The password of the user pub password: String, + /// The email address the user + pub email: Option<String>, } impl Account { - pub fn new(username: &str, email: &str, password: &str) -> Result<Self, ValidationError> { + /// Register new user + pub fn register( + username: &str, + password: &str, + email: Option<&str>, + ) -> Result<Self, ValidationError> { + let mut validated_email: Option<String> = None; + if !Validate::username(username) { return Err(ValidationError::Username); } - if !Validate::email(email) { + if !Validate::password(password) { return Err(ValidationError::Email); } - // TODO: email verification code + if let Some(email) = email { + if !Validate::email(email) { + return Err(ValidationError::Email); + } + validated_email = Some(email.to_string()); - if !Validate::password(password) { - return Err(ValidationError::Email); + // TODO: email verification code } // TODO: database registration code Ok(Account { username: username.to_string(), - email: email.to_string(), + email: validated_email, password: password.to_string(), }) }