✨ Replaced async_trait with async in traits from 2024 edition
This commit is contained in:
@ -9,6 +9,6 @@ pub(crate) struct PrimaryKey {
|
||||
|
||||
pub(crate) fn return_type(output: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
|
||||
quote! {
|
||||
std::pin::Pin<Box<dyn core::future::Future<Output = Result<#output, lib::diesel_crud_trait::CrudError>> + Send + 'async_trait>>
|
||||
Result<#output, lib::diesel_crud_trait::CrudError>
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{common, Attributes};
|
||||
use crate::{Attributes, common};
|
||||
use quote::quote;
|
||||
|
||||
pub(crate) fn derive_diesel_crud_create_impl(
|
||||
@ -16,37 +16,24 @@ pub(crate) fn derive_diesel_crud_create_impl(
|
||||
#[automatically_derived]
|
||||
impl lib::diesel_crud_trait::DieselCrudCreate<#table::table> for #struct_ident {
|
||||
type Insert = #insert;
|
||||
fn insert<'a, 'async_trait>(insert: Self::Insert, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
|
||||
where
|
||||
Self: Sized + Sync + 'a,
|
||||
'a: 'async_trait,
|
||||
{
|
||||
Box::pin(async move {
|
||||
use diesel::associations::HasTable;
|
||||
diesel_async::RunQueryDsl::get_result(
|
||||
diesel::dsl::insert_into(#table::table::table()).values(insert),
|
||||
conn
|
||||
)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
})
|
||||
async fn insert(insert: Self::Insert, conn: &mut diesel_async::AsyncPgConnection) -> #return_type {
|
||||
use diesel::associations::HasTable;
|
||||
diesel_async::RunQueryDsl::get_result(
|
||||
diesel::dsl::insert_into(#table::table::table()).values(insert),
|
||||
conn
|
||||
)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
fn insert_many<'a, 'b, 'async_trait>(insert: &'a [Self::Insert], conn: &'b mut diesel_async::AsyncPgConnection) -> #many_return_type
|
||||
where
|
||||
Self: Sized + Sync + 'async_trait,
|
||||
'a: 'async_trait,
|
||||
'b: 'async_trait,
|
||||
{
|
||||
Box::pin(async move {
|
||||
use diesel::associations::HasTable;
|
||||
diesel_async::RunQueryDsl::get_results(
|
||||
diesel::dsl::insert_into(#table::table::table()).values(insert),
|
||||
conn
|
||||
)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
})
|
||||
async fn insert_many(insert: &[Self::Insert], conn: &mut diesel_async::AsyncPgConnection) -> #many_return_type {
|
||||
use diesel::associations::HasTable;
|
||||
diesel_async::RunQueryDsl::get_results(
|
||||
diesel::dsl::insert_into(#table::table::table()).values(insert),
|
||||
conn
|
||||
)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{common, Attributes, PrimaryKey};
|
||||
use crate::{Attributes, PrimaryKey, common};
|
||||
use quote::quote;
|
||||
|
||||
pub(crate) fn derive_diesel_crud_delete_impl(
|
||||
@ -22,24 +22,18 @@ pub(crate) fn derive_diesel_crud_delete_impl(
|
||||
#[automatically_derived]
|
||||
impl lib::diesel_crud_trait::DieselCrudDelete for #struct_ident {
|
||||
type PK = #pk_type;
|
||||
fn delete<'a, 'async_trait>(pk: Self::PK, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
|
||||
where
|
||||
Self: Sized + Sync + 'a,
|
||||
'a: 'async_trait,
|
||||
{
|
||||
Box::pin(async move {
|
||||
use diesel::QueryDsl;
|
||||
use diesel::associations::HasTable;
|
||||
diesel_async::RunQueryDsl::get_result(
|
||||
diesel::delete(
|
||||
#table::table
|
||||
.filter(diesel::expression_methods::ExpressionMethods::eq(#table::#pk_ident, pk))
|
||||
),
|
||||
conn,
|
||||
)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
})
|
||||
async fn delete(pk: Self::PK, conn: &mut diesel_async::AsyncPgConnection) -> #return_type {
|
||||
use diesel::QueryDsl;
|
||||
use diesel::associations::HasTable;
|
||||
diesel_async::RunQueryDsl::get_result(
|
||||
diesel::delete(
|
||||
#table::table
|
||||
.filter(diesel::expression_methods::ExpressionMethods::eq(#table::#pk_ident, pk))
|
||||
),
|
||||
conn,
|
||||
)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{common, Attributes};
|
||||
use crate::{Attributes, common};
|
||||
use quote::quote;
|
||||
|
||||
pub(crate) fn derive_diesel_crud_list_impl(
|
||||
@ -13,15 +13,9 @@ pub(crate) fn derive_diesel_crud_list_impl(
|
||||
quote! {
|
||||
#[automatically_derived]
|
||||
impl lib::diesel_crud_trait::DieselCrudList for #struct_ident {
|
||||
fn list<'a, 'async_trait>(conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
|
||||
where
|
||||
Self: Sized + Sync + 'a,
|
||||
'a: 'async_trait
|
||||
{
|
||||
Box::pin(async move {
|
||||
use diesel::associations::HasTable;
|
||||
diesel_async::RunQueryDsl::get_results(#table::table::table(), conn).await.map_err(Into::into)
|
||||
})
|
||||
async fn list(conn: &mut diesel_async::AsyncPgConnection) -> #return_type {
|
||||
use diesel::associations::HasTable;
|
||||
diesel_async::RunQueryDsl::get_results(#table::table::table(), conn).await.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::common::PrimaryKey;
|
||||
use crate::{common, Attributes};
|
||||
use crate::{Attributes, common};
|
||||
use quote::quote;
|
||||
|
||||
pub(crate) fn derive_diesel_crud_read_impl(
|
||||
@ -20,20 +20,14 @@ pub(crate) fn derive_diesel_crud_read_impl(
|
||||
#[automatically_derived]
|
||||
impl lib::diesel_crud_trait::DieselCrudRead for #struct_ident {
|
||||
type PK = #pk_type;
|
||||
fn read<'a, 'async_trait>(pk: Self::PK, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
|
||||
where
|
||||
Self: Sized + Sync + 'a,
|
||||
'a: 'async_trait
|
||||
{
|
||||
Box::pin(async move {
|
||||
use diesel::associations::HasTable;
|
||||
diesel_async::RunQueryDsl::get_result(
|
||||
diesel::QueryDsl::find(#table::table::table(), pk),
|
||||
conn
|
||||
)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
})
|
||||
async fn read(pk: Self::PK, conn: &mut diesel_async::AsyncPgConnection) -> #return_type {
|
||||
use diesel::associations::HasTable;
|
||||
diesel_async::RunQueryDsl::get_result(
|
||||
diesel::QueryDsl::find(#table::table::table(), pk),
|
||||
conn
|
||||
)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{common, Attributes};
|
||||
use crate::{Attributes, common};
|
||||
use quote::quote;
|
||||
|
||||
pub(crate) fn derive_diesel_crud_update_impl(
|
||||
@ -15,20 +15,14 @@ pub(crate) fn derive_diesel_crud_update_impl(
|
||||
#[automatically_derived]
|
||||
impl lib::diesel_crud_trait::DieselCrudUpdate for #struct_ident {
|
||||
type Update = #update;
|
||||
fn update<'a, 'async_trait>(update: Self::Update, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
|
||||
where
|
||||
Self: Sized + Sync + 'a,
|
||||
'a: 'async_trait,
|
||||
{
|
||||
Box::pin(async move {
|
||||
use diesel::associations::HasTable;
|
||||
diesel_async::RunQueryDsl::get_result(
|
||||
diesel::dsl::update(#table::table::table()).set(update),
|
||||
conn,
|
||||
)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
})
|
||||
async fn update(update: Self::Update, conn: &mut diesel_async::AsyncPgConnection) -> #return_type {
|
||||
use diesel::associations::HasTable;
|
||||
diesel_async::RunQueryDsl::get_result(
|
||||
diesel::dsl::update(#table::table::table()).set(update),
|
||||
conn,
|
||||
)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,4 @@ rust-version = { workspace = true }
|
||||
[dependencies]
|
||||
diesel = { workspace = true, features = ["postgres"] }
|
||||
diesel-async = { workspace = true, features = ["postgres", "deadpool"] }
|
||||
async-trait = "0.1"
|
||||
deadpool-diesel = { version = "0.6", features = ["postgres"] }
|
||||
thiserror = { workspace = true }
|
||||
|
@ -1,6 +1,5 @@
|
||||
mod error;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use diesel::{AsChangeset, Insertable};
|
||||
use diesel_async::AsyncPgConnection;
|
||||
pub use error::CrudError;
|
||||
@ -28,17 +27,19 @@ pub trait DieselCrud<Table>:
|
||||
/// - `conn` - The database connection
|
||||
/// # Returns
|
||||
/// A result containing the inserted entity or a `CrudError`
|
||||
#[async_trait]
|
||||
pub trait DieselCrudCreate<Table>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
type Insert: Insertable<Table>;
|
||||
async fn insert(insert: Self::Insert, conn: &mut AsyncPgConnection) -> Result<Self, CrudError>;
|
||||
async fn insert_many(
|
||||
fn insert(
|
||||
insert: Self::Insert,
|
||||
conn: &mut AsyncPgConnection,
|
||||
) -> impl Future<Output = Result<Self, CrudError>>;
|
||||
fn insert_many(
|
||||
insert: &[Self::Insert],
|
||||
conn: &mut AsyncPgConnection,
|
||||
) -> Result<Vec<Self>, CrudError>;
|
||||
) -> impl Future<Output = Result<Vec<Self>, CrudError>>;
|
||||
}
|
||||
|
||||
/// Gets an entity from the database
|
||||
@ -52,13 +53,15 @@ where
|
||||
/// # Returns
|
||||
/// A result containing the entity or a `CrudError`.
|
||||
/// If the entity is not found, the error should be `CrudError::NotFound`.
|
||||
#[async_trait]
|
||||
pub trait DieselCrudRead
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
type PK;
|
||||
async fn read(pk: Self::PK, conn: &mut AsyncPgConnection) -> Result<Self, CrudError>;
|
||||
fn read(
|
||||
pk: Self::PK,
|
||||
conn: &mut AsyncPgConnection,
|
||||
) -> impl Future<Output = Result<Self, CrudError>>;
|
||||
}
|
||||
|
||||
/// Updates an entity in the database
|
||||
@ -73,13 +76,15 @@ where
|
||||
/// # Returns
|
||||
/// A result containing the old entry of the entity if successful or a `CrudError`.
|
||||
/// If the entity is not found, the error should be `CrudError::NotFound`.
|
||||
#[async_trait]
|
||||
pub trait DieselCrudUpdate
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
type Update: AsChangeset;
|
||||
async fn update(update: Self::Update, conn: &mut AsyncPgConnection) -> Result<Self, CrudError>;
|
||||
fn update(
|
||||
update: Self::Update,
|
||||
conn: &mut AsyncPgConnection,
|
||||
) -> impl Future<Output = Result<Self, CrudError>>;
|
||||
}
|
||||
|
||||
/// Deletes an entity from the database
|
||||
@ -93,13 +98,15 @@ where
|
||||
/// # Returns
|
||||
/// A result containing the deleted entity or a `CrudError`.
|
||||
/// If the entity is not found, the error should be `CrudError::NotFound`.
|
||||
#[async_trait]
|
||||
pub trait DieselCrudDelete
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
type PK;
|
||||
async fn delete(pk: Self::PK, conn: &mut AsyncPgConnection) -> Result<Self, CrudError>;
|
||||
fn delete(
|
||||
pk: Self::PK,
|
||||
conn: &mut AsyncPgConnection,
|
||||
) -> impl Future<Output = Result<Self, CrudError>>;
|
||||
}
|
||||
|
||||
/// Lists all entities in the table
|
||||
@ -109,10 +116,9 @@ where
|
||||
/// - `conn` - The database connection
|
||||
/// # Returns
|
||||
/// A result containing a Vec of entities or a `CrudError`.
|
||||
#[async_trait]
|
||||
pub trait DieselCrudList
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
async fn list(conn: &mut AsyncPgConnection) -> Result<Vec<Self>, CrudError>;
|
||||
fn list(conn: &mut AsyncPgConnection) -> impl Future<Output = Result<Vec<Self>, CrudError>>;
|
||||
}
|
||||
|
Reference in New Issue
Block a user