comlexr_macro 1.5.0

Dynamically build Command objects with conditional expressions
Documentation
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
extern crate quote;
extern crate syn;

use quote::quote;
use syn::parse_macro_input;

mod cmd;
mod macros;
mod pipe;

/// Generates a command expression by combining static strings, conditional logic, loops,
/// pattern matching, and closures to dynamically build and format command-line arguments.
///
/// The `cmd!` macro supports flexible syntax constructs such as:
/// - **Static arguments**: Basic string arguments.
/// - **Conditional inclusion**: Using `if` or `if let` to conditionally include arguments.
/// - **Iterative inclusion**: Using `for` loops to include multiple arguments from collections.
/// - **Pattern matching**: Using `match` expressions for dynamic argument selection.
/// - **Closures**: Dynamically generating arguments at runtime.
///
/// # Examples
///
/// ## Basic Usage
/// ```
/// # use comlexr_macro::cmd;
/// let command = cmd!("echo", "test");
/// assert_eq!(format!("{command:?}"), r#""echo" "test""#.to_string());
/// ```
///
/// ## Current Directory
/// ```
/// # use comlexr_macro::cmd;
/// let command = cmd!(
///     cd "~/";
///     "echo",
///     "test",
/// );
///
/// assert_eq!(format!("{command:?}"), r#"cd "~/" && "echo" "test""#);
/// ```
///
/// ## Environment Vars
/// ```
/// # use comlexr_macro::cmd;
/// const NEW_VAR: &str = "NEW_VAR";
///
/// let command = cmd!(
///     env {
///         "TEST": "test",
///         NEW_VAR: "new_var"
///     };
///     "echo",
///     "test",
/// );
///
/// assert_eq!(format!("{command:?}"), r#"NEW_VAR="new_var" TEST="test" "echo" "test""#);
/// ```
///
/// #### Conditional
/// You can have a default value set for an environment variable.
///
/// ```rust
/// # use comlexr_macro::cmd;
/// const NEW_VAR: &str = "NEW_VAR";
/// std::env::set_var("TEST", "realvalue");
///
/// let command = cmd!(
///     env {
///         "TEST":? "test",
///         NEW_VAR: "new_var"
///     };
///     "echo",
///     "test",
/// );
///
/// assert_eq!(format!("{command:?}"), r#"NEW_VAR="new_var" "echo" "test""#);
/// ```
///
/// ### Current Directory and Environment Variable Order
/// ```
/// # use comlexr_macro::cmd;
/// let command = cmd!(
///     cd "~/";
///     env {
///         "TEST": "test",
///     };
///     "echo",
///     "test",
/// );
///
/// assert_eq!(
///     format!("{command:?}"),
///     r#"cd "~/" && TEST="test" "echo" "test""#
/// );
///
/// ```
///
/// ## Conditional Arguments
/// ```
/// # use comlexr_macro::cmd;
/// let include_arg = true;
///
/// let command = cmd!("echo", "test", if include_arg => "optional_arg");
/// assert_eq!(format!("{command:?}"), r#""echo" "test" "optional_arg""#.to_string());
/// ```
///
/// ## Conditional Pattern Matching
/// ```
/// # use comlexr_macro::cmd;
/// let single_option = Some("single");
/// let multi_option: Option<&str> = None;
///
/// let command = cmd!(
///     "echo",
///     "test",
///     if let Some(arg) = single_option => arg,
///     if let Some(arg) = multi_option => [
///         "multi",
///         arg,
///     ],
/// );
/// assert_eq!(format!("{command:?}"), r#""echo" "test" "single""#.to_string());
/// ```
///
/// ## Iterative Argument Inclusion
/// ```
/// # use comlexr_macro::cmd;
/// let args = &["arg1", "arg2"];
///
/// let command = cmd!("echo", for args);
/// assert_eq!(format!("{command:?}"), r#""echo" "arg1" "arg2""#.to_string());
/// ```
///
/// ## Iteration with `for in`
/// ```
/// # use comlexr_macro::cmd;
/// let single_iter = &["arg1", "arg2"];
/// let multi_iter = &["multi1", "multi2"];
///
/// let command = cmd!(
///     "echo",
///     "test",
///     for arg in single_iter => arg,
///     for arg in multi_iter => [
///         "multi",
///         arg,
///     ],
/// );
/// assert_eq!(format!("{command:?}"), r#""echo" "test" "arg1" "arg2" "multi" "multi1" "multi" "multi2""#.to_string());
/// ```
///
/// ## Match Statements
/// ```
/// # use comlexr_macro::cmd;
/// enum TestArgs {
///     Arg1,
///     Arg2,
///     Arg3,
/// }
///
/// let match_arg = TestArgs::Arg2;
/// let command = cmd!(
///     "echo",
///     "test",
///     match match_arg {
///         TestArgs::Arg1 => "arg1",
///         TestArgs::Arg2 => ["arg1", "arg2"],
///         TestArgs::Arg3 => ["arg1", "arg2", "arg3"],
///     }
/// );
/// assert_eq!(format!("{command:?}"), r#""echo" "test" "arg1" "arg2""#.to_string());
/// ```
///
/// ## Dynamic Closures
/// ```
/// # use comlexr_macro::cmd;
/// let numbers = vec![1, 2, 3];
/// let multiplier = 2;
///
/// let command = cmd!("echo", || numbers.into_iter().map(|n| format!("{}", n * multiplier)));
/// assert_eq!(format!("{command:?}"), r#""echo" "2" "4" "6""#.to_string());
/// ```
#[proc_macro]
pub fn cmd(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let command = parse_macro_input!(input as cmd::Command);
    quote! { #command }.into()
}

/// Mutates a command by combining static strings, conditional logic, loops,
/// pattern matching, and closures to dynamically build and format command-line arguments.
///
/// Arguments and environment variables are appended to the existing `Command`. The working
/// directory is replaced if set with this macro.
///
/// The `cmd_mut!` macro supports flexible syntax constructs such as:
/// - **Static arguments**: Basic string arguments.
/// - **Conditional inclusion**: Using `if` or `if let` to conditionally include arguments.
/// - **Iterative inclusion**: Using `for` loops to include multiple arguments from collections.
/// - **Pattern matching**: Using `match` expressions for dynamic argument selection.
/// - **Closures**: Dynamically generating arguments at runtime.
///
/// # Examples
///
/// ## Basic Usage
/// ```
/// # use comlexr_macro::{cmd, cmd_mut};
/// let mut command = cmd!("echo");
/// cmd_mut!(&mut command, "test");
/// assert_eq!(format!("{command:?}"), r#""echo" "test""#.to_string());
/// ```
///
/// ## Current Directory
/// ```
/// # use comlexr_macro::{cmd, cmd_mut};
/// let mut command = cmd!("echo");
/// cmd_mut!(
///     cd "~/";
///     &mut command,
///     "test",
/// );
///
/// assert_eq!(format!("{command:?}"), r#"cd "~/" && "echo" "test""#);
/// ```
///
/// ## Environment Vars
/// ```
/// # use comlexr_macro::{cmd, cmd_mut};
/// const NEW_VAR: &str = "NEW_VAR";
///
/// let mut command = cmd!("echo");
/// cmd_mut!(
///     env {
///         "TEST": "test",
///         NEW_VAR: "new_var"
///     };
///     &mut command,
///     "test",
/// );
///
/// assert_eq!(format!("{command:?}"), r#"NEW_VAR="new_var" TEST="test" "echo" "test""#);
/// ```
///
/// #### Conditional
/// You can have a default value set for an environment variable.
///
/// ```rust
/// # use comlexr_macro::{cmd, cmd_mut};
/// const NEW_VAR: &str = "NEW_VAR";
/// std::env::set_var("TEST", "realvalue");
///
/// let mut command = cmd!("echo");
/// cmd_mut!(
///     env {
///         "TEST":? "test",
///         NEW_VAR: "new_var"
///     };
///     &mut command,
///     "test",
/// );
///
/// assert_eq!(format!("{command:?}"), r#"NEW_VAR="new_var" "echo" "test""#);
/// ```
///
/// ### Current Directory and Environment Variable Order
/// ```
/// # use comlexr_macro::{cmd, cmd_mut};
/// let mut command = cmd!("echo");
/// cmd_mut!(
///     cd "~/";
///     env {
///         "TEST": "test",
///     };
///     &mut command,
///     "test",
/// );
///
/// assert_eq!(
///     format!("{command:?}"),
///     r#"cd "~/" && TEST="test" "echo" "test""#
/// );
///
/// ```
///
/// ## Conditional Arguments
/// ```
/// # use comlexr_macro::{cmd, cmd_mut};
/// let include_arg = true;
///
/// let mut command = cmd!("echo");
/// cmd_mut!(&mut command, "test", if include_arg => "optional_arg");
/// assert_eq!(format!("{command:?}"), r#""echo" "test" "optional_arg""#.to_string());
/// ```
///
/// ## Conditional Pattern Matching
/// ```
/// # use comlexr_macro::{cmd, cmd_mut};
/// let single_option = Some("single");
/// let multi_option: Option<&str> = None;
///
/// let mut command = cmd!("echo");
/// cmd_mut!(
///     &mut command,
///     "test",
///     if let Some(arg) = single_option => arg,
///     if let Some(arg) = multi_option => [
///         "multi",
///         arg,
///     ],
/// );
/// assert_eq!(format!("{command:?}"), r#""echo" "test" "single""#.to_string());
/// ```
///
/// ## Iterative Argument Inclusion
/// ```
/// # use comlexr_macro::{cmd, cmd_mut};
/// let args = &["arg1", "arg2"];
///
/// let mut command = cmd!("echo");
/// cmd_mut!(&mut command, for args);
/// assert_eq!(format!("{command:?}"), r#""echo" "arg1" "arg2""#.to_string());
/// ```
///
/// ## Iteration with `for in`
/// ```
/// # use comlexr_macro::{cmd, cmd_mut};
/// let single_iter = &["arg1", "arg2"];
/// let multi_iter = &["multi1", "multi2"];
///
/// let mut command = cmd!("echo");
/// cmd_mut!(
///     &mut command,
///     "test",
///     for arg in single_iter => arg,
///     for arg in multi_iter => [
///         "multi",
///         arg,
///     ],
/// );
/// assert_eq!(format!("{command:?}"), r#""echo" "test" "arg1" "arg2" "multi" "multi1" "multi" "multi2""#.to_string());
/// ```
///
/// ## Match Statements
/// ```
/// # use comlexr_macro::{cmd, cmd_mut};
/// enum TestArgs {
///     Arg1,
///     Arg2,
///     Arg3,
/// }
///
/// let match_arg = TestArgs::Arg2;
/// let mut command = cmd!("echo");
/// cmd_mut!(
///     &mut command,
///     "test",
///     match match_arg {
///         TestArgs::Arg1 => "arg1",
///         TestArgs::Arg2 => ["arg1", "arg2"],
///         TestArgs::Arg3 => ["arg1", "arg2", "arg3"],
///     }
/// );
/// assert_eq!(format!("{command:?}"), r#""echo" "test" "arg1" "arg2""#.to_string());
/// ```
///
/// ## Dynamic Closures
/// ```
/// # use comlexr_macro::{cmd, cmd_mut};
/// let numbers = vec![1, 2, 3];
/// let multiplier = 2;
///
/// let mut command = cmd!("echo");
/// cmd_mut!(&mut command, || numbers.into_iter().map(|n| format!("{}", n * multiplier)));
/// assert_eq!(format!("{command:?}"), r#""echo" "2" "4" "6""#.to_string());
/// ```
#[proc_macro]
pub fn cmd_mut(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let command = parse_macro_input!(input as cmd::CommandMut);
    quote! { #command }.into()
}

/// Chain the stdout of commands to stdin. Execution is lazy so commands aren't run until `status()` or `output()` is called.
///
/// ```ignore
/// use comlexr::{pipe, cmd};
///
/// let dir = tempfile::tempdir().unwrap();
/// let file = dir.path().join("out");
/// let mut pipe = pipe!(cmd!("echo", "test") | cmd!("sed", "s|e|oa|") | cmd!("tee", &file));
///
/// let output = pipe.output().unwrap();
/// assert!(output.status.success());
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "toast\n");
/// ```
///
/// Or pass data via stdin.
///
/// NOTE: Data must implement `AsRef<[u8]>`.
///
/// ```ignore
/// use comlexr::{pipe, cmd};
///
/// let mut pipe = pipe!(stdin = "test"; cmd!("sed", "s|e|oa|"));
///
/// let output = pipe.output().unwrap();
/// assert!(output.status.success());
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "toast");
/// ```
#[proc_macro]
pub fn pipe(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let pipe = parse_macro_input!(input as pipe::Pipe);
    quote! { #pipe }.into()
}