diff --git a/README.md b/README.md index 4d31d87..1cb564c 100644 --- a/README.md +++ b/README.md @@ -280,6 +280,12 @@ fn main() { } ``` +# Debugging + +To see the generated code, set the DEBUG_STRUM environment variable before compiling your code. +`DEBUG_STRUM=1` will dump all of the generated code for every type. `DEBUG_STRUM=YourType` will +only dump the code generated on a type named `YourType`. + # Name Strum is short for STRing enUM because it's a library for augmenting enums with additional diff --git a/strum/src/lib.rs b/strum/src/lib.rs index 2129a0e..43931b6 100644 --- a/strum/src/lib.rs +++ b/strum/src/lib.rs @@ -276,6 +276,12 @@ //! } //! ``` //! +//! # Debugging +//! +//! To see the generated code, set the DEBUG_STRUM environment variable before compiling your code. +//! `DEBUG_STRUM=1` will dump all of the generated code for every type. `DEBUG_STRUM=YourType` will +//! only dump the code generated on a type named YourType. +//! //! # Name //! //! Strum is short for STRing enUM because it's a library for augmenting enums with additional diff --git a/strum_macros/src/lib.rs b/strum_macros/src/lib.rs index 907399f..9aa2b04 100644 --- a/strum_macros/src/lib.rs +++ b/strum_macros/src/lib.rs @@ -14,6 +14,7 @@ extern crate proc_macro; use proc_macro::TokenStream; use syn::Attribute; +use std::env; #[proc_macro_derive(EnumString,attributes(strum))] pub fn from_string(input: TokenStream) -> TokenStream { @@ -21,6 +22,7 @@ pub fn from_string(input: TokenStream) -> TokenStream { let ast = syn::parse_derive_input(&s).unwrap(); let toks = from_string_inner(&ast); + debug_print_generated(&ast, &toks); toks.parse().unwrap() } @@ -30,6 +32,7 @@ pub fn enum_iter(input: TokenStream) -> TokenStream { let ast = syn::parse_derive_input(&s).unwrap(); let toks = enum_iter_inner(&ast); + debug_print_generated(&ast, &toks); toks.parse().unwrap() } @@ -39,9 +42,24 @@ pub fn enum_messages(input: TokenStream) -> TokenStream { let ast = syn::parse_derive_input(&s).unwrap(); let toks = enum_message_inner(&ast); + debug_print_generated(&ast, &toks); toks.parse().unwrap() } +fn debug_print_generated(ast: &syn::DeriveInput, toks: "e::Tokens) { + let ident = ast.ident.as_ref(); + let debug = env::var("STRUM_DEBUG"); + if let Ok(s) = debug { + if s == "1" { + println!("{}", toks); + } + + if s == ident { + println!("{}", toks); + } + } +} + fn extract_attrs<'a>(attrs: &'a [Attribute], attr: &str, prop: &str) -> Vec<&'a str> { attrs.iter() // Get all the attributes with our tag on them.