From d085cad4370ab9759e616a7a4513f78cfe0b21be Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 31 Oct 2021 14:35:26 +0530 Subject: rework macros crate, add attr parser for explainations --- macros/src/explain.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 macros/src/explain.rs (limited to 'macros/src/explain.rs') diff --git a/macros/src/explain.rs b/macros/src/explain.rs new file mode 100644 index 0000000..8b00740 --- /dev/null +++ b/macros/src/explain.rs @@ -0,0 +1,28 @@ +use proc_macro2::TokenStream as TokenStream2; +use quote::quote; +use syn::{ItemStruct, Lit, Meta, MetaNameValue}; + +pub fn generate_explain_impl(struct_item: &ItemStruct) -> TokenStream2 { + let struct_name = &struct_item.ident; + let explain = struct_item + .attrs + .iter() + .filter_map(|a| a.parse_meta().ok()) + .filter_map(|meta| match meta { + Meta::NameValue(MetaNameValue { path, lit, .. }) if path.is_ident("doc") => Some(lit), + _ => None, + }) + .filter_map(|lit| match lit { + Lit::Str(str_lit) => Some(str_lit.value()), + _ => None, + }) + .collect::>() + .join("\n"); + quote! { + impl crate::Explain for #struct_name { + fn explaination(&self) -> &'static str { + #explain + } + } + } +} -- cgit v1.2.3