-
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
11 changed files
with
297 additions
and
11 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 |
---|---|---|
|
@@ -49,6 +49,7 @@ testv: | |
|
||
## Run the linter | ||
lint: | ||
cargo check | ||
cargo clippy | ||
|
||
## Authenticate against DigitalOcean | ||
|
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 @@ | ||
mod subscriber_email; | ||
mod subscriber_name; | ||
mod new_subscriber; | ||
|
||
pub use subscriber_email::SubscriberEmail; | ||
pub use subscriber_name::SubscriberName; | ||
pub use new_subscriber::NewSubscriber; |
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 crate::domain::subscriber_email::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,64 @@ | ||
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 email address.", s)) | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<str> for SubscriberEmail { | ||
fn as_ref(&self) -> &str { | ||
&self.0 | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use fake::faker::internet::en::SafeEmail; | ||
use fake::Fake; | ||
use rand::{rngs::StdRng, SeedableRng}; | ||
use claims::assert_err; | ||
use crate::domain::SubscriberEmail; | ||
|
||
#[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 = "name.example.com".to_string(); | ||
assert_err!(SubscriberEmail::parse(email)); | ||
} | ||
|
||
#[test] | ||
fn email_missing_subject_is_rejected() { | ||
let email = "@example.com".to_string(); | ||
assert_err!(SubscriberEmail::parse(email)); | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
struct ValidEmailFixture(pub String); | ||
|
||
impl quickcheck::Arbitrary for ValidEmailFixture { | ||
fn arbitrary(g: &mut quickcheck::Gen) -> Self { | ||
let mut rng = StdRng::seed_from_u64(u64::arbitrary(g)); | ||
let email = SafeEmail().fake_with_rng(&mut rng); | ||
Self(email) | ||
} | ||
} | ||
|
||
#[quickcheck_macros::quickcheck] | ||
fn valid_emails_are_parsed_successfully(valid_email: ValidEmailFixture) -> bool { | ||
SubscriberEmail::parse(valid_email.0).is_ok() | ||
} | ||
} |
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,70 @@ | ||
use unicode_segmentation::UnicodeSegmentation; | ||
|
||
#[derive(Debug)] | ||
pub struct SubscriberName(String); | ||
|
||
impl SubscriberName { | ||
pub fn parse(s: String) -> Result<SubscriberName, String> { | ||
let is_empty_or_whitespace = s.trim().is_empty(); | ||
let is_too_long = s.graphemes(true).count() > 256; | ||
let forbidden_characters = ['/', '(', ')', '"', '<', '>', '\\', '{', '}']; | ||
let contains_forbidden_characters = s.chars().any(|c| forbidden_characters.contains(&c)); | ||
if is_empty_or_whitespace || is_too_long || contains_forbidden_characters { | ||
Err(format!("{} is not a valid subscriber name.", s)) | ||
} | ||
else { | ||
Ok(Self(s)) | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<str> for SubscriberName { | ||
fn as_ref(&self) -> &str { | ||
&self.0 | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::domain::SubscriberName; | ||
use claims::{assert_err, assert_ok}; | ||
|
||
#[test] | ||
fn a_256_grapheme_long_name_is_valid() { | ||
let name = "a".repeat(256); | ||
assert_ok!(SubscriberName::parse(name)); | ||
} | ||
|
||
#[test] | ||
fn a_257_grapheme_long_name_is_rejected() { | ||
let name = "a".repeat(257); | ||
assert_err!(SubscriberName::parse(name)); | ||
} | ||
|
||
#[test] | ||
fn a_whitespaces_only_name_is_rejected() { | ||
let name = " ".repeat(10); | ||
assert_err!(SubscriberName::parse(name)); | ||
} | ||
|
||
#[test] | ||
fn a_name_with_forbidden_characters_is_rejected() { | ||
let name = "a/b".to_string(); | ||
assert_err!(SubscriberName::parse(name)); | ||
} | ||
|
||
#[test] | ||
fn an_empty_name_is_rejected() { | ||
let name = "".to_string(); | ||
assert_err!(SubscriberName::parse(name)); | ||
} | ||
|
||
#[test] | ||
fn names_containing_an_invalid_character_are_rejected() { | ||
for name in &['/', '(', ')', '"', '<', '>', '\\', '{', '}'] { | ||
let name = format!("a{}b", name); | ||
assert_err!(SubscriberName::parse(name)); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod configuration; | ||
pub mod domain; | ||
pub mod routes; | ||
pub mod startup; | ||
pub mod telemetry; |
Oops, something went wrong.