From e26107a2fe30cc2ec81797830e3a34a1676619e4 Mon Sep 17 00:00:00 2001 From: dullbananas Date: Tue, 1 Aug 2023 19:44:24 -0700 Subject: [PATCH] Update without_id.rs --- crates/db_schema/src/without_id.rs | 88 ++++++++++++++++-------------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/crates/db_schema/src/without_id.rs b/crates/db_schema/src/without_id.rs index 5d21319a3..5158b3328 100644 --- a/crates/db_schema/src/without_id.rs +++ b/crates/db_schema/src/without_id.rs @@ -15,49 +15,53 @@ /// /// The generated struct implements `Selectable` and `Queryable`. macro_rules! WithoutId { - ( - #[diesel(table_name = $table_name:ident)] - $(#[$_struct_meta:meta])* - $vis:vis struct $struct_name:ident { - $(#[$_id_meta:meta])* - $_id_vis:vis id: $id_type:ty, - $( - $(#[$_field_meta:meta])* - $field_vis:vis $field_name:ident : $field_type:ty, - )* + ( + #[diesel(table_name = $table_name:ident)] + $(#[$_struct_meta:meta])* + $vis:vis struct $struct_name:ident { + $(#[$_id_meta:meta])* + $_id_vis:vis id: $id_type:ty, + $( + // TODO: more flexible attribute matching + $(#[doc = $_doc1:tt])* + $(#[cfg($($cfgtt:tt)*)])* + $(#[cfg_attr($($cfgattrtt:tt)*)])* + $(#[serde($($serdett:tt)*)])* + $(#[doc = $_doc2:tt])* + $field_vis:vis $field_name:ident : $field_type:ty, + )* + } + ) => { + ::paste::paste! { + // TODO: remove serde derives + #[derive(::diesel::Queryable, ::diesel::Selectable, ::serde::Serialize, ::serde::Deserialize)] + #[diesel(table_name = $table_name)] + $vis struct [<$struct_name WithoutId>] { + $( + $(#[cfg($($cfgtt)*)])* + $field_vis $field_name : $field_type, + )* + } + + impl [<$struct_name WithoutId>] { + pub fn into_full(self, id: $id_type) -> $struct_name { + $struct_name { + id, + $($(#[cfg($($cfgtt)*)])* $field_name : self.$field_name,)* + } } - ) => { - ::paste::paste! { - #[derive(::diesel::Queryable, ::diesel::Selectable)] - #[diesel(table_name = $table_name)] - $vis struct [<$struct_name WithoutId>] { - $( - // Field attributes are not kept because either they are for other - // derive macros, or they are `#[cfg(...)]` which is evaluated before - // macro expansion. - $field_vis $field_name : $field_type, - )* - } + } + } + }; - impl [<$struct_name WithoutId>] { - pub fn into_full(self, id: $id_type) -> $struct_name { - $struct_name { - $($field_name : self.$field_name,)* - id, - } - } - } - } - }; + // Keep on removing the first attribute until `diesel(table_name = ...)` becomes + // the first, which will cause the first pattern to be matched. + (#[$_meta:meta] $($remaining:tt)*) => { + WithoutId!($($remaining)*); + }; - // Keep on removing the first attribute until `diesel(table_name = ...)` becomes - // the first, which will cause the first pattern to be matched. - (#[$_meta:meta] $($remaining:tt)*) => { - WithoutId!($($remaining)*); - }; - - // This pattern is matched when there's no attributes. - ($_vis:vis struct $($_tt:tt)*) => { - ::std::compile_error!("`#[diesel(table_name = ...)]` is missing"); - }; + // This pattern is matched when there's no attributes. + ($_vis:vis struct $($_tt:tt)*) => { + ::std::compile_error!("`#[diesel(table_name = ...)]` is missing"); + }; }