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 | |
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')
| -rw-r--r-- | rust_macro/Cargo.lock | 46 | ||||
| -rw-r--r-- | rust_macro/Cargo.toml | 11 | ||||
| -rw-r--r-- | rust_macro/src/lib.rs | 24 |
3 files changed, 81 insertions, 0 deletions
diff --git a/rust_macro/Cargo.lock b/rust_macro/Cargo.lock new file mode 100644 index 0000000..6c3146f --- /dev/null +++ b/rust_macro/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "passwordmaker_macros" +version = "1.0.0" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd" diff --git a/rust_macro/Cargo.toml b/rust_macro/Cargo.toml new file mode 100644 index 0000000..93291e2 --- /dev/null +++ b/rust_macro/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "passwordmaker_macros" +version = "1.0.0" + +[dependencies] +syn = "1.0" +quote = "1.0" + +[lib] +name = "passwordmaker_macros" +proc-macro = true
\ No newline at end of file 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() +} |
