scyllax_macros/lib.rs
1//! Macros supporting scyllax.
2//!
3//! See the [scyllax docs](https://docs.rs/scyllax) for more information.
4use proc_macro::TokenStream;
5use scyllax_macros_core::{entity, json, prepare, queries, r#enum};
6
7/// Apply this attribute to a struct to generate a select query.
8/// ## Single result
9/// ```rust,ignore
10/// #[read_query(
11/// query = "select * from person where id = ? limit 1",
12/// entity_type = "PersonEntity"
13/// )]
14/// pub struct GetPersonById {
15/// pub id: Uuid,
16/// }
17/// executor.execute_select(GetPersonById { id }).await?;
18/// // -> Option<PersonEntity>
19/// ```
20/// ## Multiple results
21/// ```rust,ignore
22/// #[read_query(
23/// query = "select * from person where id in ? limit ?",
24/// return_type = "Vec<PersonEntity>"
25/// )]
26/// pub struct GetPeopleByIds {
27/// pub ids: Vec<Uuid>,
28/// pub limit: i32,
29/// }
30/// executor.execute_select(GetPeopleByIds { ids, limit }).await?;
31/// // -> Vec<PersonEntity>
32/// ```
33#[proc_macro_derive(ReadQuery, attributes(read_query))]
34pub fn read_query(input: TokenStream) -> TokenStream {
35 queries::read::expand(input.into()).into()
36}
37
38/// Apply this attribute to a struct to generate a write query.
39/// ```rust,ignore
40/// #[write_query(
41/// query = "delete from person where id = ?",
42/// )]
43/// pub struct DeletePersonById {
44/// pub id: Uuid,
45/// }
46/// ```
47#[proc_macro_attribute]
48pub fn write_query(args: TokenStream, input: TokenStream) -> TokenStream {
49 queries::write::expand(args.into(), input.into()).into()
50}
51
52/// Apply this attribute to a entity struct to generate an upsert query.
53/// ```rust,ignore
54/// #[upsert_query(table = "person", name = UpsertPerson)]
55/// #[derive(Clone, Debug, FromRow, PartialEq, ValueList, Entity)]
56/// pub struct PersonEntity {
57/// #[pk]
58/// pub id: uuid::Uuid,
59/// pub email: String,
60/// pub created_at: i64,
61/// }
62/// ```
63#[proc_macro_attribute]
64pub fn upsert_query(args: TokenStream, input: TokenStream) -> TokenStream {
65 queries::upsert::expand(args.into(), input.into()).into()
66}
67
68/// Implements [`scyllax::EntityExt`](scyllax::EntityExt) for the struct.
69#[proc_macro_derive(Entity, attributes(entity))]
70pub fn entity_derive(input: TokenStream) -> TokenStream {
71 entity::expand(input.into()).into()
72}
73
74/// Shorthand for applying derive macros on an entity. Essentially:
75/// ```rust,ignore
76/// #[derive(Clone, Debug, FromRow, PartialEq, ValueList, Entity)]
77/// #input
78/// ```
79#[proc_macro_attribute]
80pub fn entity(args: TokenStream, input: TokenStream) -> TokenStream {
81 entity::expand_attr(args.into(), input.into()).into()
82}
83
84/// Implements
85/// * [`scylla::frame::value::Value`] and
86/// * [`scylla::cql_to_rust::FromCqlVal`]
87///
88/// for a struct.
89#[proc_macro_derive(JsonData, attributes(rename))]
90pub fn json_derive(input: TokenStream) -> TokenStream {
91 json::expand(input.into()).into()
92}
93
94/// Shorthand for applying derive macros on a JSON body. Essentially:
95/// ```rust,ignore
96/// #[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, scyllax::prelude::Json)]
97/// #input
98/// ```
99#[proc_macro_attribute]
100pub fn json_data(args: TokenStream, input: TokenStream) -> TokenStream {
101 json::expand_attr(args.into(), input.into()).into()
102}
103
104/// Implements
105/// * [`scylla::frame::value::Value`] and
106/// * [`scylla::cql_to_rust::FromCqlVal`]
107/// * [`TryFrom<i32>`]
108///
109/// for an enum. See [`int_enum`] for more.
110#[proc_macro_derive(IntEnum, attributes(rename))]
111pub fn int_enum_derive(input: TokenStream) -> TokenStream {
112 r#enum::expand(input.into()).into()
113}
114
115/// Sets up a scylla-and-protobuf compatible enum.
116#[proc_macro_attribute]
117pub fn int_enum(args: TokenStream, input: TokenStream) -> TokenStream {
118 r#enum::expand_attr(args.into(), input.into()).into()
119}
120
121#[proc_macro]
122pub fn create_query_collection(input: TokenStream) -> TokenStream {
123 prepare::expand(input.into()).into()
124}