-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
234 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pub mod new_subscriber; | ||
pub mod subscriber_email; | ||
pub mod subscriber_name; | ||
|
||
pub use new_subscriber::NewSubscriber; | ||
pub use subscriber_email::SubscriberEmail; | ||
pub use subscriber_name::SubscriberName; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use crate::domain::subscriber_name::SubscriberName; | ||
|
||
use super::SubscriberEmail; | ||
|
||
pub struct NewSubscriber { | ||
pub email: SubscriberEmail, | ||
pub name: SubscriberName, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use validator::validate_email; | ||
|
||
#[derive(Debug)] | ||
pub struct SubscriberEmail(String); | ||
|
||
impl SubscriberEmail { | ||
pub fn parse(s: String) -> Result<SubscriberEmail, String> { | ||
if validate_email(&s) { | ||
Ok(Self(s)) | ||
} else { | ||
Err(format!("{} is not a valid subscriber email", s)) | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<str> for SubscriberEmail { | ||
fn as_ref(&self) -> &str { | ||
&self.0 | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::SubscriberEmail; | ||
use claim::assert_err; | ||
use fake::{faker::internet::en::SafeEmail, Fake}; | ||
|
||
#[test] | ||
fn empty_string_is_rejected() { | ||
let email = "".to_string(); | ||
assert_err!(SubscriberEmail::parse(email)); | ||
} | ||
|
||
#[test] | ||
fn email_missing_at_symbol_is_rejected() { | ||
let email = "ursuladomain.com".to_string(); | ||
assert_err!(SubscriberEmail::parse(email)); | ||
} | ||
|
||
#[test] | ||
fn email_missing_subject_is_rejected() { | ||
let email = "@domain.com".to_string(); | ||
assert_err!(SubscriberEmail::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 { | ||
SubscriberEmail::parse(valid_email.0).is_ok() | ||
} | ||
} |
Oops, something went wrong.