I would like to write a derive macro that would create a function to return the constants defined on an enum or a struct as a vector of tuples containing the name of each constant and its value.
trait ConstantsDefinedOnStruct {
fn constants_defined_on_struct() -> Vec<(&str, usize)> // The name and value of each constant
}
#[derive(ConstantsDefinedOnStruct)]
struct StructWithConstants{}
impl StructWithConstants {
pub const ONE: usize = 1
}
// Hoping that StructWithConstants now implements ::constants_defined_on_struct() which returns vec![("ONE", 1)]
Elsewhere, to implement the macro:
#[proc_macro_derive(ConstantsDefinedOnStruct)]
pub fn constants_defined_on_struct(input: TokenStream) -> TokenStream {
println!("macro input: {:#?}", input); // Can't see the constants?
input
}
But here I get stuck. Where in the TokenStream would I find the constants implemented on the struct?
I'd be very grateful for any pointers.