From 0195710c9855a17d559af2f63c54cf89140405bb Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Tue, 18 Apr 2023 14:06:51 -0600 Subject: [PATCH] Add macros package --- ibihf-macros/Cargo.lock | 101 ++++++++++++++++++++++++++++++++++++++++ ibihf-macros/Cargo.toml | 15 ++++++ ibihf-macros/src/lib.rs | 32 +++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 ibihf-macros/Cargo.lock create mode 100644 ibihf-macros/Cargo.toml create mode 100644 ibihf-macros/src/lib.rs diff --git a/ibihf-macros/Cargo.lock b/ibihf-macros/Cargo.lock new file mode 100644 index 0000000..8ce9f3c --- /dev/null +++ b/ibihf-macros/Cargo.lock @@ -0,0 +1,101 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "darling" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "ibihf-macros" +version = "0.1.0" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "proc-macro2" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" diff --git a/ibihf-macros/Cargo.toml b/ibihf-macros/Cargo.toml new file mode 100644 index 0000000..d98dc33 --- /dev/null +++ b/ibihf-macros/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "ibihf-macros" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +darling = "0.14.4" +proc-macro2 = "1.0.56" +quote = "1.0.26" +syn = "1.0" + +[lib] +proc-macro = true diff --git a/ibihf-macros/src/lib.rs b/ibihf-macros/src/lib.rs new file mode 100644 index 0000000..12e85fa --- /dev/null +++ b/ibihf-macros/src/lib.rs @@ -0,0 +1,32 @@ +use darling::FromDeriveInput; +use proc_macro::{self, TokenStream}; +use quote::quote; +use syn::{parse_macro_input, DeriveInput}; + +#[derive(FromDeriveInput, Default)] +#[darling(default, attributes(urls))] +struct Opts { + url_key: String, + url_key_template: String, +} + +#[proc_macro_derive(TemplateUrl, attributes(urls))] +pub fn derive(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input); + let opts = Opts::from_derive_input(&input).expect("Wrong options"); + let DeriveInput { ident, .. } = input; + + let url_key = opts.url_key; + let url_key_template = opts.url_key_template; + let answer = quote! { + const URL_KEY: &'static str = #url_key; + const URL_KEY_TEMPLATE: &'static str = #url_key_template; + }; + + let output = quote! { + impl crate::traits::TemplateUrl for #ident<'_> { + #answer + } + }; + output.into() +}