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:
Ivan Tham 2020-12-23 05:19:50 +08:00 committed by GitHub
parent 9fa3183fd7
commit 6af7e78ef5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,11 +30,7 @@ impl SubscriberName {
// 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()
.filter(|g| forbidden_characters.contains(g))
.count()
> 0;
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))
@ -80,7 +76,7 @@ mod tests {
}
#[test]
fn names_containing_an_invalid_characters_are_rejected() {
fn names_containing_an_invalid_character_are_rejected() {
for name in vec!['/', '(', ')', '"', '<', '>', '\\', '{', '}'] {
let name = name.to_string();
assert_err!(SubscriberName::parse(name));