chiark / gitweb /
macros wip
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 24 Jul 2021 00:55:25 +0000 (01:55 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 24 Jul 2021 00:55:25 +0000 (01:55 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Cargo.lock
macros/Cargo.toml
macros/macros.rs

index 7f9a853199079e26a9a0f05ca307e9e97a8f0886..247b02fbd9f0ec195803e978a4aad6228ea970af 100644 (file)
@@ -215,6 +215,7 @@ dependencies = [
 name = "hippotat-macros"
 version = "0.0.0"
 dependencies = [
+ "itertools",
  "proc-macro2",
  "quote",
  "syn",
index 391ac2addaeb89464be43a14ebfec4a1294425c3..e71c494d5d313dc1616ae0d4f6bbd0bdf1d365e6 100644 (file)
@@ -9,6 +9,7 @@ edition = "2018"
 description="Asinine HTTP-over-IP, proc-macros"
 
 [dependencies]
+itertools = "0.10"
 syn = "1"
 proc-macro2 = "1"
 quote = "1"
index 89c165b5c95785149039e9f11070847af2548a29..6576c279c55729623cec6ee65146678f210806d9 100644 (file)
@@ -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()
+}