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
extern crate proc_macro;
extern crate quote;
/// Generate actor system struct and inner methods.
/// # Arguments
/// * `message_types` - Message structs or enums that the actor system will use
///
/// # Example
/// ```rust
/// xan_actor::actor_system!(
/// pub struct StructMessage {
/// pub message: String,
/// },
/// enum EnumMessage {
/// A,
/// B,
/// }
/// );
/// ```
/// Genereate actor struct and inner methods
/// # Arguments
/// * `actor_struct_name` - Name of the actor struct
/// * `message_type` - Message struct or enum that the actor will receive
/// * `resource_struct` - Resource struct that the actor will use
/// * `handle_message` - Method that will handle the message
/// * `pre_start` - Method that will be called before the actor starts
/// * `post_stop` - Method that will be called after the actor stops
/// * `pre_restart` - Method that will be called before the actor restarts
/// * `post_restart` - Method that will be called after the actor restarts
/// * `kill_in_error` - kill the actor if an error occurs - TODO: not supported yet
///
/// # Example
/// ```rust
/// xan_actor::actor_system!(); // It always needs to be called once in lib.rs or main.rs
///
/// xan_actor::actor!(
/// TestActor,
/// Message,
/// struct TestActorResource {
/// pub name: String,
/// },
/// fn handle_message(&self, message: Message) -> String {
/// message.message
/// },
/// fn pre_start(&mut self) {},
/// fn post_stop(&mut self) {},
/// fn pre_restart(&mut self) {},
/// fn post_restart(&mut self) {},
/// true
/// );
/// ```
/// Send messages to actors
/// # Arguments
/// * `actor_system` - Actor system that the actor belongs to
/// * `address` - Address of the actor(can use "*")
/// * `message` - Message to send
///
/// # Example
/// ```rust
/// xan_actor::send_msg!(
/// &mut actor_system,
/// address!("root".to_string(), "parent".to_string(), "*".to_string()),
/// &"test".to_string()
/// );
/// ```
/// Receive responses from actors
/// # Arguments
/// * `res_type` - Type of the response
/// * `response_rxs` - Response receiver vector
///
/// # Example
/// ```rust
/// let response_rxs = xan_actor::send_msg!(
/// &mut actor_system,
/// address!("root".to_string(), "parent".to_string(), "*".to_string()),
/// &"test".to_string()
/// );
/// for response in xan_actor::recv_res!(
/// String,
/// response_rxs,
/// ) {
/// println!("{}", response);
/// }
/// ```
/// Generate an actor address
/// # Arguments
/// * `parts` - Address parts. It takes multiple strings
///
/// # Example
/// ```rust
/// let address = xan_actor::address!("root".to_string(), "parent".to_string(), "child".to_string());
///
/// // you can use it like regex
/// let address = xan_actor::address!("root".to_string(), "parent".to_string(), "*".to_string());
/// let address = xan_actor::address!("root".to_string(), "*".to_string(), "child".to_string());
/// ```