Move DbUrl trait impls to newtypes.rs (#4463)

* Move DbUrl trait impls to newtypes.rs

* Update utils.rs
This commit is contained in:
dullbananas 2024-02-19 10:41:28 -07:00 committed by GitHub
parent d79502dff3
commit f56b84615c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 38 additions and 31 deletions

View file

@ -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<Text, Pg> for DbUrl {
fn to_sql(&self, out: &mut Output<Pg>) -> diesel::serialize::Result {
<std::string::String as ToSql<Text, Pg>>::to_sql(&self.0.to_string(), &mut out.reborrow())
}
}
#[cfg(feature = "full")]
impl<DB: Backend> FromSql<Text, DB> for DbUrl
where
String: FromSql<Text, DB>,
{
fn from_sql(value: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {
let str = String::from_sql(value)?;
Ok(DbUrl(Box::new(Url::parse(&str)?)))
}
}
#[cfg(feature = "full")]
impl<Kind> From<ObjectId<Kind>> for DbUrl
where
Kind: Object + Send + 'static,
for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>,
{
fn from(id: ObjectId<Kind>) -> Self {
DbUrl(Box::new(id.into()))
}
}
impl InstanceId {
pub fn inner(self) -> i32 {
self.0

View file

@ -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<Text, Pg> for DbUrl {
fn to_sql(&self, out: &mut Output<Pg>) -> diesel::serialize::Result {
<std::string::String as ToSql<Text, Pg>>::to_sql(&self.0.to_string(), &mut out.reborrow())
}
}
impl<DB: Backend> FromSql<Text, DB> for DbUrl
where
String: FromSql<Text, DB>,
{
fn from_sql(value: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {
let str = String::from_sql(value)?;
Ok(DbUrl(Box::new(Url::parse(&str)?)))
}
}
impl<Kind> From<ObjectId<Kind>> for DbUrl
where
Kind: Object + Send + 'static,
for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>,
{
fn from(id: ObjectId<Kind>) -> Self {
DbUrl(Box::new(id.into()))
}
}
pub fn now() -> AsExprOf<diesel::dsl::now, diesel::sql_types::Timestamptz> {
// https://github.com/diesel-rs/diesel/issues/1514
diesel::dsl::now.into_sql::<Timestamptz>()