aboutsummaryrefslogtreecommitdiff
path: root/macros/src/explain.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-10-31 09:05:26 +0000
committerAkshay <[email protected]>2021-10-31 09:05:26 +0000
commitd085cad4370ab9759e616a7a4513f78cfe0b21be (patch)
tree433209a18ba17e7033becfd1eb2b87a5d5ec9879 /macros/src/explain.rs
parent246c69f8cfc74cf4c56fdaceaeb0562ed1f3dad5 (diff)
rework macros crate, add attr parser for explainations
Diffstat (limited to 'macros/src/explain.rs')
-rw-r--r--macros/src/explain.rs28
1 files changed, 28 insertions, 0 deletions
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 @@
1use proc_macro2::TokenStream as TokenStream2;
2use quote::quote;
3use syn::{ItemStruct, Lit, Meta, MetaNameValue};
4
5pub fn generate_explain_impl(struct_item: &ItemStruct) -> TokenStream2 {
6 let struct_name = &struct_item.ident;
7 let explain = struct_item
8 .attrs
9 .iter()
10 .filter_map(|a| a.parse_meta().ok())
11 .filter_map(|meta| match meta {
12 Meta::NameValue(MetaNameValue { path, lit, .. }) if path.is_ident("doc") => Some(lit),
13 _ => None,
14 })
15 .filter_map(|lit| match lit {
16 Lit::Str(str_lit) => Some(str_lit.value()),
17 _ => None,
18 })
19 .collect::<Vec<_>>()
20 .join("\n");
21 quote! {
22 impl crate::Explain for #struct_name {
23 fn explaination(&self) -> &'static str {
24 #explain
25 }
26 }
27 }
28}