Lints and suggestions for the Nix programming language
1use proc_macro2::TokenStream as TokenStream2;
2use quote::quote;
3use syn::{Expr, ExprLit, 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.meta {
11 Meta::NameValue(MetaNameValue {
12 path,
13 value:
14 Expr::Lit(ExprLit {
15 lit: Lit::Str(str_lit),
16 ..
17 }),
18 ..
19 }) if path.is_ident("doc") => Some(str_lit.value()),
20 _ => None,
21 })
22 .map(|s| s.strip_prefix(' ').unwrap_or(&s).to_owned())
23 .collect::<Vec<_>>()
24 .join("\n");
25 quote! {
26 impl crate::Explain for #struct_name {
27 fn explanation(&self) -> &'static str {
28 #explain
29 }
30 }
31 }
32}