referrerpolicy=no-referrer-when-downgrade

frame_support_procedural/no_bound/
debug.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use syn::spanned::Spanned;
19
20/// Derive Debug but do not bound any generics.
21pub fn derive_debug_no_bound(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
22	let input = syn::parse_macro_input!(input as syn::DeriveInput);
23
24	let input_ident = &input.ident;
25	let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
26
27	let impl_ = match input.data {
28		syn::Data::Struct(struct_) => match struct_.fields {
29			syn::Fields::Named(named) => {
30				let fields =
31					named.named.iter().map(|i| &i.ident).map(
32						|i| quote::quote_spanned!(i.span() => .field(stringify!(#i), &self.#i) ),
33					);
34
35				quote::quote!(
36					fmt.debug_struct(stringify!(#input_ident))
37						#( #fields )*
38						.finish()
39				)
40			},
41			syn::Fields::Unnamed(unnamed) => {
42				let fields = unnamed
43					.unnamed
44					.iter()
45					.enumerate()
46					.map(|(i, _)| syn::Index::from(i))
47					.map(|i| quote::quote_spanned!(i.span() => .field(&self.#i) ));
48
49				quote::quote!(
50					fmt.debug_tuple(stringify!(#input_ident))
51						#( #fields )*
52						.finish()
53				)
54			},
55			syn::Fields::Unit => quote::quote!(fmt.write_str(stringify!(#input_ident))),
56		},
57		syn::Data::Enum(enum_) => {
58			let variants = enum_.variants.iter().map(|variant| {
59				let ident = &variant.ident;
60				let full_variant_str = format!("{}::{}", input_ident, ident);
61				match &variant.fields {
62					syn::Fields::Named(named) => {
63						let captured = named.named.iter().map(|i| &i.ident);
64						let debugged = captured.clone().map(|i| {
65							quote::quote_spanned!(i.span() =>
66								.field(stringify!(#i), &#i)
67							)
68						});
69						quote::quote!(
70							Self::#ident { #( ref #captured, )* } => {
71								fmt.debug_struct(#full_variant_str)
72									#( #debugged )*
73									.finish()
74							}
75						)
76					},
77					syn::Fields::Unnamed(unnamed) => {
78						let captured = unnamed
79							.unnamed
80							.iter()
81							.enumerate()
82							.map(|(i, f)| syn::Ident::new(&format!("_{}", i), f.span()));
83						let debugged = captured
84							.clone()
85							.map(|i| quote::quote_spanned!(i.span() => .field(&#i)));
86						quote::quote!(
87							Self::#ident ( #( ref #captured, )* ) => {
88								fmt.debug_tuple(#full_variant_str)
89									#( #debugged )*
90									.finish()
91							}
92						)
93					},
94					syn::Fields::Unit => quote::quote!(
95						Self::#ident => fmt.write_str(#full_variant_str)
96					),
97				}
98			});
99
100			quote::quote!(match *self {
101				#( #variants, )*
102			})
103		},
104		syn::Data::Union(_) => {
105			let msg = "Union type not supported by `derive(DebugNoBound)`";
106			return syn::Error::new(input.span(), msg).to_compile_error().into()
107		},
108	};
109
110	quote::quote!(
111		const _: () = {
112			#[automatically_derived]
113			#[allow(deprecated)]
114			impl #impl_generics ::core::fmt::Debug for #input_ident #ty_generics #where_clause {
115				fn fmt(&self, fmt: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
116					#impl_
117				}
118			}
119		};
120	)
121	.into()
122}