diff options
author | Akshay <[email protected]> | 2021-10-31 09:05:26 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-10-31 16:05:15 +0000 |
commit | e8c955da4cbb042e6f9b89307d143f5bfa6779fa (patch) | |
tree | 0ae4ec11fd3dc0f8b69bc0f32c08858ef23a9485 /macros/src/explain.rs | |
parent | 246c69f8cfc74cf4c56fdaceaeb0562ed1f3dad5 (diff) |
add `explain` subcommand and explanations to all lints
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..41dc5d4 --- /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(|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 | } | ||