1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//! A trivial Rust macro to derive the Display trait for any type with the Debug trait
//!
//! This is a very lightweight crate: check the README for more info



extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;

#[proc_macro_derive(Display)]
pub fn display_derive(input: TokenStream) -> TokenStream {

    let ast = syn::parse(input).expect("TokenStream could not be parsed");
    impl_display_derive(&ast)
}


fn impl_display_derive(ast: &syn::DeriveInput) -> TokenStream {
    let name = &ast.ident;
    let gen = quote! {

	    impl Display for #name {
			fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
				write!(f, "{:?}", self)
			}
		}
    };
    gen.into()
}