evento-macro 1.8.0

A collection of libraries and tools that help you build DDD, CQRS, and event sourcing.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! # Evento Macros
//!
//! This crate provides procedural macros for the Evento event sourcing framework.
//! These macros simplify the implementation of aggregators and event handlers by
//! generating boilerplate code automatically.
//!
//! ## Macros
//!
//! - [`aggregator`] - Implements the [`Aggregator`] trait for structs with event handler methods
//! - [`handler`] - Creates event handler functions for use with subscriptions
//! - [`AggregatorName`] - Derives the [`AggregatorName`] trait for event types
//!
//! ## Usage
//!
//! This crate is typically used through the main `evento` crate with the `macro` feature enabled:
//!
//! ```toml
//! [dependencies]
//! evento = { version = "1.0", features = ["macro"] }
//! ```
//!
//! ## Examples
//!
//! See the individual macro documentation for detailed usage examples.

use convert_case::{Case, Casing};
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use sha3::{Digest, Sha3_256};
use std::ops::Deref;
use syn::{parse_macro_input, spanned::Spanned, Ident, ItemFn, ItemImpl, ItemStruct};

/// Creates an event handler for use with event subscriptions
///
/// The `#[evento::handler(AggregateType)]` attribute macro transforms a function into an event handler
/// that can be used with [`evento::subscribe`]. The macro generates the necessary boilerplate to
/// integrate with the subscription system.
///
/// # Syntax
///
/// ```ignore
/// #[evento::handler(AggregateType)]
/// async fn handler_name<E: evento::Executor>(
///     context: &evento::Context<'_, E>,
///     event: EventDetails<EventType>,
/// ) -> anyhow::Result<()> {
///     // Handler logic
///     Ok(())
/// }
/// ```
///
/// # Parameters
///
/// - `AggregateType`: The aggregate type this handler is associated with
///
/// # Function Requirements
///
/// The decorated function must:
/// - Be `async`
/// - Take `&evento::Context<'_, E>` as first parameter where `E: evento::Executor`
/// - Take `EventDetails<SomeEventType>` as second parameter
/// - Return `anyhow::Result<()>`
///
/// # Examples
///
/// ```no_run
/// use evento::{Context, EventDetails, Executor};
/// use bincode::{Encode, Decode};
///
/// # use evento::AggregatorName;
/// # #[derive(AggregatorName, Encode, Decode)]
/// # struct UserCreated { name: String }
/// # #[derive(Default, Encode, Decode, Clone, Debug)]
/// # struct User;
/// # #[evento::aggregator]
/// # impl User {}
///
/// #[evento::handler(User)]
/// async fn on_user_created<E: Executor>(
///     context: &Context<'_, E>,
///     event: EventDetails<UserCreated>,
/// ) -> anyhow::Result<()> {
///     println!("User created: {}", event.data.name);
///     
///     // Can trigger side effects, call external services, etc.
///     
///     Ok(())
/// }
///
/// // Use with subscription
/// # async fn setup(executor: evento::Sqlite) -> anyhow::Result<()> {
/// evento::subscribe("user-handlers")
///     .aggregator::<User>()
///     .handler(on_user_created())
///     .run(&executor)
///     .await?;
/// # Ok(())
/// # }
/// ```
///
/// # Generated Code
///
/// The macro generates:
/// - A struct implementing [`evento::SubscribeHandler`]
/// - A constructor function returning an instance of that struct
/// - Type-safe event filtering based on the event type
#[proc_macro_attribute]
pub fn handler(attr: TokenStream, item: TokenStream) -> TokenStream {
    let item: ItemFn = parse_macro_input!(item);
    let fn_ident = item.sig.ident.to_owned();
    let struct_ident = item.sig.ident.to_string().to_case(Case::UpperCamel);
    let struct_ident = Ident::new(&struct_ident, item.span());
    let aggregator_ident = Ident::new(&attr.to_string(), item.span());
    let Some(syn::FnArg::Typed(event_arg)) = item.sig.inputs.get(1) else {
        return syn::Error::new_spanned(item, "Unable to find event input")
            .into_compile_error()
            .into();
    };

    let syn::Type::Path(ty) = *event_arg.ty.clone() else {
        return syn::Error::new_spanned(item, "Unable to find event input type")
            .into_compile_error()
            .into();
    };

    let Some(event_arg_segment) = ty.path.segments.first() else {
        return syn::Error::new_spanned(item, "Unable to find event input type iden")
            .into_compile_error()
            .into();
    };

    let syn::PathArguments::AngleBracketed(ref arguments) = event_arg_segment.arguments else {
        return syn::Error::new_spanned(item, "Unable to find event input type iden")
            .into_compile_error()
            .into();
    };

    let Some(syn::GenericArgument::Type(syn::Type::Path(ty))) = arguments.args.first() else {
        return syn::Error::new_spanned(item, "Unable to find event input type iden")
            .into_compile_error()
            .into();
    };

    let Some(segment) = ty.path.segments.first() else {
        return syn::Error::new_spanned(item, "Unable to find event input type iden")
            .into_compile_error()
            .into();
    };

    let event_ident = segment.ident.to_owned();

    quote! {
        struct #struct_ident;

        fn #fn_ident() -> #struct_ident { #struct_ident }

        impl<E: evento::Executor> evento::SubscribeHandler<E> for #struct_ident {
            fn handle<'async_trait>(&'async_trait self, context: &'async_trait evento::Context<'_, E>) -> std::pin::Pin<Box<dyn std::future::Future<Output=anyhow::Result<()>> + Send + 'async_trait>>
            where
                Self: Sync + 'async_trait
            {
                Box::pin(async move {
                    let Ok(details) = context.event.to_details() else {
                        anyhow::bail!("Failed to deserialize event details data/metadata, it seams that evento::save(id).data(a).metadata(b) and handler(event: EventDetails<a,b>) not matching.");
                    };

                    if let Some(data) = details {
                        return Self::#fn_ident(context, data).await;
                    }

                    Ok(())
                })
            }

            fn aggregator_type(&self) -> &'static str{ #aggregator_ident::name() }
            fn event_name(&self) -> &'static str { #event_ident::name() }
        }

        impl #struct_ident {
            #item
        }
    }.into()
}

/// Implements the [`evento::Aggregator`] trait for structs with event handler methods
///
/// The `#[evento::aggregator]` attribute macro automatically implements the [`evento::Aggregator`]
/// trait by generating an `aggregate` method that dispatches events to the appropriate handler
/// methods based on event type.
///
/// # Syntax
///
/// ```ignore
/// #[evento::aggregator]
/// impl AggregateStruct {
///     async fn event_handler_name(&mut self, event: EventDetails<EventType>) -> anyhow::Result<()> {
///         // Update self based on event
///         Ok(())
///     }
/// }
/// ```
///
/// # Requirements
///
/// - The struct must implement all required traits for [`evento::Aggregator`]:
///   - `Default`, `Send`, `Sync`, `Clone`, `Debug`
///   - `bincode::Encode`, `bincode::Decode`
///   - [`evento::AggregatorName`]
/// - Handler methods must be `async`
/// - Handler methods must take `&mut self` and `EventDetails<SomeEventType>`
/// - Handler methods must return `anyhow::Result<()>`
///
/// # Event Matching
///
/// The macro matches events to handler methods by calling [`evento::Event::to_details`] with
/// the event type inferred from the handler method's parameter type.
///
/// # Examples
///
/// ```no_run
/// use evento::{EventDetails, AggregatorName};
/// use serde::{Deserialize, Serialize};
/// use bincode::{Encode, Decode};
///
/// #[derive(AggregatorName, Encode, Decode)]
/// struct UserCreated {
///     name: String,
///     email: String,
/// }
///
/// #[derive(AggregatorName, Encode, Decode)]
/// struct UserEmailChanged {
///     email: String,
/// }
///
/// #[derive(Default, Serialize, Deserialize, Encode, Decode, Clone, Debug)]
/// struct User {
///     name: String,
///     email: String,
///     version: u32,
/// }
///
/// #[evento::aggregator]
/// impl User {
///     async fn user_created(&mut self, event: EventDetails<UserCreated>) -> anyhow::Result<()> {
///         self.name = event.data.name;
///         self.email = event.data.email;
///         self.version = event.version as u32;
///         Ok(())
///     }
///
///     async fn user_email_changed(&mut self, event: EventDetails<UserEmailChanged>) -> anyhow::Result<()> {
///         self.email = event.data.email;
///         self.version = event.version as u32;
///         Ok(())
///     }
/// }
/// ```
///
/// # Generated Code
///
/// The macro generates:
/// - Implementation of [`evento::Aggregator::aggregate`] with event dispatching
/// - Implementation of [`evento::Aggregator::revision`] based on a hash of the handler methods
/// - Implementation of [`evento::AggregatorName`] using the package name and struct name
#[proc_macro_attribute]
pub fn aggregator(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let item: ItemImpl = parse_macro_input!(item);
    let mut hasher = Sha3_256::new();

    let syn::Type::Path(item_path) = item.self_ty.deref() else {
        return syn::Error::new_spanned(item, "Unable to find name of impl struct")
            .into_compile_error()
            .into();
    };

    let Some(ident) = item_path.path.get_ident() else {
        return syn::Error::new_spanned(item, "Unable to get ident of impl struct")
            .into_compile_error()
            .into();
    };

    let handler_fns = item
        .items
        .iter()
        .filter_map(|item| {
            let syn::ImplItem::Fn(item_fn) = item else {
                return None;
            };

            hasher.update(item_fn.to_token_stream().to_string());
            let ident = item_fn.sig.ident.clone();

            Some(quote! {
                let Ok(details) = event.to_details() else {
                        anyhow::bail!("Failed to deserialize event details data/metadata, it seams that evento::save(id).data(a).metadata(b) and handler(event: EventDetails<a,b>) not matching.");
                    };

                if let Some(data) = details {
                    self.#ident(data).await?;
                    return Ok(());
                }
            })
        })
        .collect::<proc_macro2::TokenStream>();

    let revision = format!("{:x}", hasher.finalize());
    let name = ident.to_string();

    quote! {
        #item

        impl evento::Aggregator for #ident {
            fn aggregate<'async_trait>(&'async_trait mut self, event: &'async_trait evento::Event) -> std::pin::Pin<Box<dyn std::future::Future<Output=anyhow::Result<()>> + Send + 'async_trait>>
            where
                Self: Sync + 'async_trait
            {
                Box::pin(async move {
                    #handler_fns

                    Ok(())
                })
            }

            fn revision() -> &'static str {
                #revision
            }
        }

        impl evento::AggregatorName for #ident {
            fn name() -> &'static str {
                static NAME: std::sync::LazyLock<String> = std::sync::LazyLock::new(||{
                    format!("{}/{}", env!("CARGO_PKG_NAME"), #name)
                });

                &NAME
            }
        }
    }
    .into()
}

/// Derives the [`evento::AggregatorName`] trait for event types
///
/// The `#[derive(AggregatorName)]` macro automatically implements the [`evento::AggregatorName`]
/// trait, which provides a name identifier for the type. This is essential for event types
/// as it allows the event system to identify and match events by name.
///
/// # Usage
///
/// Apply this derive macro to structs that represent events:
///
/// ```no_run
/// use evento::AggregatorName;
/// use bincode::{Encode, Decode};
///
/// #[derive(AggregatorName, Encode, Decode)]
/// struct UserCreated {
///     name: String,
///     email: String,
/// }
///
/// // The derived implementation returns the struct name as a string
/// assert_eq!(UserCreated::name(), "UserCreated");
/// ```
///
/// # Requirements
///
/// Event types using this derive must also implement:
/// - `bincode::Encode` and `bincode::Decode` for serialization
/// - Usually derived together: `#[derive(AggregatorName, Encode, Decode)]`
///
/// # Generated Implementation
///
/// The macro generates a simple implementation that returns the struct name as a static string:
///
/// ```ignore
/// impl evento::AggregatorName for YourEventType {
///     fn name() -> &'static str {
///         "YourEventType"
///     }
/// }
/// ```
///
/// # Event Identification
///
/// The name returned by this trait is used by the event system to:
/// - Match events to their types during deserialization
/// - Route events to appropriate handlers
/// - Filter events in subscriptions
#[proc_macro_derive(AggregatorName)]
pub fn derive_aggregator_name(input: TokenStream) -> TokenStream {
    let ItemStruct { ident, .. } = parse_macro_input!(input);
    let name = ident.to_string();

    quote! {
        impl evento::AggregatorName for #ident {
            fn name() -> &'static str {
                #name
            }
        }
    }
    .into()
}