aboutsummaryrefslogtreecommitdiff
path: root/macros/src/explain.rs
diff options
context:
space:
mode:
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..41dc5d4
--- /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(|attr| match attr.parse_meta().ok() {
11 Some(Meta::NameValue(MetaNameValue {
12 path,
13 lit: Lit::Str(str_lit),
14 ..
15 })) if path.is_ident("doc") => Some(str_lit.value()),
16 _ => None,
17 })
18 .map(|s| s.strip_prefix(' ').unwrap_or(&s).to_owned())
19 .collect::<Vec<_>>()
20 .join("\n");
21 quote! {
22 impl crate::Explain for #struct_name {
23 fn explanation(&self) -> &'static str {
24 #explain
25 }
26 }
27 }
28}