diff options
| author | Andreas Grois <andi@grois.info> | 2022-10-10 21:30:02 +0200 |
|---|---|---|
| committer | Andreas Grois <andi@grois.info> | 2022-10-10 21:37:15 +0200 |
| commit | e4ad766315879e1ff05bb111229f073f8f0ed68e (patch) | |
| tree | 4b043ff47c78b2c00c80c94ebda622c32c8b6d3d /rust_macro/src/lib.rs | |
PassFish: Initial Commit
Well, that's a lie. But nobody needs to see all the iterations I decided
to sweep under the rug.
That said, I think the repo is, while not clean, clean enough now, to
not be embarrassed by uploading it to github.
Diffstat (limited to 'rust_macro/src/lib.rs')
| -rw-r--r-- | rust_macro/src/lib.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/rust_macro/src/lib.rs b/rust_macro/src/lib.rs new file mode 100644 index 0000000..1d4a5ae --- /dev/null +++ b/rust_macro/src/lib.rs @@ -0,0 +1,24 @@ +/// This is a dumping ground for all proc-macros used by passfish. +/// There's a corresponding runtime module in the main crate. +extern crate syn; +extern crate quote; +extern crate proc_macro; + +use proc_macro::TokenStream; +use quote::quote; + +/// Adds a global constant name ENUMTYPE_ +/// Thanks, StackOverflow: https://stackoverflow.com/questions/41637978/how-to-get-the-number-of-elements-variants-in-an-enum-as-a-constant-value +#[proc_macro_derive(EnumVariantCount)] +pub fn derive_enum_variant_count(input: TokenStream) -> TokenStream { + let syn_item: syn::DeriveInput = syn::parse(input).unwrap(); + let len = match syn_item.data { + syn::Data::Enum(enum_item) => enum_item.variants.len(), + _ => panic!("EnumVariantCount only works on Enums"), + }; + let enum_name = syn_item.ident; + let result = quote! { + impl EnumVariantCount for #enum_name { fn variant_count() -> usize { #len } } + }; + result.into() +} |
