Rewritten diesel_crud to be more flexible

This commit is contained in:
Martin Berg Alstad
2024-08-24 19:29:54 +02:00
parent 3318aacf7c
commit 17c81f4da1
15 changed files with 385 additions and 348 deletions

View File

@ -1,35 +1,28 @@
use proc_macro2::Ident;
use crate::{common, Attributes};
use quote::quote;
use syn::Expr;
use crate::{common, StructAttributes};
pub(crate) fn derive_diesel_crud_list_impl(
StructAttributes { table, entity, .. }: &StructAttributes,
identifier: &Ident,
Attributes {
struct_ident,
table,
..
}: &Attributes,
) -> proc_macro2::TokenStream {
let body = function_body(table);
let return_type = common::return_type(quote! { Vec<Self::Entity> });
let return_type = common::return_type(quote! { Vec<Self> });
quote! {
#[automatically_derived]
impl lib::diesel_crud_trait::DieselCrudList for #identifier {
type Entity = #entity;
fn list<'a, 'b>(&'a self) -> #return_type
impl lib::diesel_crud_trait::DieselCrudList for #struct_ident {
fn list<'a, 'b>(conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
where
Self: Sync + 'a,
Self: Sized + Sync + 'a,
'a: 'b
{
Box::pin(async move {
#body
use diesel::associations::HasTable;
diesel_async::RunQueryDsl::get_results(#table::table::table(), conn).await.map_err(Into::into)
})
}
}
}
}
fn function_body(table: &Expr) -> proc_macro2::TokenStream {
common::function_body(quote! {
diesel_async::RunQueryDsl::get_results(#table::table::table(), &mut connection).await.map_err(Into::into)
})
}