-
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
151 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; | ||
|
||
#[derive(Debug)] | ||
pub struct SubscriberName(String); | ||
|
||
pub struct NewSubscriber { | ||
pub email: String, | ||
pub name: SubscriberName, | ||
} | ||
|
||
impl SubscriberName { | ||
/// Returns an instance of `SubscriberName` if the input satisfies all /// our validation constraints on subscriber names. | ||
/// It panics otherwise. | ||
pub fn parse(s: String) -> Result<SubscriberName, String> { | ||
// `.trim()` returns a view over the input `s` without trailing // whitespace-like characters. | ||
// `.is_empty` checks if the view contains any character. | ||
let is_empty_or_whitespace = s.trim().is_empty(); | ||
// A grapheme is defined by the Unicode standard as a "user-perceived" | ||
// character: `å` is a single grapheme, but it is composed of two characters // (`a` and `̊`). | ||
// | ||
// `graphemes` returns an iterator over the graphemes in the input `s`. | ||
// `true` specifies that we want to use the extended grapheme definition set, // the recommended one. | ||
let is_too_long = s.graphemes(true).count() > 256; | ||
|
||
// Iterate over all characters in the input `s` to check if any of them | ||
// matches one of the characters in the forbidden array. | ||
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 claims::{assert_err, assert_ok}; | ||
#[test] | ||
fn a_256_grapheme_long_name_is_valid() { | ||
let name = "ё".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 configuration; | ||
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