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
//! Procedural macros for the Theta actor framework.
//!
//! This crate provides the `#[actor]` attribute macro and `ActorArgs` derive macro
//! that simplify actor implementation by generating the necessary boilerplate code.
use TokenStream;
/// Attribute macro for implementing actors with automatic message handling.
///
/// This macro generates the necessary boilerplate for actor implementation,
/// including message enum generation, message processing, and remote communication
/// support when enabled.
///
/// # Default Implementations
///
/// The macro automatically provides default implementations for:
/// - `type View = Nil;` - No state updateing by default
/// - `const _: () = {};` - Empty message handler block
///
/// You only need to specify these if you want custom behavior.
///
/// # Usage
///
/// ## Basic actor (no custom View or message handlers)
/// ```
/// use theta::prelude::*;
///
/// #[derive(Debug, Clone, ActorArgs)]
/// struct MyActor { value: i32 }
///
/// #[actor("12345678-1234-5678-9abc-123456789abc")]
/// impl Actor for MyActor {}
/// ```
///
/// ## Actor with message handlers
/// ```
/// use theta::prelude::*;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Debug, Clone, ActorArgs)]
/// struct MyActor { value: i32 }
///
/// #[derive(Debug, Clone, Serialize, Deserialize)]
/// struct Increment(i32);
///
/// #[actor("12345678-1234-5678-9abc-123456789abc")]
/// impl Actor for MyActor {
/// const _: () = {
/// async |Increment(amount): Increment| {
/// self.value += amount;
/// };
/// };
/// }
/// ```
///
/// # Arguments
///
/// * `uuid` - A UUID string literal that uniquely identifies this actor type for remote
/// communication. Must be a valid UUID format (e.g., "12345678-1234-5678-9abc-123456789abc").
/// This UUID should be generated once and remain constant for the lifetime of the actor type.
///
/// ## Optional Parameters
/// * `snapshot` - Enables persistence support. When specified, automatically implements
/// `PersistentActor` trait for the actor type with default configuration:
/// - `Snapshot = T` (the actor type itself)
/// - `RuntimeArgs = ()` (no runtime arguments)
/// - `ActorArgs = T` (same as snapshot type)
///
/// Can be used as `snapshot` (defaults to `Self`) or `snapshot = CustomType` for custom snapshot types.
///
/// # Return
///
/// Generates a complete actor implementation including:
/// * `Actor` trait implementation with message processing
/// * Message enum type containing all handled message variants
/// * `ProcessMessage` trait implementation for message routing
/// * Remote communication support with serialization/deserialization
/// * Optional `PersistentActor` implementation when `snapshot` parameter is used
///
/// # Errors
///
/// Compilation errors may occur if:
/// * UUID string is not a valid UUID format
/// * Actor implementation is malformed or missing required elements
/// * Message handler closures have invalid signatures
/// * Snapshot type (when specified) does not implement required traits
///
/// # Notes
///
/// * All message types used in handlers must implement `Serialize + Deserialize`
/// * Actor state type must implement `Clone + Debug`
/// * Message handlers are defined as async closures within `const _: () = {};` blocks
/// Derive macro for actor argument types with automatic trait implementations.
///
/// This macro implements necessary traits for actor initialization arguments,
/// including `Clone` and automatic `From<&Self>` conversion for convenient
/// actor spawning patterns.
///
/// # Usage
///
/// ## Auto-args pattern (recommended)
/// When using the auto-args pattern with `ctx.spawn_auto`, the macro enables
/// convenient spawning without explicit conversion:
/// ```no_run
/// use theta::prelude::*;
///
/// #[derive(Debug, Clone, ActorArgs)]
/// struct MyActor { value: i32 }
///
/// #[actor("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")]
/// impl Actor for MyActor {}
///
/// // Usage: auto-args pattern
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let ctx = RootContext::default();
/// let actor = ctx.spawn(MyActor { value: 42 });
/// # Ok(())
/// # }
/// ```
///
/// ## Custom implementation pattern
/// For actors with custom initialization logic:
/// ```
/// use theta::prelude::*;
///
/// #[derive(Debug, Clone, ActorArgs)]
/// struct DatabaseActor {
/// connection_string: String,
/// pool_size: usize,
/// }
///
/// #[actor("11111111-2222-3333-4444-555555555555")]
/// impl Actor for DatabaseActor {}
///
/// impl DatabaseActor {
/// pub fn new(connection_string: String) -> Self {
/// Self {
/// connection_string,
/// pool_size: 10, // default pool size
/// }
/// }
/// }
/// ```
///
/// # Arguments
///
/// The derive macro is applied to struct types that serve as actor initialization arguments.
/// The struct must contain all fields necessary for actor initialization.
///
/// # Return
///
/// Automatically generates implementations for:
/// * `Clone` trait - Required for all actor argument types to enable multiple spawning
/// * `From<&Self> for Self` trait - Enables convenient reference-to-owned conversion
/// for the auto-args spawning pattern used with `ctx.spawn_auto()`
///
/// # Errors
///
/// Compilation errors may occur if:
/// * Applied to non-struct types (enums, unions not supported)
/// * Struct contains fields that do not implement `Clone`
/// * Struct has generic parameters that don't meet trait bounds
///
/// # Notes
///
/// * This derive macro is specifically designed for actor argument structs
/// * Works seamlessly with the `#[actor]` attribute macro
/// * Enables both direct instantiation and auto-args spawning patterns
/// * All fields in the struct must be cloneable for the generated `Clone` implementation
///
/// # Generated Implementations
///
/// The macro automatically generates:
/// - `Clone` trait (required for all actor args)
/// - `From<&Self> for Self` - Enables reference-to-owned conversion for spawning
/// Derive macro that generates typed TypeScript interface definitions for structs.
///
/// Emits a `wasm_bindgen(typescript_custom_section)` containing
/// `export interface StructName { field: tsType; ... }`.
///
/// Only active when the `ts` feature is enabled; generates nothing otherwise.