mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-21 22:27:08 +00:00
Move DbUrl trait impls to newtypes.rs (#4463)
* Move DbUrl trait impls to newtypes.rs * Update utils.rs
This commit is contained in:
parent
d79502dff3
commit
f56b84615c
|
@ -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
|
||||
|
|
|
@ -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>()
|
||||
|
|
Loading…
Reference in a new issue