toml_const_macros 1.3.0

proc-macros for toml_const
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! Custom input syntax for proc-macro inputs

use std::fs;
use std::path::{Path, PathBuf};

use proc_macro2 as pm2;
use proc_macro2::{Delimiter, Group};
use quote::{quote, ToTokens, TokenStreamExt};
use syn::spanned::Spanned;
use syn::{braced, parse::Parse, punctuated::Punctuated, LitStr};
use syn::{Ident, Token};

// attributes to forward
const INSTANTIATION_ATTR_PATH: &str = "instance";
const DEFINITION_ATTR_PATH: &str = "define";

#[derive(Clone)]
pub struct MultipleMacroInput(pub Vec<MacroInput>);

/// Input to [toml_const!](crate::toml_const)
#[derive(Clone)]
pub struct MacroInput {
    pub attrs: Vec<syn::Attribute>,

    // pub destructure_datetime: bool,
    /// Whether the static variable is public
    pub is_pub: bool,

    /// `false` if static, `true` if const
    pub static_const: bool,

    /// Static item identifier
    pub item_ident: Ident,

    /// `final` marks if the input file can be substituted
    pub is_final: bool,

    /// Path to the template file, mandatory
    pub path: LitStr,

    /// Any optional paths to substitute over the first path
    pub sub_paths: Option<Vec<UsePath>>,
}

/// A litstring path, with an optional use override keyword
#[derive(Clone)]
pub struct UsePath {
    pub path: LitStr,
    /// Manual use override in macro input
    pub is_used: bool,
}

impl Parse for MultipleMacroInput {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let mut macro_inputs = Vec::new();
        while !input.is_empty() {
            let macro_input: MacroInput = input.parse()?;
            macro_inputs.push(macro_input);
        }

        Ok(Self(macro_inputs))
    }
}

impl Parse for MacroInput {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        // parse docstring and datetime attr
        let attrs = input.call(syn::Attribute::parse_outer).unwrap_or_default();

        let is_pub: bool = {
            let lookahead = input.lookahead1();
            match lookahead.peek(syn::Token![pub]) {
                true => {
                    let _: syn::Token![pub] = input.parse()?;
                    true
                }
                false => false,
            }
        };

        let static_const = {
            let lookahead = input.lookahead1();

            if lookahead.peek(syn::Token![const]) {
                let _: syn::Token![const] = input.parse()?;
                true
            } else if lookahead.peek(syn::Token![static]) {
                let _: syn::Token![static] = input.parse()?;
                false
            } else {
                return Err(syn::Error::new(
                    input.span(),
                    "expected `static` or `const`",
                ));
            }
        };

        let item_ident: syn::Ident = input.parse()?;
        let _: syn::Token![:] = input.parse()?;

        let is_final = {
            let lookahead = input.lookahead1();

            match lookahead.peek(syn::Token![final]) {
                true => {
                    let _: syn::Token![final] = input.parse()?;
                    true
                }
                false => false,
            }
        };

        let template: LitStr = input.parse()?;

        let lookahead = input.lookahead1();
        let sub_paths = match lookahead.peek(syn::Token![;]) {
            true => {
                let _: syn::Token![;] = input.parse()?;
                None
            }
            false => match lookahead.peek(syn::token::Brace) {
                true => {
                    let content;
                    braced!(content in input);

                    let lit_str_vec =
                        Punctuated::<UsePath, syn::token::Semi>::parse_terminated(&content)?;

                    let res = lit_str_vec.into_iter().collect::<Vec<_>>();
                    Some(res)
                }
                false => return Err(syn::Error::new(input.span(), "expected {} or ;")),
            },
        };

        match is_final && sub_paths.is_some() {
            true => Err(syn::Error::new(
                template.span(),
                "final inputs cannot accept substitutions",
            )),
            false => Ok(Self {
                attrs,
                is_pub,
                static_const,
                item_ident,
                is_final,
                path: template,
                sub_paths,
            }),
        }
    }
}

impl ToTokens for MacroInput {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        for attr in &self.attrs {
            attr.to_tokens(tokens);
        }

        if self.is_pub {
            quote! {pub}.to_tokens(tokens);
        }

        match self.static_const {
            true => quote! {const}.to_tokens(tokens),
            false => quote! {static}.to_tokens(tokens),
        }

        self.item_ident.to_tokens(tokens);
        quote! {:}.to_tokens(tokens);

        if self.is_final {
            quote! {final}.to_tokens(tokens);
        }

        self.path.to_tokens(tokens);

        match &self.sub_paths {
            Some(sub) => {
                let subs = sub.iter().collect::<Punctuated<_, syn::Token![;]>>();

                let subs = match subs.len() {
                    0 => quote! {#subs},
                    _ => quote! {#subs;},
                };

                tokens.append(Group::new(Delimiter::Brace, subs.to_token_stream()));
            }
            None => quote! {;}.to_tokens(tokens),
        }
    }
}

impl Parse for UsePath {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let is_used = {
            let lookahead = input.lookahead1();
            match lookahead.peek(syn::Token![use]) {
                true => {
                    let _: syn::Token![use] = input.parse()?;
                    true
                }
                false => false,
            }
        };

        let path: LitStr = input.parse()?;

        Ok(Self { path, is_used })
    }
}

impl ToTokens for UsePath {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        if self.is_used {
            quote! {use}.to_tokens(tokens);
        }

        self.path.to_tokens(tokens);
    }
}

impl MacroInput {
    /// Return one or more const definitions to an underscore expression (`_`).
    /// If the path does not point to a file, it will not be included.
    ///
    /// These are calls to [include_str!] containing absolute paths.
    pub fn to_const_defs(&self, base_path: &Path) -> pm2::TokenStream {
        let mut template_path = base_path.to_path_buf();
        template_path.push(PathBuf::from(&self.path.value()));
        let template_path = pathbuf_to_str(&template_path);

        let mut const_defs = vec![quote! {const _: &'static str = include_str!(#template_path);}];

        if let Some(sp) = &self.sub_paths {
            let additions = sp.iter().map(|sub_path| {
                let mut abs_sub_path = base_path.to_path_buf();
                abs_sub_path.push(PathBuf::from(sub_path.path.value()));

                match abs_sub_path.exists() {
                    true => match abs_sub_path.is_file() {
                        true => {
                            let sub_path = pathbuf_to_str(&abs_sub_path);

                            quote! {
                                const _: &'static str = include_str!(#sub_path);
                            }
                        }
                        false => syn::Error::new(
                            sub_path.path.span(),
                            format!("path {} is not a file", abs_sub_path.display()),
                        )
                        .to_compile_error()
                        .to_token_stream(),
                    },
                    false => quote! {},
                }
            });

            const_defs.extend(additions);
        }

        const_defs.into_iter().collect::<pm2::TokenStream>()
    }

    /// Create a clone of `self` with all inner paths turned to absolute paths.
    ///
    /// The input base path must be absolute.
    pub fn to_abs_path(&self, base_path: &Path) -> Self {
        let mut abs_base_path = base_path.to_path_buf();

        abs_base_path.push(PathBuf::from(self.path.value()));
        let abs_base_path = LitStr::new(pathbuf_to_str(&abs_base_path), self.path.span());

        let sub_paths = self.sub_paths.clone();
        let sub_paths = sub_paths.map(|sp| {
            sp.into_iter()
                .map(|p| {
                    let mut abs_sub_path = base_path.to_path_buf();
                    abs_sub_path.push(PathBuf::from(p.path.value()));
                    let new_path = LitStr::new(pathbuf_to_str(&abs_sub_path), p.path.span());

                    UsePath {
                        path: new_path,
                        ..p
                    }
                })
                .collect::<Vec<_>>()
        });

        Self {
            path: abs_base_path,
            sub_paths,
            ..self.clone()
        }
    }

    /// With the the data in `self`, read in the template file and apply any substitutions
    pub fn generate_toml_table(&self) -> Result<toml::Table, pm2::TokenStream> {
        let template_toml = read_litstr_to_toml(&self.path)?.ok_or(
            syn::Error::new(
                self.path.span(),
                format!("unable to read template file: {}", self.path.value()),
            )
            .to_compile_error(),
        )?;

        let substitute_file = match &self.sub_paths {
            Some(paths) => {
                let mut res_sub = None;

                for sub_path in paths.iter() {
                    let sub_toml = read_litstr_to_toml(&sub_path.path)?;
                    let sub_toml = match sub_toml {
                        Some(st) => st,
                        None => continue,
                    };

                    match (sub_path.is_used, sub_toml.contains_key("use")) {
                        // macro-level override
                        (true, _) => {
                            res_sub = Some(sub_toml);
                            break;
                        }
                        // toml-level override
                        (false, true) => {
                            let use_val = sub_toml.get("use").expect("already checked");
                            if let toml::Value::Boolean(true) = use_val {
                                res_sub = Some(sub_toml);
                                break;
                            }
                        }
                        (false, false) => continue,
                    }
                }

                res_sub
            }
            None => None,
        };

        let merged = match substitute_file {
            Some(sf) => merge_tables(&template_toml, &sf),
            None => template_toml,
        };

        Ok(merged)
    }

    /// Inner method for [MacroInput::define_attr] and [MacroInput::instance_attr]
    fn strip_attr_path_and_transform(
        attr: &syn::Attribute,
        attr_path: &str,
    ) -> Result<Option<syn::Attribute>, syn::Error> {
        match &attr.meta {
            syn::Meta::Path(path) => Err(syn::Error::new(
                path.span(),
                format!("Nothing to forward. Use #[{}(...)]", attr_path),
            )),
            syn::Meta::List(meta_list) => {
                let inner_attr = syn::Attribute {
                    pound_token: Token![#](meta_list.span()),
                    style: syn::AttrStyle::Outer,
                    bracket_token: attr.bracket_token,
                    meta: {
                        let m: syn::Meta = syn::parse2(meta_list.tokens.clone())?;
                        //
                        m
                    },
                };

                Ok(Some(inner_attr))
            }
            syn::Meta::NameValue(meta_name_value) => Err(syn::Error::new(
                meta_name_value.span(),
                format!("Incorrect syntax, use #[{}(...)] instead", attr_path),
            )),
        }
    }

    fn define_attr(attr: &syn::Attribute) -> Result<Option<syn::Attribute>, syn::Error> {
        if attr.path().is_ident("derive") {
            Ok(Some(attr.clone()))
        } else if attr.path().is_ident(DEFINITION_ATTR_PATH) {
            Self::strip_attr_path_and_transform(attr, DEFINITION_ATTR_PATH)
        } else if !(attr.path().is_ident("doc") || attr.path().is_ident(INSTANTIATION_ATTR_PATH)) {
            Ok(Some(attr.clone()))
        } else {
            Ok(None)
        }
    }

    /// Returns Some if the attribute should be forwarded to the instantiation.
    ///
    /// Transforms the contents of the attribute, if applicable.
    fn instance_attr(attr: &syn::Attribute) -> Result<Option<syn::Attribute>, syn::Error> {
        if attr.path().is_ident("doc") {
            Ok(Some(attr.clone()))
        } else if attr.path().is_ident(INSTANTIATION_ATTR_PATH) {
            Self::strip_attr_path_and_transform(attr, INSTANTIATION_ATTR_PATH)
        } else if !(attr.path().is_ident("derive") || attr.path().is_ident(DEFINITION_ATTR_PATH)) {
            Ok(Some(attr.clone()))
        } else {
            Ok(None)
        }
    }

    /// Returns all attributes that should be forwarded to struct definitions.
    ///
    /// Transforms the contents of the attribute, if applicable.
    pub fn definition_attrs(&self) -> Result<Vec<syn::Attribute>, syn::Error> {
        self.attrs
            .iter()
            .filter_map(|a| match Self::define_attr(a) {
                Ok(Some(attr)) => Some(Ok(attr)),
                Ok(None) => None,
                Err(e) => Some(Err(e)),
            })
            .collect::<Result<Vec<_>, _>>()
    }

    /// Returns all attributes that should be forwarded to instantiation
    pub fn instantiation_attrs(&self) -> Result<Vec<syn::Attribute>, syn::Error> {
        self.attrs
            .iter()
            .filter_map(|a| match Self::instance_attr(a) {
                Ok(Some(attr)) => Some(Ok(attr)),
                Ok(None) => None,
                Err(e) => Some(Err(e)),
            })
            .collect::<Result<Vec<_>, _>>()
    }
}

/// Merge a toml template with a changes table. Changes will set/overwrite values in the template.
/// If both values are tables, merge recursively. If both are arrays, merge arrays element-wise.
/// Otherwise, the value from `changes` overrides the value from `template`.
fn merge_tables(template: &toml::Table, changes: &toml::Table) -> toml::Table {
    let mut merged_table = template.clone();

    for (key, value) in changes.iter() {
        match (merged_table.get(key), value) {
            (Some(toml::Value::Table(orig)), toml::Value::Table(chg)) => {
                merged_table.insert(key.clone(), toml::Value::Table(merge_tables(orig, chg)));
            }
            (Some(toml::Value::Array(orig)), toml::Value::Array(chg)) => {
                let mut merged_array = orig.clone();
                let min_len = merged_array.len().min(chg.len());
                // Overwrite elements in orig with those in chg, element-wise
                for i in 0..min_len {
                    merged_array[i] = match (&merged_array[i], &chg[i]) {
                        (toml::Value::Table(orig_t), toml::Value::Table(chg_t)) => {
                            toml::Value::Table(merge_tables(orig_t, chg_t))
                        }
                        (toml::Value::Array(orig_a), toml::Value::Array(chg_a)) => {
                            // Recursively merge arrays
                            let merged = merge_arrays(orig_a, chg_a);
                            toml::Value::Array(merged)
                        }
                        (_, chg_v) => chg_v.clone(),
                    };
                }
                // If chg is longer, append the extra elements
                if chg.len() > merged_array.len() {
                    merged_array.extend_from_slice(&chg[merged_array.len()..]);
                }
                merged_table.insert(key.clone(), toml::Value::Array(merged_array));
            }
            // Otherwise, just override
            _ => {
                merged_table.insert(key.clone(), value.clone());
            }
        }
    }

    merged_table
}

/// Merge two TOML arrays element-wise, recursively merging tables/arrays, otherwise replacing.
fn merge_arrays(orig: &[toml::Value], chg: &[toml::Value]) -> Vec<toml::Value> {
    let mut merged = orig.to_vec();
    let min_len = orig.len().min(chg.len());
    for i in 0..min_len {
        merged[i] = match (&orig[i], &chg[i]) {
            (toml::Value::Table(orig_t), toml::Value::Table(chg_t)) => {
                toml::Value::Table(merge_tables(orig_t, chg_t))
            }
            (toml::Value::Array(orig_a), toml::Value::Array(chg_a)) => {
                toml::Value::Array(merge_arrays(orig_a, chg_a))
            }
            (_, chg_v) => chg_v.clone(),
        };
    }
    if chg.len() > orig.len() {
        merged.extend_from_slice(&chg[orig.len()..]);
    }
    merged
}

fn pathbuf_to_str(input: &Path) -> &str {
    input.to_str().expect("failed to convert path to str")
}

/// Read in a litstr path to a toml file, return an error tokenstream if it fails.
fn read_litstr_to_toml(litstr: &LitStr) -> Result<Option<toml::Table>, pm2::TokenStream> {
    let path = PathBuf::from(litstr.value());

    // we allow paths that do not resolve to a file
    if !path.exists() {
        return Ok(None);
    }

    let file = match fs::read_to_string(path) {
        Ok(tf) => tf,
        Err(e) => {
            return Err(syn::Error::new(litstr.span(), e.to_string())
                .to_compile_error()
                .to_token_stream());
        }
    };

    let template_toml: toml::Table = match toml::from_str(&file) {
        Ok(tt) => tt,
        Err(e) => {
            return Err(syn::Error::new(litstr.span(), e.to_string())
                .to_compile_error()
                .to_token_stream());
        }
    };

    Ok(Some(template_toml))
}

#[cfg(test)]
mod tests {

    use super::*;

    /// Test parsing of some syntax, as well as checking that the re-generated token stream
    /// is the same as the input.
    macro_rules! test_parse {
        ($data_type: ident: $test_fn: ident {$($tokens: tt)*}) => {
            #[test]
            fn $test_fn() {
                let tokens = quote::quote! {
                    $($tokens)*
                };
                let input: $data_type = syn::parse2(tokens.clone()).expect("failed to parse input from tokenstream");

                let output = input.to_token_stream();
                assert_eq!(tokens.to_string(), output.to_string(), "generated tokenstream and original tokenstream do not match");
            }
        };
    }

    test_parse!(MacroInput: test_parse_template_new {
        const X: "some_file_path.toml";
    });

    test_parse!(MacroInput: test_parse_template_empty_brace {
        const X: "some_file_path.toml" {}
    });

    test_parse!(MacroInput: test_parse_template_and_subs {
        pub const X: "some_file_path.toml" {
            "some_sub_file_path.toml";
            "some_other_sub_file_path.toml";
        }
    });

    test_parse!(MacroInput: test_parse_public_static {
        pub static X: "some_file_path.toml" {
            "some_sub_file_path.toml";
            "some_other_sub_file_path.toml";
        }
    });

    test_parse!(MacroInput: test_parse_template_use_subs {
        pub const X: "some_file_path.toml" {
            use "some_sub_file_path.toml";
            "some_other_sub_file_path.toml";
        }
    });

    test_parse!(MacroInput: test_parse_template_final {
        pub const X: final "some_file_path.toml";
    });

    test_parse!(MacroInput: test_parse_template_with_attributes {
        /// Docstring = #[doc = "Docstring"]
        /// Another docstring line
        pub const X: final "some_file_path.toml";
    });

    test_parse!(UsePath: test_parse_use_path_used {
        use "some_file_path.toml"
    });

    test_parse!(UsePath: test_parse_use_path_unused {
        "some_file_path.toml"
    });

    /// Outer attributes only
    ///
    /// Used for macro doctest below
    struct Attr(syn::Attribute);

    impl Parse for Attr {
        fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
            syn::Attribute::parse_outer(input).map(|res| Attr(res.into_iter().next().unwrap()))
        }
    }

    /// Test attribute forwarding and transformation
    #[test]
    fn test_forward_attributes() {
        macro_rules! test_forward {
            ($($fn_path:ident)::+ (#[$attr: meta]) = $result: pat, $err: literal) => {
                test_forward! {
                    $($fn_path)::+ (#[$attr] => #[$attr]) = $result, $err
                }
            };

            ($($fn_path:ident)::+ (#[$attr: meta] => #[$out_attr: meta]) = $result: pat, $err: literal) => {
                let tokenstream = quote! {#[$attr]};
                let attr: Result<Attr, _> = syn::parse2(tokenstream);

                let res = attr.map(|a| {
                    $($fn_path)::+(&a.0).expect("processing should not fail")
                });

                assert!(matches!(res, $result), $err);

                if let Ok(Some(inner_attr)) = res {
                    let expected_out_attr: Attr = syn::parse2(quote! {#[ $out_attr ]}).expect("failed to parse out attribute");
                    assert_eq!(
                        inner_attr.to_token_stream().to_string(),
                        expected_out_attr.0.to_token_stream().to_string()
                    );
                }

            };
        }

        // defines
        test_forward! {MacroInput::define_attr(#[derive(Clone, Debug)]) = Ok(Some(_)), "derives are forwarded"}
        test_forward! {MacroInput::define_attr(#[doc = "Docstring"]) = Ok(None), "doc attrs not forwarded"}
        test_forward! {
            MacroInput::define_attr(
                #[define(some_definition_attr)] => #[some_definition_attr]
            ) = Ok(Some(_)), "defines are forwarded"
        }
        test_forward! {
            MacroInput::define_attr(
                #[define(allow(unused))] => #[allow(unused)]
            ) = Ok(Some(_)), "defines are forwarded"
        }
        test_forward! {MacroInput::define_attr(#[instance(some_instance_attr)]) = Ok(None), "instance not forwarded"}
        test_forward! {MacroInput::define_attr(#[rustfmt::skip]) = Ok(Some(_)), "non matching attr paths are all forwarded"};

        // instances
        test_forward! {MacroInput::instance_attr(#[derive(Clone, Debug)]) = Ok(None), "derives are not forwarded"}
        test_forward! {MacroInput::instance_attr(#[doc = "Docstring"]) = Ok(Some(_)), "doc attrs are forwarded"}
        test_forward! {
            MacroInput::instance_attr(
                #[instance(some_instance_attr)] => #[some_instance_attr]
            ) = Ok(Some(_)), "instances are forwarded"
        }
        test_forward! {
            MacroInput::instance_attr(
                #[instance(allow(unused))] => #[allow(unused)]
            ) = Ok(Some(_)), "instances are forwarded"
        }
        test_forward! {MacroInput::instance_attr(#[define(some_define_attr)]) = Ok(None), "defines are not forwarded"};
        test_forward! {MacroInput::instance_attr(#[rustfmt::skip]) = Ok(Some(_)), "non matching attr paths are all forwarded"};
    }

    /// Test attribute detection and forwarding
    #[test]
    fn test_forward_attributes_debug() {
        // let attr1 = quote! {#[derive(Clone, Debug)]};
        // let attr2 = quote! {#[doc = "Docstring"]};
        // let attr3 = quote! {#[define(some_definition_attr)]};
        let attr4 = quote! {#[define(allow(unused))]};
        // let attr5 = quote! {#[instance(some_instance_attr)]};
        // let attr6 = quote! {#[rustfmt::skip]};

        // let attr1: Attr = syn::parse2(attr1).expect("failed to parse attr1");
        // let attr2: Attr = syn::parse2(attr2).expect("failed to parse attr2");
        // let attr3: Attr = syn::parse2(attr3).expect("failed to parse attr3");
        let attr4: Attr = syn::parse2(attr4).expect("failed to parse attr4");
        // let attr5: Attr = syn::parse2(attr5).expect("failed to parse attr5");
        // let attr6: Attr = syn::parse2(attr6).expect("failed to parse attr6");

        // println!("attr1: {:#?}", attr1.0.to_token_stream().to_string());
        // println!("attr2: {:#?}", attr2.0.to_token_stream().to_string());
        // println!("attr3: {:#?}", attr3.0.to_token_stream().to_string());
        // println!("attr4: {:#?}", attr4.0.to_token_stream().to_string());
        // println!("attr5: {:#?}", attr5.0.to_token_stream().to_string());
        // println!("attr6: {:#?}", attr6.0.to_token_stream().to_string());

        fn show_input_output(attr: syn::Attribute) {
            println!("{}", attr.to_token_stream());
            let res = MacroInput::define_attr(&attr);
            if let Ok(Some(inner)) = res {
                println!("{}", inner.to_token_stream());
            }
        }

        show_input_output(attr4.0);
    }

    #[test]
    fn test_forward_testing() {

        // MacroInput::define_attr;
    }
}