1 | #![allow (unused_macros, unused_macro_rules)] |
2 | |
3 | #[path = "../debug/mod.rs" ] |
4 | pub mod debug; |
5 | |
6 | use syn::parse::{Parse, Result}; |
7 | |
8 | macro_rules! errorf { |
9 | ($($tt:tt)*) => {{ |
10 | use ::std::io::Write; |
11 | let stderr = ::std::io::stderr(); |
12 | write!(stderr.lock(), $($tt)*).unwrap(); |
13 | }}; |
14 | } |
15 | |
16 | macro_rules! punctuated { |
17 | ($($e:expr,)+) => {{ |
18 | let mut seq = ::syn::punctuated::Punctuated::new(); |
19 | $( |
20 | seq.push($e); |
21 | )+ |
22 | seq |
23 | }}; |
24 | |
25 | ($($e:expr),+) => { |
26 | punctuated!($($e,)+) |
27 | }; |
28 | } |
29 | |
30 | macro_rules! snapshot { |
31 | ($($args:tt)*) => { |
32 | snapshot_impl!(() $($args)*) |
33 | }; |
34 | } |
35 | |
36 | macro_rules! snapshot_impl { |
37 | (($expr:ident) as $t:ty, @$snapshot:literal) => { |
38 | let $expr = crate::macros::Tokens::parse::<$t>($expr).unwrap(); |
39 | let debug = crate::macros::debug::Lite(&$expr); |
40 | if !cfg!(miri) { |
41 | insta::assert_debug_snapshot!(debug, @$snapshot); |
42 | } |
43 | }; |
44 | (($($expr:tt)*) as $t:ty, @$snapshot:literal) => {{ |
45 | let syntax_tree = crate::macros::Tokens::parse::<$t>($($expr)*).unwrap(); |
46 | let debug = crate::macros::debug::Lite(&syntax_tree); |
47 | if !cfg!(miri) { |
48 | insta::assert_debug_snapshot!(debug, @$snapshot); |
49 | } |
50 | syntax_tree |
51 | }}; |
52 | (($($expr:tt)*) , @$snapshot:literal) => {{ |
53 | let syntax_tree = $($expr)*; |
54 | let debug = crate::macros::debug::Lite(&syntax_tree); |
55 | if !cfg!(miri) { |
56 | insta::assert_debug_snapshot!(debug, @$snapshot); |
57 | } |
58 | syntax_tree |
59 | }}; |
60 | (($($expr:tt)*) $next:tt $($rest:tt)*) => { |
61 | snapshot_impl!(($($expr)* $next) $($rest)*) |
62 | }; |
63 | } |
64 | |
65 | pub trait Tokens { |
66 | fn parse<T: Parse>(self) -> Result<T>; |
67 | } |
68 | |
69 | impl<'a> Tokens for &'a str { |
70 | fn parse<T: Parse>(self) -> Result<T> { |
71 | syn::parse_str(self) |
72 | } |
73 | } |
74 | |
75 | impl Tokens for proc_macro2::TokenStream { |
76 | fn parse<T: Parse>(self) -> Result<T> { |
77 | syn::parse2(self) |
78 | } |
79 | } |
80 | |