2023-05-17 09:49:39 +08:00
|
|
|
use flowy_error::ErrorCode;
|
2021-06-29 23:21:25 +08:00
|
|
|
use validator::validate_email;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2021-07-05 16:54:41 +08:00
|
|
|
pub struct UserEmail(pub String);
|
2021-06-29 23:21:25 +08:00
|
|
|
|
|
|
|
impl UserEmail {
|
2023-05-17 09:49:39 +08:00
|
|
|
pub fn parse(s: String) -> Result<UserEmail, ErrorCode> {
|
2023-02-13 09:29:49 +08:00
|
|
|
if s.trim().is_empty() {
|
2023-05-17 09:49:39 +08:00
|
|
|
return Err(ErrorCode::EmailIsEmpty);
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2021-07-06 14:14:47 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
if validate_email(&s) {
|
|
|
|
Ok(Self(s))
|
|
|
|
} else {
|
2023-05-17 09:49:39 +08:00
|
|
|
Err(ErrorCode::EmailFormatInvalid)
|
2021-06-29 23:21:25 +08:00
|
|
|
}
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2021-06-29 23:21:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<str> for UserEmail {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
&self.0
|
|
|
|
}
|
2021-06-29 23:21:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-02-13 09:29:49 +08:00
|
|
|
use fake::{faker::internet::en::SafeEmail, Fake};
|
|
|
|
use rand::prelude::StdRng;
|
|
|
|
use rand_core::SeedableRng;
|
2021-06-29 23:21:25 +08:00
|
|
|
|
2023-05-17 09:49:39 +08:00
|
|
|
use super::*;
|
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[test]
|
|
|
|
fn empty_string_is_rejected() {
|
|
|
|
let email = "".to_string();
|
2023-03-03 20:38:31 +08:00
|
|
|
assert!(UserEmail::parse(email).is_err());
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2021-06-29 23:21:25 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[test]
|
|
|
|
fn email_missing_at_symbol_is_rejected() {
|
|
|
|
let email = "helloworld.com".to_string();
|
2023-03-03 20:38:31 +08:00
|
|
|
assert!(UserEmail::parse(email).is_err());
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2021-06-29 23:21:25 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[test]
|
|
|
|
fn email_missing_subject_is_rejected() {
|
|
|
|
let email = "@domain.com".to_string();
|
2023-03-03 20:38:31 +08:00
|
|
|
assert!(UserEmail::parse(email).is_err());
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2021-06-29 23:21:25 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct ValidEmailFixture(pub String);
|
2021-06-29 23:21:25 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
impl quickcheck::Arbitrary for ValidEmailFixture {
|
|
|
|
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
|
|
|
let mut rand_slice: [u8; 32] = [0; 32];
|
|
|
|
#[allow(clippy::needless_range_loop)]
|
|
|
|
for i in 0..32 {
|
|
|
|
rand_slice[i] = u8::arbitrary(g);
|
|
|
|
}
|
|
|
|
let mut seed = StdRng::from_seed(rand_slice);
|
|
|
|
let email = SafeEmail().fake_with_rng(&mut seed);
|
|
|
|
Self(email)
|
2021-06-29 23:21:25 +08:00
|
|
|
}
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2021-06-29 23:21:25 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[quickcheck_macros::quickcheck]
|
|
|
|
fn valid_emails_are_parsed_successfully(valid_email: ValidEmailFixture) -> bool {
|
|
|
|
UserEmail::parse(valid_email.0).is_ok()
|
|
|
|
}
|
2021-06-29 23:21:25 +08:00
|
|
|
}
|