36 lines
1.1 KiB
Rust
Raw Normal View History

use crate::{common, Attributes};
2024-08-19 19:40:14 +02:00
use quote::quote;
pub(crate) fn derive_diesel_crud_update_impl(
Attributes {
struct_ident,
table,
update,
..
}: &Attributes,
2024-08-19 19:40:14 +02:00
) -> proc_macro2::TokenStream {
let return_type = common::return_type(quote! { Self });
2024-08-19 19:40:14 +02:00
quote! {
#[automatically_derived]
impl lib::diesel_crud_trait::DieselCrudUpdate for #struct_ident {
2024-08-19 19:40:14 +02:00
type Update = #update;
fn update<'a, 'b>(update: Self::Update, conn: &'a mut diesel_async::AsyncPgConnection) -> #return_type
2024-08-19 19:40:14 +02:00
where
Self: Sized + Sync + 'a,
2024-08-19 19:40:14 +02:00
'a: 'b,
{
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)
2024-08-19 19:40:14 +02:00
})
}
}
}
}