From: Ian Jackson Date: Sat, 24 Jul 2021 00:55:25 +0000 (+0100) Subject: macros wip X-Git-Tag: hippotat/1.0.0~500 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=69f2efec8cc5d334d2aa7a027b5b4559596c5774;p=hippotat.git macros wip Signed-off-by: Ian Jackson --- diff --git a/Cargo.lock b/Cargo.lock index 7f9a853..247b02f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -215,6 +215,7 @@ dependencies = [ name = "hippotat-macros" version = "0.0.0" dependencies = [ + "itertools", "proc-macro2", "quote", "syn", diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 391ac2a..e71c494 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" description="Asinine HTTP-over-IP, proc-macros" [dependencies] +itertools = "0.10" syn = "1" proc-macro2 = "1" quote = "1" diff --git a/macros/macros.rs b/macros/macros.rs index 89c165b..6576c27 100644 --- a/macros/macros.rs +++ b/macros/macros.rs @@ -2,3 +2,30 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // There is NO WARRANTY. +use syn::{parse_macro_input, Data, DataStruct, DeriveInput}; +use quote::{quote, quote_spanned}; + +use itertools::Itertools; + +#[proc_macro_derive(ResolveConfig)] +pub fn resolve(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + let input = parse_macro_input!(input as DeriveInput); + + let fields = match input.data { + Data::Struct(DataStruct { fields: syn::Fields::Named(ref f),.. }) => f, + _ => panic!(), + }; + + let target = &input.ident; + let names = fields.named.iter().map( + |f| f.ident.as_ref().unwrap() + ) + .collect_vec(); + + let output = quote! { + impl #target { + const FIELDS: &[&str] = &[ #( #names ),* ]; + } + }; + output.into() +}