Add SubscriberName constructor.

This commit is contained in:
Luca Palmieri 2020-12-09 17:20:15 +00:00
parent 2953d329ff
commit 0eaf310f29
3 changed files with 40 additions and 0 deletions

1
Cargo.lock generated
View file

@ -2901,5 +2901,6 @@ dependencies = [
"tracing-futures",
"tracing-log",
"tracing-subscriber",
"unicode-segmentation",
"uuid",
]

View file

@ -28,6 +28,7 @@ tracing-bunyan-formatter = "0.1.6"
tracing-log = "0.1.1"
tracing-actix-web = "0.2.0"
serde-aux = "1.0.1"
unicode-segmentation = "1.7.1"
[dev-dependencies]
reqwest = { version = "0.10.7", features = ["json"] }

View file

@ -1 +1,39 @@
use unicode_segmentation::UnicodeSegmentation;
pub struct SubscriberName(String);
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) -> SubscriberName {
// `.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()
.filter(|g| forbidden_characters.contains(g))
.count()
> 0;
if is_empty_or_whitespace || is_too_long || contains_forbidden_characters {
panic!(format!("{} is not a valid subscriber name.", s))
} else {
Self(s)
}
}
}