Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Use TryFrom instead of Decode for Subscription fields
Browse files Browse the repository at this point in the history
  • Loading branch information
0rzech committed Mar 11, 2024
1 parent 590814c commit 50fbfd7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 32 deletions.
18 changes: 4 additions & 14 deletions src/domain/subscriber_email.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use serde::Deserialize;
use sqlx::{
error::BoxDynError,
postgres::{PgTypeInfo, PgValueRef},
Decode, Postgres, Type,
};
use validator::validate_email;

#[derive(Clone, Debug, Deserialize)]
Expand All @@ -25,16 +20,11 @@ impl AsRef<str> for SubscriberEmail {
}
}

impl Type<Postgres> for SubscriberEmail {
fn type_info() -> PgTypeInfo {
String::type_info()
}
}
impl TryFrom<String> for SubscriberEmail {
type Error = String;

impl<'r> Decode<'r, Postgres> for SubscriberEmail {
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
let email = String::decode(value)?;
Self::parse(email).map_err(|e| e.into())
fn try_from(s: String) -> Result<Self, Self::Error> {
Self::parse(s)
}
}

Expand Down
18 changes: 4 additions & 14 deletions src/domain/subscriber_name.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use once_cell::sync::Lazy;
use serde::Deserialize;
use sqlx::{
error::BoxDynError,
postgres::{PgTypeInfo, PgValueRef},
Decode, Postgres, Type,
};
use unicode_segmentation::UnicodeSegmentation;

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -37,16 +32,11 @@ impl AsRef<str> for SubscriberName {
}
}

impl Type<Postgres> for SubscriberName {
fn type_info() -> PgTypeInfo {
String::type_info()
}
}
impl TryFrom<String> for SubscriberName {
type Error = String;

impl<'r> Decode<'r, Postgres> for SubscriberName {
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
let name = String::decode(value)?;
Self::parse(name).map_err(|e| e.into())
fn try_from(s: String) -> Result<Self, Self::Error> {
Self::parse(s)
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/domain/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ use uuid::Uuid;
#[derive(FromRow)]
pub struct Subscription {
pub id: Uuid,
#[sqlx(try_from = "String")]
pub email: SubscriberEmail,
#[sqlx(try_from = "String")]
pub name: SubscriberName,
pub subscribed_at: OffsetDateTime,
#[sqlx(try_from = "String")]
pub status: SubscriptionStatus,
}
19 changes: 15 additions & 4 deletions src/domain/subscription_status.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use sqlx::Type;

#[derive(PartialEq, Type)]
#[sqlx(type_name = "text", rename_all = "snake_case")]
#[derive(PartialEq)]
pub enum SubscriptionStatus {
PendingConfirmation,
Confirmed,
Expand All @@ -15,3 +12,17 @@ impl AsRef<str> for SubscriptionStatus {
}
}
}

impl TryFrom<String> for SubscriptionStatus {
type Error = String;

fn try_from(s: String) -> Result<Self, Self::Error> {
match s.as_ref() {
"pending_confirmation" => Ok(SubscriptionStatus::PendingConfirmation),
"confirmed" => Ok(SubscriptionStatus::Confirmed),
other => Err(format!(
"`{other}` is not a valid variant of SubscriptionStatus",
)),
}
}
}

0 comments on commit 50fbfd7

Please sign in to comment.