Use Result

This commit is contained in:
Luca Palmieri 2020-12-11 14:24:11 +00:00
parent 1c27412280
commit 56ee4e7746
2 changed files with 3 additions and 3 deletions

View file

@ -11,7 +11,7 @@ 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 {
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.
@ -38,7 +38,7 @@ impl SubscriberName {
if is_empty_or_whitespace || is_too_long || contains_forbidden_characters {
panic!(format!("{} is not a valid subscriber name.", s))
} else {
Self(s)
Ok(Self(s))
}
}
}

View file

@ -24,7 +24,7 @@ pub async fn subscribe(
) -> Result<HttpResponse, HttpResponse> {
let new_subscriber = NewSubscriber {
email: form.0.email,
name: SubscriberName::parse(form.0.name),
name: SubscriberName::parse(form.0.name).expect("Name validation failed."),
};
insert_subscriber(&pool, &new_subscriber)
.await