derive_syn_parse/
error_macros.rs

1//! Macros for producing errors within the derive macro
2
3macro_rules! invalid_input_kind {
4    ($arm:expr) => {{
5        return syn::Error::new(
6            $arm.span(),
7            "`#[derive(Parse)]` is only available on structs",
8        )
9        .to_compile_error();
10    }};
11}
12
13// Handle a `syn::Result` inside of a function that returns `proc_macro::TokenStream` by turning it
14// into a compile error if it's an error
15macro_rules! handle_syn_result {
16    (
17        @default_impl_from($generics_intro:ident, $ident:ident, $generics_args:ident, $where_clause:ident),
18        $result:expr
19    ) => {{
20        let res: syn::Result<_> = $result;
21        match res {
22            Ok(value) => value,
23            Err(e) => {
24                let mut ts = quote! {
25                    impl #$generics_intro ::syn::parse::Parse for #$ident #$generics_args #$where_clause {
26                        fn parse(input: ::syn::parse::ParseStream) -> ::syn::Result<Self> {
27                            unimplemented!("failed to derive `Parse`")
28                        }
29                    }
30                };
31                ts.extend(e.to_compile_error());
32                return ts;
33            }
34        }
35    }};
36
37    ($result:expr) => {{
38        let res: syn::Result<_> = $result;
39        match res {
40            Err(e) => return e.to_compile_error().into(),
41            Ok(value) => value,
42        }
43    }};
44}