2024-08-24 19:29:54 +02:00
|
|
|
use crate::common::PrimaryKey;
|
|
|
|
use crate::{common, Attributes};
|
2024-08-19 19:40:14 +02:00
|
|
|
use quote::quote;
|
|
|
|
|
|
|
|
pub(crate) fn derive_diesel_crud_read_impl(
|
2024-08-24 19:29:54 +02:00
|
|
|
Attributes {
|
|
|
|
struct_ident,
|
|
|
|
table,
|
|
|
|
pk,
|
|
|
|
..
|
|
|
|
}: &Attributes,
|
2024-08-19 19:40:14 +02:00
|
|
|
) -> proc_macro2::TokenStream {
|
2024-08-24 19:29:54 +02:00
|
|
|
if pk.is_none() {
|
|
|
|
panic!("Please specify a primary key using #[diesel_crud(pk)]");
|
|
|
|
}
|
|
|
|
let PrimaryKey { ty: pk_type, .. } = pk.as_ref().unwrap();
|
|
|
|
let return_type = common::return_type(quote! { Self });
|
2024-08-19 19:40:14 +02:00
|
|
|
|
|
|
|
quote! {
|
|
|
|
#[automatically_derived]
|
2024-08-24 19:29:54 +02:00
|
|
|
impl lib::diesel_crud_trait::DieselCrudRead for #struct_ident {
|
|
|
|
type PK = #pk_type;
|
|
|
|
fn read<'a, 'b>(pk: Self::PK, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
|
2024-08-19 19:40:14 +02:00
|
|
|
where
|
2024-08-24 19:29:54 +02:00
|
|
|
Self: Sized + Sync + 'a,
|
2024-08-19 19:40:14 +02:00
|
|
|
'a: 'b
|
|
|
|
{
|
|
|
|
Box::pin(async move {
|
2024-08-24 19:29:54 +02:00
|
|
|
use diesel::associations::HasTable;
|
|
|
|
diesel_async::RunQueryDsl::get_result(
|
|
|
|
diesel::QueryDsl::find(#table::table::table(), pk),
|
|
|
|
conn
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.map_err(Into::into)
|
2024-08-19 19:40:14 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|