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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use TokenStream;
use quote;
use ;
use crate;
use crateInventoryGenerator;
/// Code generators shared by the `#[command]` and `#[application]` macros
/// Compile-time code that links a subcommand to its parent module
/// Writes an informational message via the `Output` on `Context`
///
/// Expands to `context.output().message(format!(...)).await.expect("event channel closed")`,
/// where `context` resolves to the local variable in the calling function's scope. This is the
/// macro equivalent of `Output::message`.
///
/// The macro must be called from an async function because the core `Output` methods are async.
///
/// # Examples
///
/// ```rust,ignore
/// use clawless::prelude::*;
///
/// #[command]
/// pub async fn deploy(args: DeployArgs, context: Context) -> CommandResult {
/// message!("deploying to {}", args.target);
/// Ok(())
/// }
/// ```
/// Writes a supplementary detail via the `Output` on `Context`
///
/// Expands to `context.output().detail(format!(...)).await.expect("event channel closed")`,
/// where `context` resolves to the local variable in the calling function's scope. Detail is only
/// shown when the user passes `--verbose`. This is the macro equivalent of `Output::detail`.
///
/// The macro must be called from an async function because the core `Output` methods are async.
///
/// # Examples
///
/// ```rust,ignore
/// use clawless::prelude::*;
///
/// #[command]
/// pub async fn deploy(args: DeployArgs, context: Context) -> CommandResult {
/// detail!("config loaded from {}", args.config_path);
/// Ok(())
/// }
/// ```
/// Writes an artifact value via the `Output` on `Context`
///
/// Expands to `context.output().artifact(...).await.expect("event channel closed")`, where
/// `context` resolves to the local variable in the calling function's scope. Unlike `message!`
/// and `detail!`, this macro does not use `format!` — it takes an expression that implements
/// `Display`, `Serialize`, and `Debug`. This is the macro equivalent of `Output::artifact`.
///
/// The macro must be called from an async function because the core `Output` methods are async.
///
/// # Examples
///
/// ```rust,ignore
/// use clawless::prelude::*;
///
/// #[command]
/// pub async fn count(args: CountArgs, context: Context) -> CommandResult {
/// let count = WordCount { words: 42 };
/// artifact!(count);
/// Ok(())
/// }
/// ```
/// Expands one of the output macros into a call on the local `context`
///
/// The `message!`, `detail!`, and `artifact!` macros differ only in the `Output` method that
/// they call. They therefore share one expansion.
///
/// This function builds the `context` identifier with [`Span::call_site`]. The identifier then
/// resolves to the local variable of the caller, not to a hygienic name.
///
/// [`Span::call_site`]: proc_macro2::Span::call_site
/// Set up the commands module for a Clawless application
///
/// This macro generates the root command for the command-line application and allows subcommands to
/// be registered under it. It should be called inside `src/commands.rs` or `src/commands/mod.rs` to
/// follow Clawless's convention.
///
/// # Example
///
/// ```rust,ignore
/// // src/commands.rs
/// mod greet;
/// mod deploy;
///
/// clawless::commands!();
/// ```
/// Initialize and run a Clawless application
///
/// This macro generates the `main` function for a Clawless application. It uses two-phase
/// dispatch: first it parses arguments and resolves the subcommand tree to find the leaf, then
/// it matches on the `ResolvedLeaf` variant to delegate to the appropriate runner.
///
/// # Example
///
/// ```rust,ignore
/// // src/main.rs
/// mod commands;
///
/// clawless::main!();
/// ```
/// Add a command to a Clawless application
///
/// This macro attribute can be used to register a function as a (sub)command in
/// a Clawless application. The name of the function will be used as the name of
/// the command, and it will be automatically registered as a subcommand under
/// its parent module.
///
/// Command functions must accept exactly two parameters:
/// 1. An `args` parameter: a `clap::Args` struct with the command's arguments
/// 2. A `context` parameter: the `Context` providing access to the application environment
/// and the cancellation token for cooperative shutdown
///
/// # Attributes
///
/// - `alias = "name"` - Add a visible alias for the command. Can be repeated for multiple aliases.
/// - `require_subcommand` - Require a subcommand; show help if the command is invoked without one.
///
/// # Requiring Subcommands
///
/// Use `require_subcommand` to create a command that serves as a container for subcommands. When
/// this attribute is set, invoking the command without a subcommand will display help instead of
/// running the command body. This is useful for organizing related commands under a common prefix.
///
/// For example, a CLI might have `db migrate`, `db seed`, and `db reset` commands, where `db`
/// itself requires a subcommand and doesn't perform any action on its own.
///
/// # Examples
///
/// Basic command:
///
/// ```rust,ignore
/// use clawless::prelude::*;
///
/// #[derive(Debug, Args)]
/// pub struct GreetArgs {
/// #[arg(short, long)]
/// name: String,
/// }
///
/// #[command]
/// pub async fn greet(args: GreetArgs, context: Context) -> CommandResult {
/// message!("Hello, {}!", args.name);
/// Ok(())
/// }
/// ```
///
/// Command with alias:
///
/// ```rust,ignore
/// use clawless::prelude::*;
///
/// #[derive(Debug, Args)]
/// pub struct GenerateArgs {}
///
/// // Users can run `mycli generate` or `mycli g`
/// #[command(alias = "g")]
/// pub async fn generate(args: GenerateArgs, context: Context) -> CommandResult {
/// Ok(())
/// }
/// ```
///
/// Command that requires a subcommand:
///
/// ```rust,ignore
/// use clawless::prelude::*;
///
/// #[derive(Debug, Args)]
/// pub struct DbArgs {}
///
/// // Running `mycli db` shows help; users must specify a subcommand like `mycli db migrate`
/// #[command(require_subcommand, alias = "d")]
/// pub async fn db(args: DbArgs, context: Context) -> CommandResult {
/// Ok(())
/// }
/// ```
/// Add a TUI application to a Clawless project
///
/// This macro attribute registers a function as a TUI application in a Clawless project. Unlike
/// `#[command]`, which creates a stateless CLI command rendered through a push-based presenter,
/// `#[application]` creates a stateful TUI application that queries a pull-based projection.
///
/// Application functions must accept exactly three parameters:
/// 1. An `args` parameter: a `clap::Args` struct with the application's arguments
/// 2. A `context` parameter: the `Context` for emitting events and cooperative shutdown
/// 3. A `projection` parameter: the `Projection` for querying accumulated state
///
/// # Attributes
///
/// - `alias = "name"` - Add a visible alias for the application. Can be repeated.
/// - `require_subcommand` - Require a subcommand; show help if invoked without one.
///
/// # Examples
///
/// ```rust,ignore
/// use clawless::prelude::*;
/// use clawless::tui::projection::Projection;
///
/// #[derive(Debug, Args)]
/// pub struct DashboardArgs {
/// #[arg(short, long, default_value = "3000")]
/// port: u16,
/// }
///
/// /// Interactive project dashboard
/// #[application]
/// pub async fn dashboard(
/// args: DashboardArgs,
/// context: Context,
/// projection: Projection,
/// ) -> CommandResult {
/// Ok(())
/// }
/// ```
///