mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-12-18 05:56:35 +00:00
Use Iterator::any to find forbidden characters (#30)
* Use Iterator::any to find forbidden characters Iterator any stops if any are found, easier to understand than counting. * Use Self rather than SubscriberName
This commit is contained in:
parent
9fa3183fd7
commit
6af7e78ef5
1 changed files with 2 additions and 6 deletions
|
@ -30,11 +30,7 @@ impl SubscriberName {
|
||||||
// Iterate over all characters in the input `s` to check if any of them matches
|
// Iterate over all characters in the input `s` to check if any of them matches
|
||||||
// one of the characters in the forbidden array.
|
// one of the characters in the forbidden array.
|
||||||
let forbidden_characters = ['/', '(', ')', '"', '<', '>', '\\', '{', '}'];
|
let forbidden_characters = ['/', '(', ')', '"', '<', '>', '\\', '{', '}'];
|
||||||
let contains_forbidden_characters = s
|
let contains_forbidden_characters = s.chars().any(|g| forbidden_characters.contains(&g));
|
||||||
.chars()
|
|
||||||
.filter(|g| forbidden_characters.contains(g))
|
|
||||||
.count()
|
|
||||||
> 0;
|
|
||||||
|
|
||||||
if is_empty_or_whitespace || is_too_long || contains_forbidden_characters {
|
if is_empty_or_whitespace || is_too_long || contains_forbidden_characters {
|
||||||
Err(format!("{} is not a valid subscriber name.", s))
|
Err(format!("{} is not a valid subscriber name.", s))
|
||||||
|
@ -80,7 +76,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn names_containing_an_invalid_characters_are_rejected() {
|
fn names_containing_an_invalid_character_are_rejected() {
|
||||||
for name in vec!['/', '(', ')', '"', '<', '>', '\\', '{', '}'] {
|
for name in vec!['/', '(', ')', '"', '<', '>', '\\', '{', '}'] {
|
||||||
let name = name.to_string();
|
let name = name.to_string();
|
||||||
assert_err!(SubscriberName::parse(name));
|
assert_err!(SubscriberName::parse(name));
|
||||||
|
|
Loading…
Reference in a new issue