From 4d3b4ba283aa002e3714f913c4e744927b5cbb22 Mon Sep 17 00:00:00 2001 From: phiresky Date: Mon, 9 Sep 2024 19:59:17 +0200 Subject: [PATCH] fix lints --- .../src/source/community_post_tag.rs | 8 ++-- .../db_views/src/community_post_tags_view.rs | 29 ++++++++++++ crates/db_views/src/lib.rs | 2 + crates/db_views/src/structs.rs | 46 ++----------------- 4 files changed, 40 insertions(+), 45 deletions(-) create mode 100644 crates/db_views/src/community_post_tags_view.rs diff --git a/crates/db_schema/src/source/community_post_tag.rs b/crates/db_schema/src/source/community_post_tag.rs index 8b094b2f6..6feeacd17 100644 --- a/crates/db_schema/src/source/community_post_tag.rs +++ b/crates/db_schema/src/source/community_post_tag.rs @@ -1,10 +1,10 @@ -use crate::{ - newtypes::{CommunityId, CommunityPostTagId, DbUrl, PostId}, - schema::{community_post_tag, post_community_post_tag}, -}; +use crate::newtypes::{CommunityId, CommunityPostTagId, DbUrl, PostId}; +#[cfg(feature = "full")] +use crate::schema::{community_post_tag, post_community_post_tag}; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; +#[cfg(feature = "full")] use ts_rs::TS; /// A tag that can be assigned to a post within a community. diff --git a/crates/db_views/src/community_post_tags_view.rs b/crates/db_views/src/community_post_tags_view.rs new file mode 100644 index 000000000..43c0ee913 --- /dev/null +++ b/crates/db_views/src/community_post_tags_view.rs @@ -0,0 +1,29 @@ +use crate::structs::PostCommunityPostTags; +use diesel::{ + deserialize::FromSql, + pg::{Pg, PgValue}, + serialize::ToSql, + sql_types::{self, Nullable}, +}; + +impl FromSql, Pg> for PostCommunityPostTags { + fn from_sql(bytes: PgValue) -> diesel::deserialize::Result { + let value = >::from_sql(bytes)?; + Ok(serde_json::from_value::(value)?) + } + fn from_nullable_sql( + bytes: Option<::RawValue<'_>>, + ) -> diesel::deserialize::Result { + match bytes { + Some(bytes) => Self::from_sql(bytes), + None => Ok(Self { tags: vec![] }), + } + } +} + +impl ToSql, Pg> for PostCommunityPostTags { + fn to_sql(&self, out: &mut diesel::serialize::Output) -> diesel::serialize::Result { + let value = serde_json::to_value(self)?; + >::to_sql(&value, &mut out.reborrow()) + } +} diff --git a/crates/db_views/src/lib.rs b/crates/db_views/src/lib.rs index e93c7409d..7c0a071e7 100644 --- a/crates/db_views/src/lib.rs +++ b/crates/db_views/src/lib.rs @@ -6,6 +6,8 @@ pub mod comment_report_view; #[cfg(feature = "full")] pub mod comment_view; #[cfg(feature = "full")] +pub mod community_post_tags_view; +#[cfg(feature = "full")] pub mod custom_emoji_view; #[cfg(feature = "full")] pub mod local_image_view; diff --git a/crates/db_views/src/structs.rs b/crates/db_views/src/structs.rs index a5dfea88a..5ad3981a8 100644 --- a/crates/db_views/src/structs.rs +++ b/crates/db_views/src/structs.rs @@ -1,12 +1,7 @@ #[cfg(feature = "full")] use diesel::Queryable; -use diesel::{ - deserialize::{FromSql, FromSqlRow}, - expression::AsExpression, - pg::{Pg, PgValue}, - serialize::ToSql, - sql_types::{self, Nullable}, -}; +#[cfg(feature = "full")] +use diesel::{deserialize::FromSqlRow, expression::AsExpression, sql_types}; use lemmy_db_schema::{ aggregates::structs::{CommentAggregates, PersonAggregates, PostAggregates, SiteAggregates}, source::{ @@ -238,41 +233,10 @@ pub struct LocalImageView { pub person: Person, } -#[derive( - Clone, - serde::Serialize, - serde::Deserialize, - Debug, - PartialEq, - TS, - FromSqlRow, - AsExpression, - Default, -)] +#[derive(Clone, serde::Serialize, serde::Deserialize, Debug, PartialEq, Default)] +#[cfg_attr(feature = "full", derive(TS, FromSqlRow, AsExpression))] #[serde(transparent)] -#[diesel(sql_type = Nullable)] +#[cfg_attr(feature = "full", diesel(sql_type = Nullable))] pub struct PostCommunityPostTags { pub tags: Vec, } - -impl FromSql, Pg> for PostCommunityPostTags { - fn from_sql(bytes: PgValue) -> diesel::deserialize::Result { - let value = >::from_sql(bytes)?; - Ok(serde_json::from_value::(value)?) - } - fn from_nullable_sql( - bytes: Option<::RawValue<'_>>, - ) -> diesel::deserialize::Result { - match bytes { - Some(bytes) => Self::from_sql(bytes), - None => Ok(Self { tags: vec![] }), - } - } -} - -impl ToSql, Pg> for PostCommunityPostTags { - fn to_sql(&self, out: &mut diesel::serialize::Output) -> diesel::serialize::Result { - let value = serde_json::to_value(self)?; - >::to_sql(&value, &mut out.reborrow()) - } -}