presage_macros/
lib.rs

1use proc_macro::TokenStream;
2
3mod command;
4mod event;
5pub(crate) mod utils;
6
7/// Derives the [Event](https://docs.rs/presage/latest/presage/trait.Event.html) trait.
8///
9/// The name of the event is the name of the type converted to kebab case (e.g., `TodoCreated`
10/// becomes `todo-created`). To specify another name, use the `#[presage(name = "name")]` attribute.
11#[proc_macro_derive(Event, attributes(presage))]
12pub fn derive_event(event: TokenStream) -> TokenStream {
13    event::derive_event::derive_event(event)
14}
15
16/// Derives the [AggregateEvent](https://docs.rs/presage/latest/presage/trait.AggregateEvent.html)
17/// trait.
18///
19/// The name of the event is the name of the type converted to kebab case (e.g., `TodoCreated`
20/// becomes `todo-created`). To specify another name, use the `#[presage(name = "name")]` attribute.
21///
22/// The aggregate type must be provided using the `presage` attribute: `#[presage(Aggregate)]`
23/// or `#[presage(aggregate = Aggregate)]`. To extract the aggregate id from the event, an id field
24/// is required for a struct or for each variant of an enum. The id field must be annotated with the
25/// `#[id]` attribute or its name can be specified on the type with the `presage` attribute :
26/// `#[presage(Aggregate, id = id_field)]`
27#[proc_macro_derive(AggregateEvent, attributes(presage, id))]
28pub fn derive_aggregate_event(event: TokenStream) -> TokenStream {
29    event::derive_aggregate_event::derive_aggregate_event(event)
30}
31
32/// Creates an [EventHandler](https://docs.rs/presage/latest/presage/trait.EventHandler.html) from a
33/// function.
34///
35/// The function must have two parameters (the context and the handled event) and return a
36/// `Result<presage::Commands, _>`. You can use any error type, but if it cannot be extracted from
37/// the function signature (e.g., when using a type alias for `Result`), the error type must be
38/// specified as argument of the attribute: `#[event_handler(error = MyError)]`.
39#[proc_macro_attribute]
40pub fn event_handler(arguments: TokenStream, handler: TokenStream) -> TokenStream {
41    event::event_handler::event_handler(arguments, handler)
42}
43
44/// Derives the [Command](https://docs.rs/presage/latest/presage/trait.Command.html) trait.
45///
46/// The name of the command is the name of the type converted to kebab case (e.g., `CreateTodo`
47/// becomes `create-todo`). To specify another name, use the `#[presage(name = "name")]` attribute.
48#[proc_macro_derive(Command, attributes(presage))]
49pub fn derive_command(command: TokenStream) -> TokenStream {
50    command::derive_command::derive_command(command)
51}
52
53/// Creates an [CommandHandler](https://docs.rs/presage/latest/presage/trait.CommandHandler.html)
54/// from a function.
55///
56/// The function must have two parameters (the context and the handled command) and return a
57/// `Result<presage::Commands, _>`. You can use any error type, but if it cannot be extracted from
58/// the function signature (e.g., when using a type alias for `Result`), the error type must be
59/// specified as argument of the attribute: `#[command_handler(error = MyError)]`.
60#[proc_macro_attribute]
61pub fn command_handler(arguments: TokenStream, handler: TokenStream) -> TokenStream {
62    command::command_handler::command_handler(arguments, handler)
63}