From 3695967ec9cf7c69ac32b9a8dfe276d020e9c6ae Mon Sep 17 00:00:00 2001 From: Franklin Date: Wed, 15 Mar 2023 08:45:46 -0400 Subject: [PATCH] Finished todo --- src/domain/mod.rs | 3 ++- src/domain/project.rs | 1 - src/domain/project_condition/impls.rs | 32 ++++++++++++++++++++++++++ src/domain/project_condition/mod.rs | 33 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 src/domain/project_condition/impls.rs create mode 100644 src/domain/project_condition/mod.rs diff --git a/src/domain/mod.rs b/src/domain/mod.rs index ba0a2dd..c8920c3 100644 --- a/src/domain/mod.rs +++ b/src/domain/mod.rs @@ -7,4 +7,5 @@ pub mod project_state; pub mod error; pub mod credential; pub mod unit_type; -pub mod media; \ No newline at end of file +pub mod media; +pub mod project_condition; \ No newline at end of file diff --git a/src/domain/project.rs b/src/domain/project.rs index 49fd465..bcc6dd8 100644 --- a/src/domain/project.rs +++ b/src/domain/project.rs @@ -4,7 +4,6 @@ use uuid::Uuid; use super::{media::MediaList, project_type::ProjectType}; -//TODO: Add ProjectCondition enum (New or Resale) #[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct Project { pub id: Uuid, diff --git a/src/domain/project_condition/impls.rs b/src/domain/project_condition/impls.rs new file mode 100644 index 0000000..3e4679a --- /dev/null +++ b/src/domain/project_condition/impls.rs @@ -0,0 +1,32 @@ + +use sqlx::{Postgres, postgres::{PgValueRef, PgTypeInfo, PgArgumentBuffer}, error::BoxDynError, encode::IsNull}; + +use super::ProjectCondition; +use std::str::FromStr; + +impl sqlx::Encode<'_, Postgres> for ProjectCondition { + fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull { + let binding = self.to_string(); + <&str as sqlx::Encode>::encode(&binding, buf) + } +} + +impl sqlx::Decode<'_, Postgres> for ProjectCondition { + fn decode(value: PgValueRef<'_>) -> Result { + let column = value.as_str()?; + match Self::from_str(column) { + Ok(listing_state) => Ok(listing_state), + Err(error) => Err(Box::new(error)), + } + } +} + +impl sqlx::Type for ProjectCondition { + fn type_info() -> PgTypeInfo { + PgTypeInfo::with_name("VARCHAR") + } + + fn compatible(ty: &::TypeInfo) -> bool { + *ty == Self::type_info() + } +} diff --git a/src/domain/project_condition/mod.rs b/src/domain/project_condition/mod.rs new file mode 100644 index 0000000..6e539d1 --- /dev/null +++ b/src/domain/project_condition/mod.rs @@ -0,0 +1,33 @@ +use std::{fmt::Display, str::FromStr}; + +use serde::{Serialize, Deserialize}; + +use super::error::Error; + +#[cfg(feature = "sqlx")] +pub mod impls; + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] +pub enum ProjectCondition { + New, + Resale, +} + +impl Display for ProjectCondition { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ProjectCondition::New => write!(f, "Nuevo"), + ProjectCondition::Resale => write!(f, "Reventa"), + } + } +} + +impl FromStr for ProjectCondition { + type Err = Error; + + fn from_str(s: &str) -> Result { + match s { + _ => Err(Error::Parsing), + } + } +} \ No newline at end of file