bubbly-bub-test 0.3.1

Interactive mode extension crate to Command Line Arguments Parser (https://crates.io/crates/clap) (derive macros helper crate)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
extern crate proc_macro;

use methods::cli_field_type;
use proc_macro2::{Span, TokenStream};
use proc_macro_error::abort_call_site;
use quote::{quote, ToTokens};
use syn;

use crate::{LONG_VEC_MUTLIPLE_OPT, VERBATIM_DOC_COMMENT};

pub(crate) mod methods;

pub fn impl_interactive_clap(ast: &syn::DeriveInput) -> TokenStream {
    let name = &ast.ident;
    let cli_name_string = format!("Cli{}", &ast.ident);
    let cli_name = &syn::Ident::new(&cli_name_string, Span::call_site());
    match &ast.data {
        syn::Data::Struct(data_struct) => {
            let fields = data_struct.fields.clone();
            let mut ident_skip_field_vec: Vec<syn::Ident> = Vec::new();

            let cli_fields = fields
                .iter()
                .map(|field| {
                    let ident_field = field.ident.clone().expect("this field does not exist");
                    let ty = &field.ty;
                    let cli_ty = self::methods::cli_field_type::cli_field_type(ty);
                    let mut cli_field = quote! {
                        pub #ident_field: #cli_ty
                    };
                    if field.attrs.is_empty() {
                        return cli_field;
                    };
                    let mut clap_attr_vec: Vec<proc_macro2::TokenStream> = Vec::new();
                    let mut cfg_attr_vec: Vec<proc_macro2::TokenStream> = Vec::new();
                    let mut doc_attr_vec: Vec<proc_macro2::TokenStream> = Vec::new();
                    #[allow(clippy::unused_enumerate_index)]
                    for attr in &field.attrs {
                        dbg_cond!(attr.path.to_token_stream().into_iter().collect::<Vec<_>>());
                        if attr.path.is_ident("interactive_clap") || attr.path.is_ident("cfg") {
                            for (_index,  attr_token) in attr.tokens.clone().into_iter().enumerate() {
                                dbg_cond!(_index, &attr_token);
                                match attr_token {
                                    proc_macro2::TokenTree::Group(group) => {
                                        let group_string = group.stream().to_string();
                                        if group_string.contains("subcommand")
                                            || group_string.contains("value_enum")
                                            || group_string.contains("long")
                                            || (group_string == *"skip")
                                            || (group_string == *"flatten")
                                            || (group_string == VERBATIM_DOC_COMMENT)
                                        {
                                            if group_string != LONG_VEC_MUTLIPLE_OPT {
                                                clap_attr_vec.push(group.stream())
                                            }
                                        } else if group.stream().to_string() == *"named_arg" {
                                            let ident_subcommand =
                                                syn::Ident::new("subcommand", Span::call_site());
                                            clap_attr_vec.push(quote! {#ident_subcommand});
                                            let type_string = match ty {
                                                syn::Type::Path(type_path) => {
                                                    match type_path.path.segments.last() {
                                                        Some(path_segment) => {
                                                            path_segment.ident.to_string()
                                                        }
                                                        _ => String::new(),
                                                    }
                                                }
                                                _ => String::new(),
                                            };
                                            let enum_for_clap_named_arg = syn::Ident::new(
                                                &format!(
                                                    "ClapNamedArg{}For{}",
                                                    &type_string, &name
                                                ),
                                                Span::call_site(),
                                            );
                                            cli_field = quote! {
                                                pub #ident_field: Option<#enum_for_clap_named_arg>
                                            }
                                        };
                                        if group.stream().to_string().contains("feature") {
                                            cfg_attr_vec.push(attr.into_token_stream())
                                        };
                                        if group.stream().to_string().contains("subargs") {
                                            let ident_subargs =
                                                syn::Ident::new("flatten", Span::call_site());
                                            clap_attr_vec.push(quote! {#ident_subargs});
                                        };
                                        if group.stream().to_string() == *"skip" {
                                            ident_skip_field_vec.push(ident_field.clone());
                                            cli_field = quote!()
                                        };
                                        if group.stream().to_string() == LONG_VEC_MUTLIPLE_OPT {
                                            if !cli_field_type::starts_with_vec(ty) {
                                                abort_call_site!("`{}` attribute is only supposed to be used with `Vec` types", LONG_VEC_MUTLIPLE_OPT)
                                            }
                                            // implies `#[interactive_clap(long)]`
                                            clap_attr_vec.push(quote! { long });
                                            // type goes into output unchanged, otherwise it
                                            // prevents clap deriving correctly its `remove_many` thing  
                                            cli_field = quote! {
                                                pub #ident_field: #ty
                                            };
                                        }
                                    }
                                    _ => {
                                        abort_call_site!("Only option `TokenTree::Group` is needed")
                                    }
                                }
                            }
                        }
                        if attr.path.is_ident("doc") {
                            doc_attr_vec.push(attr.into_token_stream())
                        }
                    }
                    if cli_field.is_empty() {
                        return cli_field;
                    };
                    let cfg_attrs = cfg_attr_vec.iter();
                    if !clap_attr_vec.is_empty() {
                        let clap_attrs = clap_attr_vec.iter();
                        quote! {
                            #(#cfg_attrs)*
                            #(#doc_attr_vec)*
                            #[clap(#(#clap_attrs, )*)]
                            #cli_field
                        }
                    } else {
                        quote! {
                            #(#cfg_attrs)*
                            #(#doc_attr_vec)*
                            #cli_field
                        }
                    }
                })
                .filter(|token_stream| !token_stream.is_empty())
                .collect::<Vec<_>>();

            let for_cli_fields = fields
                .iter()
                .map(|field| for_cli_field(field, &ident_skip_field_vec))
                .filter(|token_stream| !token_stream.is_empty());

            let fn_from_cli_for_struct =
                self::methods::from_cli_for_struct::from_cli_for_struct(ast, &fields);

            let vec_fn_input_arg = self::methods::input_arg::vec_fn_input_arg(ast, &fields);

            let context_scope_fields = fields
                .iter()
                .map(context_scope_for_struct_field)
                .filter(|token_stream| !token_stream.is_empty())
                .collect::<Vec<_>>();
            let context_scope_for_struct = context_scope_for_struct(name, context_scope_fields);

            let clap_enum_for_named_arg = fields.iter().find_map(|field| {
                let ident_field = &field.clone().ident.expect("this field does not exist");
                let variant_name_string = crate::helpers::snake_case_to_camel_case::snake_case_to_camel_case(ident_field.to_string());
                let variant_name = &syn::Ident::new(&variant_name_string, Span::call_site());
                let attr_doc_vec: Vec<_> = field.attrs.iter()
                    .filter(|attr| attr.path.is_ident("doc"))
                    .map(|attr| attr.into_token_stream())
                    .collect();
                field.attrs.iter()
                    .filter(|attr| attr.path.is_ident("interactive_clap"))
                    .flat_map(|attr| attr.tokens.clone())
                    .filter(|attr_token| {
                        match attr_token {
                            proc_macro2::TokenTree::Group(group) => group.stream().to_string() == *"named_arg",
                            _ => abort_call_site!("Only option `TokenTree::Group` is needed")
                        }
                    })
                    .map(|_| {
                        let ty = &field.ty;
                        let type_string = match ty {
                            syn::Type::Path(type_path) => {
                                match type_path.path.segments.last() {
                                    Some(path_segment) => path_segment.ident.to_string(),
                                    _ => String::new()
                                }
                            },
                            _ => String::new()
                        };
                        let enum_for_clap_named_arg = syn::Ident::new(&format!("ClapNamedArg{}For{}", &type_string, &name), Span::call_site());
                        quote! {
                            #[derive(Debug, Clone, clap::Parser, interactive_clap_derive::ToCliArgs)]
                            pub enum #enum_for_clap_named_arg {
                                #(#attr_doc_vec)*
                                #variant_name(<#ty as interactive_clap::ToCli>::CliVariant)
                            }

                            impl From<#ty> for #enum_for_clap_named_arg {
                                fn from(item: #ty) -> Self {
                                    Self::#variant_name(<#ty as interactive_clap::ToCli>::CliVariant::from(item))
                                }
                            }
                        }
                    })
                    .next()
                })
                .unwrap_or(quote!());

            quote! {
                #[derive(Debug, Default, Clone, clap::Parser, interactive_clap::ToCliArgs)]
                #[clap(author, version, about, long_about = None)]
                pub struct #cli_name {
                    #( #cli_fields, )*
                }

                impl interactive_clap::ToCli for #name {
                    type CliVariant = #cli_name;
                }

                #context_scope_for_struct

                #fn_from_cli_for_struct

                impl #name {
                    #(#vec_fn_input_arg)*

                    pub fn try_parse() -> Result<#cli_name, clap::Error> {
                        <#cli_name as clap::Parser>::try_parse()
                    }

                    pub fn parse() -> #cli_name {
                        <#cli_name as clap::Parser>::parse()
                    }

                    pub fn try_parse_from<I, T>(itr: I) -> Result<#cli_name, clap::Error>
                    where
                        I: ::std::iter::IntoIterator<Item = T>,
                        T: ::std::convert::Into<::std::ffi::OsString> + ::std::clone::Clone,
                    {
                        <#cli_name as clap::Parser>::try_parse_from(itr)
                    }
                }

                impl From<#name> for #cli_name {
                    fn from(args: #name) -> Self {
                        Self {
                            #( #for_cli_fields, )*
                        }
                    }
                }

                #clap_enum_for_named_arg
            }
        }
        syn::Data::Enum(syn::DataEnum { variants, .. }) => {
            let enum_variants = variants.iter().map(|variant| {
                let ident = &variant.ident;
                let mut attrs: Vec<proc_macro2::TokenStream> = Vec::new();
                if !&variant.attrs.is_empty() {
                    for attr in &variant.attrs {
                        if attr.path.is_ident("doc") {
                            attrs.push(attr.into_token_stream());
                        };
                        if attr.path.is_ident("cfg") {
                            for attr_token in attr.tokens.clone() {
                                match attr_token {
                                    proc_macro2::TokenTree::Group(group) => {
                                        if group.stream().to_string().contains("feature") {
                                            attrs.push(attr.into_token_stream());
                                        } else {
                                            continue;
                                        };
                                    }
                                    _ => {
                                        abort_call_site!("Only option `TokenTree::Group` is needed")
                                    }
                                }
                            }
                        };
                    }
                    match &variant.fields {
                        syn::Fields::Unnamed(fields) => {
                            let ty = &fields.unnamed[0].ty;
                            if attrs.is_empty() {
                                quote! {#ident(<#ty as interactive_clap::ToCli>::CliVariant)}
                            } else {
                                quote! {
                                    #(#attrs)*
                                    #ident(<#ty as interactive_clap::ToCli>::CliVariant)
                                }
                            }
                        }
                        syn::Fields::Unit => {
                            if attrs.is_empty() {
                                quote! {#ident}
                            } else {
                                quote! {
                                    #(#attrs)*
                                    #ident
                                }
                            }
                        }
                        _ => abort_call_site!(
                            "Only option `Fields::Unnamed` or `Fields::Unit` is needed"
                        ),
                    }
                } else {
                    match &variant.fields {
                        syn::Fields::Unnamed(fields) => {
                            let ty = &fields.unnamed[0].ty;
                            quote! { #ident(<#ty as interactive_clap::ToCli>::CliVariant) }
                        }
                        syn::Fields::Unit => {
                            quote! { #ident }
                        }
                        _ => abort_call_site!(
                            "Only option `Fields::Unnamed` or `Fields::Unit` is needed"
                        ),
                    }
                }
            });
            let for_cli_enum_variants = variants.iter().map(|variant| {
                let ident = &variant.ident;
                match &variant.fields {
                    syn::Fields::Unnamed(_) => {
                        quote! { #name::#ident(arg) => Self::#ident(arg.into()) }
                    }
                    syn::Fields::Unit => {
                        quote! { #name::#ident => Self::#ident }
                    }
                    _ => abort_call_site!(
                        "Only option `Fields::Unnamed` or `Fields::Unit` is needed"
                    ),
                }
            });

            let scope_for_enum = context_scope_for_enum(name);

            let fn_choose_variant = self::methods::choose_variant::fn_choose_variant(ast, variants);

            let fn_from_cli_for_enum =
                self::methods::from_cli_for_enum::from_cli_for_enum(ast, variants);

            quote! {
                #[derive(Debug, Clone, clap::Parser, interactive_clap::ToCliArgs)]
                pub enum #cli_name {
                    #( #enum_variants, )*
                }

                impl interactive_clap::ToCli for #name {
                    type CliVariant = #cli_name;
                }

                #scope_for_enum

                impl From<#name> for #cli_name {
                    fn from(command: #name) -> Self {
                        match command {
                            #( #for_cli_enum_variants, )*
                        }
                    }
                }

                #fn_from_cli_for_enum

                impl #name {
                    #fn_choose_variant

                    pub fn try_parse() -> Result<#cli_name, clap::Error> {
                        <#cli_name as clap::Parser>::try_parse()
                    }

                    pub fn parse() -> #cli_name {
                        <#cli_name as clap::Parser>::parse()
                    }

                    pub fn try_parse_from<I, T>(itr: I) -> Result<#cli_name, clap::Error>
                    where
                        I: ::std::iter::IntoIterator<Item = T>,
                        T: ::std::convert::Into<::std::ffi::OsString> + ::std::clone::Clone,
                    {
                        <#cli_name as clap::Parser>::try_parse_from(itr)
                    }
                }
            }
        }
        _ => abort_call_site!("`#[derive(InteractiveClap)]` only supports structs and enums"),
    }
}

fn context_scope_for_struct(
    name: &syn::Ident,
    context_scope_fields: Vec<proc_macro2::TokenStream>,
) -> proc_macro2::TokenStream {
    let interactive_clap_context_scope_for_struct = syn::Ident::new(
        &format!("InteractiveClapContextScopeFor{}", &name),
        Span::call_site(),
    );
    quote! {
        pub struct #interactive_clap_context_scope_for_struct {
            #(#context_scope_fields,)*
        }
        impl interactive_clap::ToInteractiveClapContextScope for #name {
            type InteractiveClapContextScope = #interactive_clap_context_scope_for_struct;
        }
    }
}

fn context_scope_for_struct_field(field: &syn::Field) -> proc_macro2::TokenStream {
    let ident_field = &field.ident.clone().expect("this field does not exist");
    let ty = &field.ty;
    if !self::methods::fields_with_subcommand::is_field_with_subcommand(field)
        && !self::methods::fields_with_subargs::is_field_with_subargs(field)
    {
        quote! {
            pub #ident_field: #ty
        }
    } else {
        quote!()
    }
}

fn context_scope_for_enum(name: &syn::Ident) -> proc_macro2::TokenStream {
    let interactive_clap_context_scope_for_enum = syn::Ident::new(
        &format!("InteractiveClapContextScopeFor{}", &name),
        Span::call_site(),
    );
    let enum_discriminants = syn::Ident::new(&format!("{}Discriminants", &name), Span::call_site());
    quote! {
        pub type #interactive_clap_context_scope_for_enum = #enum_discriminants;
        impl interactive_clap::ToInteractiveClapContextScope for #name {
                    type InteractiveClapContextScope = #interactive_clap_context_scope_for_enum;
                }
    }
}

fn for_cli_field(
    field: &syn::Field,
    ident_skip_field_vec: &[syn::Ident],
) -> proc_macro2::TokenStream {
    let ident_field = &field.clone().ident.expect("this field does not exist");
    if ident_skip_field_vec.contains(ident_field) {
        quote!()
    } else {
        let ty = &field.ty;
        if field.attrs.iter().any(|attr|
            attr.path.is_ident("interactive_clap") &&
            attr.tokens.clone().into_iter().any(
                |attr_token|
                matches!(
                    attr_token,
                    proc_macro2::TokenTree::Group(group) if group.stream().to_string() == LONG_VEC_MUTLIPLE_OPT
                )
            )
        ) {
            return quote! {
                #ident_field: args.#ident_field.into()
            };
        }

        match &ty {
            syn::Type::Path(type_path) => match type_path.path.segments.first() {
                Some(path_segment) => {
                    if path_segment.ident == "Option" || path_segment.ident == "bool" {
                        quote! {
                            #ident_field: args.#ident_field.into()
                        }
                    } else {
                        quote! {
                            #ident_field: Some(args.#ident_field.into())
                        }
                    }
                }
                _ => abort_call_site!("Only option `PathSegment` is needed"),
            },
            _ => abort_call_site!("Only option `Type::Path` is needed"),
        }
    }
}