Files
rust-lib/crates/diesel_crud_derive/src/read.rs

35 lines
1.0 KiB
Rust

use crate::common::PrimaryKey;
use crate::{Attributes, common};
use quote::quote;
pub(crate) fn derive_diesel_crud_read_impl(
Attributes {
struct_ident,
table,
pk,
..
}: &Attributes,
) -> proc_macro2::TokenStream {
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 });
quote! {
#[automatically_derived]
impl lib::diesel_crud_trait::DieselCrudRead for #struct_ident {
type PK = #pk_type;
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)
}
}
}
}