From 9fee999afc2cad9b0b5f26f7556144f90822791a Mon Sep 17 00:00:00 2001 From: Carter Himmel Date: Wed, 6 Sep 2023 16:55:59 -0600 Subject: [PATCH] feat: `#[entity]` shorthand macro --- example/src/entities/person/model.rs | 2 +- scyllax-macros/src/entity.rs | 7 +++++++ scyllax-macros/src/lib.rs | 10 ++++++++++ src/prelude.rs | 6 +++--- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/example/src/entities/person/model.rs b/example/src/entities/person/model.rs index 5629900..d3e1856 100644 --- a/example/src/entities/person/model.rs +++ b/example/src/entities/person/model.rs @@ -1,8 +1,8 @@ use scyllax::prelude::*; /// Represents a person in the database +#[entity] #[upsert_query(table = "person", name = UpsertPerson)] -#[derive(Clone, Debug, FromRow, PartialEq, ValueList, Entity)] pub struct PersonEntity { /// The id of the person #[pk] diff --git a/scyllax-macros/src/entity.rs b/scyllax-macros/src/entity.rs index 9aa1caf..c08b7ef 100644 --- a/scyllax-macros/src/entity.rs +++ b/scyllax-macros/src/entity.rs @@ -73,6 +73,13 @@ pub fn get_field_name(field: &Field) -> String { .to_string() } +pub fn expand_attr(_args: TokenStream, input: TokenStream) -> TokenStream { + quote! { + #[derive(Clone, Debug, PartialEq, scyllax::FromRow, scyllax::prelude::ValueList, scyllax::Entity)] + #input + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/scyllax-macros/src/lib.rs b/scyllax-macros/src/lib.rs index bab5b10..f12950f 100644 --- a/scyllax-macros/src/lib.rs +++ b/scyllax-macros/src/lib.rs @@ -76,3 +76,13 @@ pub fn upsert_query(args: TokenStream, input: TokenStream) -> TokenStream { pub fn entity_derive(input: TokenStream) -> TokenStream { entity::expand(input.into()).into() } + +/// Shorthand for applying derive macros on an entity. Essentially: +/// ```rust,ignore +/// #[derive(Clone, Debug, FromRow, PartialEq, ValueList, Entity)] +/// #input +/// ``` +#[proc_macro_attribute] +pub fn entity(args: TokenStream, input: TokenStream) -> TokenStream { + entity::expand_attr(args.into(), input.into()).into() +} diff --git a/src/prelude.rs b/src/prelude.rs index 3402e7a..e448e28 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,8 +1,8 @@ //! Re-exports of the most commonly used types and traits. pub use crate::{ - error::BuildUpsertQueryError, executor::Executor, maybe_unset::MaybeUnset, select_query, - upsert_query, util::v1_uuid, DeleteQuery, Entity, EntityExt, FromRow, ImplValueList, - ScyllaxError, SelectQuery, UpsertQuery, + entity, error::BuildUpsertQueryError, executor::Executor, maybe_unset::MaybeUnset, + select_query, upsert_query, util::v1_uuid, DeleteQuery, Entity, EntityExt, FromRow, + ImplValueList, ScyllaxError, SelectQuery, UpsertQuery, }; pub use scylla::frame::value::SerializeValuesError;