From f56b84615c78a0ca9942e7a82c8d7ce4255ec5cb Mon Sep 17 00:00:00 2001 From: dullbananas Date: Mon, 19 Feb 2024 10:41:28 -0700 Subject: [PATCH] Move DbUrl trait impls to newtypes.rs (#4463) * Move DbUrl trait impls to newtypes.rs * Update utils.rs --- crates/db_schema/src/newtypes.rs | 37 ++++++++++++++++++++++++++++++++ crates/db_schema/src/utils.rs | 32 +-------------------------- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/crates/db_schema/src/newtypes.rs b/crates/db_schema/src/newtypes.rs index 5e5970e91..96fc23ac6 100644 --- a/crates/db_schema/src/newtypes.rs +++ b/crates/db_schema/src/newtypes.rs @@ -6,6 +6,14 @@ use activitypub_federation::{ traits::Object, }; #[cfg(feature = "full")] +use diesel::{ + backend::Backend, + deserialize::FromSql, + pg::Pg, + serialize::{Output, ToSql}, + sql_types::Text, +}; +#[cfg(feature = "full")] use diesel_ltree::Ltree; use serde::{Deserialize, Serialize}; use std::{ @@ -239,6 +247,35 @@ impl TS for DbUrl { } } +#[cfg(feature = "full")] +impl ToSql for DbUrl { + fn to_sql(&self, out: &mut Output) -> diesel::serialize::Result { + >::to_sql(&self.0.to_string(), &mut out.reborrow()) + } +} + +#[cfg(feature = "full")] +impl FromSql for DbUrl +where + String: FromSql, +{ + fn from_sql(value: DB::RawValue<'_>) -> diesel::deserialize::Result { + let str = String::from_sql(value)?; + Ok(DbUrl(Box::new(Url::parse(&str)?))) + } +} + +#[cfg(feature = "full")] +impl From> for DbUrl +where + Kind: Object + Send + 'static, + for<'de2> ::Kind: serde::Deserialize<'de2>, +{ + fn from(id: ObjectId) -> Self { + DbUrl(Box::new(id.into())) + } +} + impl InstanceId { pub fn inner(self) -> i32 { self.0 diff --git a/crates/db_schema/src/utils.rs b/crates/db_schema/src/utils.rs index 213e0015e..6afc850b5 100644 --- a/crates/db_schema/src/utils.rs +++ b/crates/db_schema/src/utils.rs @@ -5,20 +5,16 @@ use crate::{ CommentSortType, SortType, }; -use activitypub_federation::{fetch::object_id::ObjectId, traits::Object}; use anyhow::Context; use chrono::{DateTime, Utc}; use deadpool::Runtime; use diesel::{ - backend::Backend, - deserialize::FromSql, helper_types::AsExprOf, pg::Pg, query_builder::{Query, QueryFragment}, query_dsl::methods::LimitDsl, result::{ConnectionError, ConnectionResult, Error as DieselError, Error::QueryBuilderError}, - serialize::{Output, ToSql}, - sql_types::{self, Text, Timestamptz}, + sql_types::{self, Timestamptz}, IntoSql, PgConnection, }; @@ -475,32 +471,6 @@ pub mod functions { pub const DELETED_REPLACEMENT_TEXT: &str = "*Permanently Deleted*"; -impl ToSql for DbUrl { - fn to_sql(&self, out: &mut Output) -> diesel::serialize::Result { - >::to_sql(&self.0.to_string(), &mut out.reborrow()) - } -} - -impl FromSql for DbUrl -where - String: FromSql, -{ - fn from_sql(value: DB::RawValue<'_>) -> diesel::deserialize::Result { - let str = String::from_sql(value)?; - Ok(DbUrl(Box::new(Url::parse(&str)?))) - } -} - -impl From> for DbUrl -where - Kind: Object + Send + 'static, - for<'de2> ::Kind: serde::Deserialize<'de2>, -{ - fn from(id: ObjectId) -> Self { - DbUrl(Box::new(id.into())) - } -} - pub fn now() -> AsExprOf { // https://github.com/diesel-rs/diesel/issues/1514 diesel::dsl::now.into_sql::()