Skip to main content

acktor_derive/
lib.rs

1use proc_macro::TokenStream;
2use syn::DeriveInput;
3
4mod message;
5mod message_response;
6
7/// Derive the [`Message`] trait for a struct or enum.
8///
9/// The `result_type` attribute is required and specifies the type returned
10/// when the message is handled by an actor.
11///
12/// # Examples
13///
14/// ```
15/// use acktor_derive::{Message, MessageResponse};
16///
17/// #[derive(MessageResponse)]
18/// struct Sum(i64);
19///
20/// #[derive(Message)]
21/// #[result_type = "Sum"]
22/// struct Add(i64, i64);
23/// ```
24#[proc_macro_derive(Message, attributes(result_type))]
25pub fn message_derive(input: TokenStream) -> TokenStream {
26    let ast: DeriveInput = syn::parse(input).unwrap();
27
28    message::expand(&ast).into()
29}
30
31/// Derive the [`MessageResponse`] trait for a struct or enum.
32///
33/// This implements the default response handling, which sends the value
34/// back through the oneshot channel to the caller.
35///
36/// # Examples
37///
38/// ```
39/// use acktor_derive::MessageResponse;
40///
41/// #[derive(MessageResponse)]
42/// struct Sum(i64);
43///
44/// #[derive(MessageResponse)]
45/// enum Status {
46///     Ok,
47///     Error(String),
48/// }
49/// ```
50#[proc_macro_derive(MessageResponse)]
51pub fn message_response_derive(input: TokenStream) -> TokenStream {
52    let ast: DeriveInput = syn::parse(input).unwrap();
53
54    message_response::expand(&ast).into()
55}