xref: /third_party/rust/crates/syn/tests/macros/mod.rs (revision fad3a1d3)
1#![allow(unused_macros, unused_macro_rules)]
2
3#[path = "../debug/mod.rs"]
4pub mod debug;
5
6use std::str::FromStr;
7use syn::parse::Result;
8
9macro_rules! errorf {
10    ($($tt:tt)*) => {{
11        use ::std::io::Write;
12        let stderr = ::std::io::stderr();
13        write!(stderr.lock(), $($tt)*).unwrap();
14    }};
15}
16
17macro_rules! punctuated {
18    ($($e:expr,)+) => {{
19        let mut seq = ::syn::punctuated::Punctuated::new();
20        $(
21            seq.push($e);
22        )+
23        seq
24    }};
25
26    ($($e:expr),+) => {
27        punctuated!($($e,)+)
28    };
29}
30
31macro_rules! snapshot {
32    ($($args:tt)*) => {
33        snapshot_impl!(() $($args)*)
34    };
35}
36
37macro_rules! snapshot_impl {
38    (($expr:ident) as $t:ty, @$snapshot:literal) => {
39        let tokens = crate::macros::TryIntoTokens::try_into_tokens($expr).unwrap();
40        let $expr: $t = syn::parse_quote!(#tokens);
41        let debug = crate::macros::debug::Lite(&$expr);
42        if !cfg!(miri) {
43            #[allow(clippy::needless_raw_string_hashes)] // https://github.com/mitsuhiko/insta/issues/389
44            {
45                insta::assert_debug_snapshot!(debug, @$snapshot);
46            }
47        }
48    };
49    (($($expr:tt)*) as $t:ty, @$snapshot:literal) => {{
50        let tokens = crate::macros::TryIntoTokens::try_into_tokens($($expr)*).unwrap();
51        let syntax_tree: $t = syn::parse_quote!(#tokens);
52        let debug = crate::macros::debug::Lite(&syntax_tree);
53        if !cfg!(miri) {
54            #[allow(clippy::needless_raw_string_hashes)]
55            {
56                insta::assert_debug_snapshot!(debug, @$snapshot);
57            }
58        }
59        syntax_tree
60    }};
61    (($($expr:tt)*) , @$snapshot:literal) => {{
62        let syntax_tree = $($expr)*;
63        let debug = crate::macros::debug::Lite(&syntax_tree);
64        if !cfg!(miri) {
65            #[allow(clippy::needless_raw_string_hashes)]
66            {
67                insta::assert_debug_snapshot!(debug, @$snapshot);
68            }
69        }
70        syntax_tree
71    }};
72    (($($expr:tt)*) $next:tt $($rest:tt)*) => {
73        snapshot_impl!(($($expr)* $next) $($rest)*)
74    };
75}
76
77pub trait TryIntoTokens {
78    fn try_into_tokens(self) -> Result<proc_macro2::TokenStream>;
79}
80
81impl<'a> TryIntoTokens for &'a str {
82    fn try_into_tokens(self) -> Result<proc_macro2::TokenStream> {
83        let tokens = proc_macro2::TokenStream::from_str(self)?;
84        Ok(tokens)
85    }
86}
87
88impl TryIntoTokens for proc_macro2::TokenStream {
89    fn try_into_tokens(self) -> Result<proc_macro2::TokenStream> {
90        Ok(self)
91    }
92}
93