mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2025-08-27 10:16:56 +00:00
60 lines
1.4 KiB
Rust
60 lines
1.4 KiB
Rust
![]() |
use validator::validate_email;
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
pub struct UserEmail(String);
|
||
|
|
||
|
impl UserEmail {
|
||
|
pub fn parse(s: String) -> Result<UserEmail, String> {
|
||
|
if validate_email(&s) {
|
||
|
Ok(Self(s))
|
||
|
} else {
|
||
|
Err(format!("{} is not a valid subscriber email.", s))
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl AsRef<str> for UserEmail {
|
||
|
fn as_ref(&self) -> &str { &self.0 }
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
use claim::assert_err;
|
||
|
use fake::{faker::internet::en::SafeEmail, Fake};
|
||
|
use quickcheck::Gen;
|
||
|
|
||
|
#[test]
|
||
|
fn empty_string_is_rejected() {
|
||
|
let email = "".to_string();
|
||
|
assert_err!(UserEmail::parse(email));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn email_missing_at_symbol_is_rejected() {
|
||
|
let email = "helloworld.com".to_string();
|
||
|
assert_err!(UserEmail::parse(email));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn email_missing_subject_is_rejected() {
|
||
|
let email = "@domain.com".to_string();
|
||
|
assert_err!(UserEmail::parse(email));
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Clone)]
|
||
|
struct ValidEmailFixture(pub String);
|
||
|
|
||
|
impl quickcheck::Arbitrary for ValidEmailFixture {
|
||
|
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
|
||
|
let email = SafeEmail().fake_with_rng(g);
|
||
|
Self(email)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[quickcheck_macros::quickcheck]
|
||
|
fn valid_emails_are_parsed_successfully(valid_email: ValidEmailFixture) -> bool {
|
||
|
UserEmail::parse(valid_email.0).is_ok()
|
||
|
}
|
||
|
}
|