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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//! # Axin
//!
//! Axin is a powerful Rust procedural macro library that provides function instrumentation capabilities through the
//! `axin` attribute macro. This enables clean separation of concerns and reduces boilerplate code in applications that
//! require cross-cutting functionality like logging, timing, validation, or resource management.
//!
//! ## What can Axin do?
//!
//! Axin allows you change the behavior of functions in a declarative way using attributes. You can:
//! - add entry and exit hooks,
//! - insert statements at the beginning of function execution, and
//! - wrap functions with decorators.
//!
//! ### Entry and Exit Hooks
//!
//! These hooks allow you to execute custom functions when entering or exiting the target function. It's also possible
//! to specify arguments for these hooks, which can be used to pass context or configuration.
//!
//! ```
//! use axin::axin;
//!
//! fn setup() {
//! println!("Setting up");
//! }
//!
//! fn cleanup(msg: &str) {
//! println!("Cleaning up: {}", msg);
//! }
//!
//! #[axin(on_enter(setup), on_exit(cleanup("Goodbye from function1!")))]
//! fn function1() {
//! println!("Main logic");
//! }
//!
//! #[axin(on_enter(setup), on_exit(cleanup("Goodnight from function2!")))]
//! fn function2() {
//! println!("Different logic");
//! }
//!
//! fn main() {
//! function1();
//! function2();
//! // Output:
//! // Setting up
//! // Main logic
//! // Cleaning up: Goodbye from function1!
//! // Setting up
//! // Different logic
//! // Cleaning up: Goodnight from function2!
//! }
//! ```
//!
//! ### Prologue Statements
//!
//! Prologue statements allow you to insert arbitrary Rust code at the beginning of the function body. This can be very
//! useful sometimes, as the inserted code shares the same scope as the function, though hooks and decorators are better
//! choices for most use cases.
//!
//! ```
//! use axin::axin;
//!
//! #[axin(prologue(
//! println!("Function starting");
//! ))]
//! fn my_function() {
//! println!("Main logic");
//! }
//!
//! fn main() {
//! my_function();
//! // Output:
//! // Function starting
//! // Main logic
//! }
//! ```
//!
//! ### Decorators
//!
//! Decorators allow you to wrap the function with additional behavior. This is useful for cross-cutting concerns like
//! logging, monitoring, or authentication. Unlike decorators in some other languages, Axin decorators are called every
//! time the function is invoked, not just once at definition time.
//!
//! Decorators can have parameters, allowing you to customize their behavior based on the context in which they are
//! called. You can refer to the example below to see how to use decorators with and without parameters.
//!
//! ```
//! use axin::axin;
//!
//! fn timing_decorator<F, R>(func: F) -> R
//! where
//! F: FnOnce() -> R,
//! {
//! let start = std::time::Instant::now();
//! println!("Starting timer...");
//! let result = func();
//! println!("Took: {:?}", start.elapsed());
//! result
//! }
//!
//! #[axin(decorator(timing_decorator))]
//! fn expensive_computation() -> i32 {
//! // Simulate work
//! println!("Doing expensive computation...");
//! std::thread::sleep(std::time::Duration::from_millis(100));
//! 42
//! }
//!
//! fn custom_logging_decorator<F, R>(msg: &'static str) -> impl FnOnce(F) -> R
//! where
//! F: FnOnce() -> R,
//! {
//! move |f| {
//! println!("Custom log: {}", msg);
//! f()
//! }
//! }
//!
//! #[axin(decorator(custom_logging_decorator("Hello from decorated function!")))]
//! fn decorated_function() -> String {
//! println!("Doing something important...");
//! "Decorated result".to_string()
//! }
//!
//! fn main() {
//! let result = expensive_computation();
//! println!("Result: {}", result);
//! // Output:
//! // Starting timer...
//! // Doing expensive computation...
//! // Took: ...
//! // Result: 42
//!
//! let decorated_result = decorated_function();
//! println!("Decorated result: {}", decorated_result);
//! // Output:
//! // Custom log: Hello from decorated function!
//! // Doing something important...
//! // Decorated result: Decorated result
//! }
//! ```
//!
//! Decorators do not support variadic arguments, due to the limitation of Rust.
//!
//! ## Order of Execution
//!
//! The order of execution for the various Axin features is as follows:
//! 1. Entry hook function (if specified) is executed first, then
//! 2. Decorator function (if specified) is called, and when it calls the original function,
//! 3. Prologue statements (if specified) are executed, and then
//! 4. The original function body is executed, after which
//! 5. The control flow returns to the decorator, and after it completes,
//! 6. The exit hook function (if specified) is executed last.
use TokenStream;
use quote;
use ;
use AxinArgs;
use ;
/// An attribute procedural macro that enhances functions with entry and exit hooks, decorators, and prologue statements.
///
/// For more details, see the [Axin documentation](crate).
///
/// ## Example
///
/// ```
/// use axin::axin;
///
/// fn setup() {
/// println!("Starting function");
/// }
///
/// fn cleanup() {
/// println!("Function completed");
/// }
///
/// fn timing_decorator<F, R>(func: F) -> R
/// where F: FnOnce() -> R
/// {
/// let start = std::time::Instant::now();
/// let result = func();
/// println!("Execution time: {:?}", start.elapsed());
/// result
/// }
///
/// #[axin(
/// prologue(println!("Initializing");),
/// on_enter(setup),
/// decorator(timing_decorator),
/// on_exit(cleanup)
/// )]
/// fn instrumented_function() -> i32 {
/// println!("Core logic");
/// 42
/// }
///
/// fn main() {
/// let result = instrumented_function();
/// println!("Result: {}", result);
///
/// // Output:
/// // Starting function
/// // Initializing
/// // Core logic
/// // Execution time: ...
/// // Function completed
/// // Result: 42
/// }
/// ```