Implement TryFrom instead of TryInto.

This commit is contained in:
Luca Palmieri 2021-09-01 09:22:31 +02:00
parent a786105daa
commit ee52fa7071

View file

@ -2,7 +2,7 @@ use crate::domain::{NewSubscriber, SubscriberEmail, SubscriberName};
use actix_web::{web, HttpResponse}; use actix_web::{web, HttpResponse};
use chrono::Utc; use chrono::Utc;
use sqlx::PgPool; use sqlx::PgPool;
use std::convert::TryInto; use std::convert::{TryFrom, TryInto};
use uuid::Uuid; use uuid::Uuid;
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
@ -11,13 +11,13 @@ pub struct FormData {
name: String, name: String,
} }
impl TryInto<NewSubscriber> for FormData { impl TryFrom<FormData> for NewSubscriber {
type Error = String; type Error = String;
fn try_into(self) -> Result<NewSubscriber, Self::Error> { fn try_from(value: FormData) -> Result<Self, Self::Error> {
let name = SubscriberName::parse(self.name)?; let name = SubscriberName::parse(value.name)?;
let email = SubscriberEmail::parse(self.email)?; let email = SubscriberEmail::parse(value.email)?;
Ok(NewSubscriber { email, name }) Ok(Self { email, name })
} }
} }