event_driven_macro/
lib.rs

1use aggregate::render_aggregate_token;
2
3use message::{find_identifier, render_event_visibility, render_message_token};
4// use outbox::render_outbox_token;
5
6use proc_macro::TokenStream;
7use syn::{DeriveInput, ItemFn};
8
9#[macro_use]
10extern crate quote;
11mod aggregate;
12
13mod handler;
14mod message;
15mod result;
16mod utils;
17
18#[proc_macro_derive(Message, attributes(internally_notifiable, externally_notifiable, identifier))]
19pub fn message_derive(attr: TokenStream) -> TokenStream {
20	let ast: DeriveInput = syn::parse(attr.clone()).unwrap();
21	let propagatability = render_event_visibility(&ast);
22	let identifier = find_identifier(&ast);
23
24	render_message_token(&ast, propagatability, identifier).into()
25}
26
27#[proc_macro_derive(Aggregate)]
28pub fn aggregate_derive(attr: TokenStream) -> TokenStream {
29	let ast: DeriveInput = syn::parse(attr.clone()).unwrap();
30
31	render_aggregate_token(&ast)
32}
33
34/// Define ApplicationResponse so that could be recognized by messagebus
35/// ## Example
36/// ```ignore
37/// #[derive(Debug, ApplicationResponse)]
38/// enum ServiceResponse{
39///     Response1
40///     Response2
41/// }
42/// ```
43#[proc_macro_derive(ApplicationResponse)]
44pub fn response_derive(attr: TokenStream) -> TokenStream {
45	let ast: DeriveInput = syn::parse(attr.clone()).unwrap();
46	result::render_response_token(&ast)
47}
48
49/// Define a Application Error type that can be used in the event-driven-library.
50///
51/// Before deriving this, you must impl `Debug`traits.
52///
53/// This macro can be only used in enum.
54///
55/// ## Attributes
56///
57/// - `#[crates(...)]` - Specify the name of root of event-driven-library crate. (Default is `event_driven_library`)
58/// - `#[stop_sentinel]` - Specify the error matching for `BaseError::StopSentinel`.
59/// - `#[stop_sentinel_with_event]` - Specify the error matching for `BaseError::StopSentinelWithEvent`.
60/// - `#[database_error]` - Specify the error matching for `BaseError::DatabaseError`.
61///
62/// ## Example
63/// ```ignore
64/// #[derive(Debug, ApplicationError)]
65/// #[crates(crate::imports::event_driven_library)]
66/// enum TestError {
67///   #[stop_sentinel]
68///   Stop,
69///   #[stop_sentinel_with_event]
70///   StopWithEvent(Box<AnyError>),
71///   #[database_error]
72///   DatabaseError(Box<AnyError>),
73/// }
74/// ```
75#[proc_macro_derive(ApplicationError, attributes(stop_sentinel, stop_sentinel_with_event, database_error, crates))]
76pub fn error_derive(attr: TokenStream) -> TokenStream {
77	let ast: DeriveInput = syn::parse(attr).unwrap();
78
79	result::render_error_token(&ast)
80}
81
82#[proc_macro_attribute]
83pub fn entity(_: TokenStream, input: TokenStream) -> TokenStream {
84	aggregate::render_entity_token(input)
85}
86
87#[proc_macro_derive(Command)]
88pub fn command_derive(attr: TokenStream) -> TokenStream {
89	let ast: DeriveInput = syn::parse(attr.clone()).unwrap();
90	let name = ast.ident;
91
92	quote!(
93		impl Command for #name{}
94	)
95	.into()
96}
97
98#[proc_macro_attribute]
99pub fn message_handler(_: TokenStream, input: TokenStream) -> TokenStream {
100	let ast: ItemFn = syn::parse_macro_input!(input as ItemFn);
101
102	handler::parse_handler(ast)
103}