diff --git a/core/src/join.rs b/core/src/join.rs index 1ea4097..4c4877a 100644 --- a/core/src/join.rs +++ b/core/src/join.rs @@ -1,12 +1,12 @@ use crate::model::Model; use async_trait::async_trait; +use serde::de::Error; use serde::Deserialize; use serde::{Serialize, Serializer}; use sqlmo::query::Join as JoinQueryFragment; use sqlmo::query::SelectColumn; use sqlx::{Database, Decode, Encode, Type}; use std::ops::{Deref, DerefMut}; -use serde::de::Error; pub trait JoinMeta { type IdType: Clone + Send + Eq + PartialEq + std::hash::Hash; @@ -37,15 +37,16 @@ pub trait Loadable { E: 'e + sqlx::Executor<'e, Database = DB>; } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Join { pub id: T::IdType, data: JoinData, } /// Only represents a many-to-one relationship. -#[derive(Debug)] +#[derive(Debug, Default)] pub enum JoinData { + #[default] NotQueried, QueryResult(T), Modified(T), @@ -228,20 +229,23 @@ impl Serialize for Join { } impl<'de, T> Deserialize<'de> for Join +where + T: JoinMeta + Deserialize<'de>, +{ + fn deserialize(deserializer: D) -> Result where - T: JoinMeta + Deserialize<'de>, + D: serde::Deserializer<'de>, { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let data = Option::::deserialize(deserializer)?; - - let (id_type, join_data) = match data { - Some(value) => (T::_id(&value), JoinData::QueryResult(value)), - None => return Err(D::Error::custom("Invalid value")) - }; - - Ok(Join { id: id_type, data: join_data }) - } + let data = Option::::deserialize(deserializer)?; + + let (id_type, join_data) = match data { + Some(value) => (T::_id(&value), JoinData::QueryResult(value)), + None => return Err(D::Error::custom("Invalid value")), + }; + + Ok(Join { + id: id_type, + data: join_data, + }) } +}