Debian Codegraph @

debian/rust-vendor/defmt-macros/src/derives/format/codegen.rs

raw
1 use proc_macro2::TokenStream as TokenStream2;
2 use quote::{quote, ToTokens};
3 use syn::{
4 parse_quote, punctuated::Punctuated, DataStruct, Ident, ImplGenerics, Token, TypeGenerics,
5 WhereClause, WherePredicate,
6 };
7
8 pub(crate) use enum_data::encode as encode_enum_data;
9
10 use crate::construct;
11
12 mod enum_data;
13 mod fields;
14
15 pub(crate) struct EncodeData {
16 pub(crate) format_tag: TokenStream2,
17 pub(crate) stmts: Vec<TokenStream2>,
18 pub(crate) where_predicates: Vec<WherePredicate>,
19 }
20
21 pub(crate) fn encode_struct_data(
22 ident: &Ident,
23 data: &DataStruct,
24 defmt_path: &syn::Path,
25 ) -> syn::Result<EncodeData> {
26 let mut format_string = ident.to_string();
27 let mut stmts = vec![];
28 let mut field_patterns = vec![];
29
30 let (encode_fields_stmts, where_predicates) = fields::codegen(
31 &data.fields,
32 &mut format_string,
33 &mut field_patterns,
34 defmt_path,
35 )?;
36
37 stmts.push(quote!(match self {
38 Self { #(#field_patterns),* } => {
39 #(#encode_fields_stmts;)*
40 }
41 }));
42
43 let format_tag = construct::interned_string(&format_string, "derived", false, None, defmt_path);
44 Ok(EncodeData {
45 format_tag,
46 stmts,
47 where_predicates,
48 })
49 }
50
51 pub(crate) struct Generics<'a> {
52 pub(crate) impl_generics: ImplGenerics<'a>,
53 pub(crate) type_generics: TypeGenerics<'a>,
54 pub(crate) where_clause: WhereClause,
55 }
56
57 impl<'a> Generics<'a> {
58 pub(crate) fn codegen(
59 generics: &'a mut syn::Generics,
60 where_predicates: Vec<WherePredicate>,
61 ) -> Self {
62 let mut where_clause = generics.make_where_clause().clone();
63 let (impl_generics, type_generics, _) = generics.split_for_impl();
64
65 // Extend where-clause with `Format` bounds for all field types.
66 where_clause.predicates.extend(where_predicates);
67
68 Self {
69 impl_generics,
70 type_generics,
71 where_clause,
72 }
73 }
74 }
75
76 pub(crate) struct DefmtAttr {
77 pub(crate) transparent: bool,
78 pub(crate) defmt_path: syn::Path,
79 pub(crate) where_clause: Option<WhereClause>,
80 }
81
82 impl DefmtAttr {
83 pub(crate) fn from_attrs(attrs: &[syn::Attribute]) -> syn::Result<Self> {
84 let (transparent, defmt_path, bound) = attrs
85 .iter()
86 .filter(|attr| attr.path().is_ident("defmt"))
87 .try_fold(
88 (false, None, None),
89 |(mut transparent, mut defmt_path, mut bound), attr| -> syn::Result<_> {
90 let options = attr.meta.require_list()?;
91 options.parse_nested_meta(|meta| {
92 if meta.path.is_ident("transparent") {
93 transparent = true;
94 } else if meta.path.is_ident("crate") {
95 meta.input.parse::<syn::Token![=]>()?;
96 defmt_path = Some(meta.input.parse::<syn::Path>()?);
97 } else if meta.path.is_ident("bound") {
98 let content;
99 let _paren = ::syn::parenthesized!(content in meta.input);
100 bound =
101 Some(Punctuated::<WherePredicate, Token![,]>::parse_terminated(
102 &content,
103 )?);
104 } else {
105 let path = meta.path.to_token_stream().to_string().replace(' ', "");
106 return Err(
107 meta.error(format_args!("unknown defmt attribute `{path}`"))
108 );
109 }
110 Ok(())
111 })?;
112
113 Ok((transparent, defmt_path, bound))
114 },
115 )?;
116
117 let defmt_path = defmt_path.unwrap_or_else(|| parse_quote! { defmt });
118 let where_clause = bound.map(|bound| parse_quote! { where #bound });
119 Ok(Self {
120 transparent,
121 defmt_path,
122 where_clause,
123 })
124 }
125 }