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
/*!
# Execute Command Macro Impl

See [`execute-command-macro`](https://crates.io/crates/execute-command-macro).
*/

extern crate execute_command_tokens;
extern crate proc_macro_hack;

#[macro_use]
extern crate syn;

#[macro_use]
extern crate quote;

extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro_hack::proc_macro_hack;
use syn::LitStr;

use execute_command_tokens::command_tokens;

#[proc_macro_hack]
pub fn command(input: TokenStream) -> TokenStream {
    let s = parse_macro_input!(input as LitStr).value();

    let tokens = command_tokens(s);

    let tokens_length = tokens.len();

    let command = match tokens_length {
        0 => {
            quote! {
                std::process::Command::new("")
            }
        }
        1 => {
            let program = &tokens[0];

            quote! {
                std::process::Command::new(#program)
            }
        }
        _ => {
            let program = &tokens[0];
            let args = &tokens[1..];

            quote! {
                {
                    let mut command = std::process::Command::new(#program);

                    command.args(&[#(#args,)*]);

                    command
                }
            }
        }
    };

    command.into()
}