| 1 |
use codegen::DefmtAttr; |
| 2 |
use proc_macro::TokenStream; |
| 3 |
use proc_macro2::Span; |
| 4 |
use quote::quote; |
| 5 |
use syn::{ |
| 6 |
parse_macro_input, parse_quote, spanned::Spanned, Arm, Data, DeriveInput, Generics, Ident, |
| 7 |
WhereClause, |
| 8 |
}; |
| 9 |
|
| 10 |
mod codegen; |
| 11 |
|
| 12 |
pub(crate) fn expand(input: TokenStream) -> TokenStream { |
| 13 |
let DeriveInput { |
| 14 |
attrs, |
| 15 |
vis: _, |
| 16 |
ident, |
| 17 |
mut generics, |
| 18 |
data, |
| 19 |
} = parse_macro_input!(input as DeriveInput); |
| 20 |
|
| 21 |
let DefmtAttr { |
| 22 |
transparent, |
| 23 |
defmt_path, |
| 24 |
where_clause: custom_where_clause, |
| 25 |
} = match DefmtAttr::from_attrs(&attrs) { |
| 26 |
Ok(maybe_attr) => maybe_attr, |
| 27 |
Err(err) => return err.into_compile_error().into(), |
| 28 |
}; |
| 29 |
|
| 30 |
if transparent { |
| 31 |
return match expand_transparent(ident, data, generics, defmt_path, custom_where_clause) { |
| 32 |
Ok(attr) => attr, |
| 33 |
Err(err) => err.into_compile_error().into(), |
| 34 |
}; |
| 35 |
} |
| 36 |
|
| 37 |
let encode_data = match &data { |
| 38 |
Data::Enum(data) => codegen::encode_enum_data(&ident, data, &defmt_path), |
| 39 |
Data::Struct(data) => codegen::encode_struct_data(&ident, data, &defmt_path), |
| 40 |
Data::Union(_) => { |
| 41 |
return syn::Error::new( |
| 42 |
Span::call_site(), |
| 43 |
"`#[derive(Format)]` does not support unions", |
| 44 |
) |
| 45 |
.into_compile_error() |
| 46 |
.into() |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
let codegen::EncodeData { |
| 51 |
format_tag, |
| 52 |
stmts, |
| 53 |
where_predicates, |
| 54 |
} = match encode_data { |
| 55 |
Ok(data) => data, |
| 56 |
Err(e) => return e.into_compile_error().into(), |
| 57 |
}; |
| 58 |
|
| 59 |
let codegen::Generics { |
| 60 |
impl_generics, |
| 61 |
type_generics, |
| 62 |
where_clause, |
| 63 |
} = codegen::Generics::codegen(&mut generics, where_predicates); |
| 64 |
|
| 65 |
let where_clause = custom_where_clause.unwrap_or(where_clause); |
| 66 |
quote!( |
| 67 |
#[automatically_derived] |
| 68 |
impl #impl_generics #defmt_path::Format for #ident #type_generics #where_clause { |
| 69 |
fn format(&self, f: #defmt_path::Formatter) { |
| 70 |
use #defmt_path as defmt; |
| 71 |
#defmt_path::unreachable!() |
| 72 |
} |
| 73 |
|
| 74 |
fn _format_tag() -> #defmt_path::Str { |
| 75 |
#format_tag |
| 76 |
} |
| 77 |
|
| 78 |
fn _format_data(&self) { |
| 79 |
#(#stmts)* |
| 80 |
} |
| 81 |
} |
| 82 |
) |
| 83 |
.into() |
| 84 |
} |
| 85 |
|
| 86 |
pub(crate) fn expand_transparent( |
| 87 |
ident: Ident, |
| 88 |
data: Data, |
| 89 |
mut generics: Generics, |
| 90 |
defmt_path: syn::Path, |
| 91 |
custom_where_clause: Option<WhereClause>, |
| 92 |
) -> syn::Result<TokenStream> { |
| 93 |
let mut where_clause = generics.make_where_clause().clone(); |
| 94 |
let (impl_generics, ty_generics, ..) = generics.split_for_impl(); |
| 95 |
|
| 96 |
let mut member_types: Vec<syn::Type> = vec![]; |
| 97 |
let body = match data { |
| 98 |
Data::Enum(data) => { |
| 99 |
let mut match_arms = vec![]; |
| 100 |
for v in data.variants { |
| 101 |
let mut fields = v.fields.iter(); |
| 102 |
let field = fields.next(); |
| 103 |
let one_or_less = fields.next().is_none(); |
| 104 |
let Some(field) = field.filter(|_| one_or_less) else { |
| 105 |
return Err(syn::Error::new( |
| 106 |
v.fields.span(), |
| 107 |
format!( |
| 108 |
"Transparent format can only be applied \ |
| 109 |
when all variants have exactly one field (got {})", |
| 110 |
v.fields.len(), |
| 111 |
), |
| 112 |
)); |
| 113 |
}; |
| 114 |
|
| 115 |
member_types.push(field.ty.clone()); |
| 116 |
let field = field.ident.clone().map_or_else( |
| 117 |
|| { |
| 118 |
syn::Member::Unnamed(syn::Index { |
| 119 |
index: 0, |
| 120 |
span: Span::call_site(), |
| 121 |
}) |
| 122 |
}, |
| 123 |
syn::Member::Named, |
| 124 |
); |
| 125 |
let variant_name = &v.ident; |
| 126 |
let match_arm: Arm = parse_quote! { |
| 127 |
Self::#variant_name{ #field: inner } => inner.format(f) |
| 128 |
}; |
| 129 |
match_arms.push(match_arm) |
| 130 |
} |
| 131 |
quote! { |
| 132 |
match &self { |
| 133 |
#( #match_arms, )* |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
Data::Struct(data) => { |
| 138 |
if data.fields.len() > 1 { |
| 139 |
return Err(syn::Error::new( |
| 140 |
Span::call_site(), |
| 141 |
format!( |
| 142 |
"Transparent format can only be applied to structs with one field (got {})", |
| 143 |
data.fields.len() |
| 144 |
), |
| 145 |
)); |
| 146 |
} |
| 147 |
let mut fields = data.fields.iter(); |
| 148 |
let field = fields.next(); |
| 149 |
let Some(field) = field else { |
| 150 |
return Err(syn::Error::new( |
| 151 |
Span::call_site(), |
| 152 |
"Transparent format can only be applied to structs with one field (got 0)", |
| 153 |
)); |
| 154 |
}; |
| 155 |
|
| 156 |
member_types.push(field.ty.clone()); |
| 157 |
let field = field.ident.clone().map_or_else( |
| 158 |
|| { |
| 159 |
syn::Member::Unnamed(syn::Index { |
| 160 |
index: 0, |
| 161 |
span: Span::call_site(), |
| 162 |
}) |
| 163 |
}, |
| 164 |
syn::Member::Named, |
| 165 |
); |
| 166 |
quote! { |
| 167 |
self.#field.format(f); |
| 168 |
} |
| 169 |
} |
| 170 |
Data::Union(_) => { |
| 171 |
return Err(syn::Error::new( |
| 172 |
ident.span(), |
| 173 |
"`#[derive(Format)]` does not support unions", |
| 174 |
)) |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
let generic_bounds: Vec<syn::WherePredicate> = member_types |
| 179 |
.iter() |
| 180 |
.map(|ty| parse_quote! { #ty: #defmt_path::Format }) |
| 181 |
.collect(); |
| 182 |
where_clause.predicates.extend(generic_bounds); |
| 183 |
|
| 184 |
let where_clause = custom_where_clause.unwrap_or(where_clause); |
| 185 |
let quoted = quote! { |
| 186 |
impl #impl_generics #defmt_path::Format for #ident #ty_generics #where_clause { |
| 187 |
fn format(&self, f: #defmt_path::Formatter) { |
| 188 |
#body |
| 189 |
} |
| 190 |
} |
| 191 |
}; |
| 192 |
Ok(quoted.into()) |
| 193 |
} |