diff --git a/src/domain.rs b/src/domain.rs index c42f4cb..5961b46 100644 --- a/src/domain.rs +++ b/src/domain.rs @@ -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 { // `.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)) } } } diff --git a/src/routes/subscriptions.rs b/src/routes/subscriptions.rs index 2d3ea6e..6e8715c 100644 --- a/src/routes/subscriptions.rs +++ b/src/routes/subscriptions.rs @@ -24,7 +24,7 @@ pub async fn subscribe( ) -> Result { 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