serde_derive/
dummy.rs

1use proc_macro2::{Ident, Span, TokenStream};
2
3use syn;
4use try;
5
6pub fn wrap_in_const(
7    serde_path: Option<&syn::Path>,
8    trait_: &str,
9    ty: &Ident,
10    code: TokenStream,
11) -> TokenStream {
12    let try_replacement = try::replacement();
13
14    let dummy_const = Ident::new(
15        &format!("_IMPL_{}_FOR_{}", trait_, unraw(ty)),
16        Span::call_site(),
17    );
18
19    let use_serde = match serde_path {
20        Some(path) => quote! {
21            use #path as _serde;
22        },
23        None => quote! {
24            #[allow(unknown_lints)]
25            #[cfg_attr(feature = "cargo-clippy", allow(useless_attribute))]
26            #[allow(rust_2018_idioms)]
27            extern crate serde as _serde;
28        },
29    };
30
31    quote! {
32        #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
33        const #dummy_const: () = {
34            #use_serde
35            #try_replacement
36            #code
37        };
38    }
39}
40
41#[allow(deprecated)]
42fn unraw(ident: &Ident) -> String {
43    // str::trim_start_matches was added in 1.30, trim_left_matches deprecated
44    // in 1.33. We currently support rustc back to 1.15 so we need to continue
45    // to use the deprecated one.
46    ident.to_string().trim_left_matches("r#").to_owned()
47}