diff options
Diffstat (limited to 'macros/src/explain.rs')
-rw-r--r-- | macros/src/explain.rs | 28 |
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 @@ | |||
1 | use proc_macro2::TokenStream as TokenStream2; | ||
2 | use quote::quote; | ||
3 | use syn::{ItemStruct, Lit, Meta, MetaNameValue}; | ||
4 | |||
5 | pub 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 | } | ||