Device macro to generate stateful and publish variants of device specific poll inputs.

This commit is contained in:
Zachary Sunforge
2023-06-18 20:49:30 -07:00
parent 07ae111e15
commit ce72a8a5be
6 changed files with 101 additions and 4 deletions

View File

@ -0,0 +1,46 @@
use proc_macro::TokenStream;
use std::ops::Deref;
use quote::quote;
use syn::{parse_macro_input, DeriveInput, Data, Ident};
// Struct name: Ads1256PollStateful, Ads1256PollPublish, Ads1256PollStatePub
#[proc_macro_derive(PollVariants)]
pub fn transducer_macro(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let DeriveInput {
attrs,
vis,
ident,
generics,
data,
} = &input;
// Check that item the macro was used on is valid
match data {
Data::Struct(struct_data) => struct_data,
Data::Enum(_) => panic!("Stateful struct cannot be derived from an enum."),
Data::Union(_) => panic!("Stateful struct cannot be derived from a union.")
};
// Check if generics contains T
// If it does make sure it is T: Copy
// If it isn't Copy, panic
// If it doesn't contain T, add it for use in the type that will be generated
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let stateful_name = format!("{ident}Stateful");
let stateful_ident = Ident::new(stateful_name.deref(), ident.span());
eprintln!("input: {input:#?}");
eprintln!("impl_generics: {impl_generics:#?}");
let expanded = quote! {
#vis struct #stateful_ident #generics {
pub poll: #ident #ty_generics,
pub state: physical::transducer::State<T>,
}
};
TokenStream::from(expanded)
}