Skip to content

Commit

Permalink
feat: #[entity] shorthand macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyko committed Sep 6, 2023
1 parent c4f2cb6 commit 9fee999
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/src/entities/person/model.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
7 changes: 7 additions & 0 deletions scyllax-macros/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
10 changes: 10 additions & 0 deletions scyllax-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
6 changes: 3 additions & 3 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 9fee999

Please sign in to comment.