-
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
6 changed files
with
160 additions
and
10 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,79 @@ | ||
use unicode_segmentation::UnicodeSegmentation; | ||
|
||
pub struct NewSubscriber { | ||
pub email: String, | ||
pub name: SubscriberName, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct SubscriberName(String); | ||
|
||
impl SubscriberName { | ||
/// Returns an instance of `SubscriberName` if the input string | ||
/// contains valid characters only, and `None` otherwise. | ||
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(|g| forbidden_characters.contains(&g)); | ||
|
||
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 claim::{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_name_longer_than_256_graphemes_is_rejected() { | ||
let name = "a".repeat(257); | ||
assert_err!(SubscriberName::parse(name)); | ||
} | ||
|
||
#[test] | ||
fn whitespace_only_names_are_rejected() { | ||
let name = " ".to_string(); | ||
assert_err!(SubscriberName::parse(name)); | ||
} | ||
|
||
#[test] | ||
fn empty_string_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 = name.to_string(); | ||
assert_err!(SubscriberName::parse(name)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn a_valid_name_is_parsed_successfully() { | ||
let name = "Ursula Le Guin".to_string(); | ||
assert_ok!(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 configurations; | ||
pub mod domain; | ||
pub mod routes; | ||
pub mod startup; | ||
pub mod telemetry; |
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