//! Derive macros for tf-log-parser extern crate proc_macro; mod event; mod events; use crate::event::Event; use crate::events::Events; use proc_macro2::TokenStream; use quote::ToTokens; use std::fmt::Display; use syn::{parse_macro_input, DeriveInput, Error, Result}; /// Derive the `Event` trait for a struct #[proc_macro_derive(Event, attributes(event))] pub fn derive_event(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let expanded = derive_trait::(parse_macro_input!(input as DeriveInput)); proc_macro::TokenStream::from(expanded) } /// Derive the `Events` trait for a struct #[proc_macro_derive(Events, attributes())] pub fn derive_events(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let expanded = derive_trait::(parse_macro_input!(input as DeriveInput)); proc_macro::TokenStream::from(expanded) } /// Basic wrapper for error handling fn derive_trait(input: DeriveInput) -> TokenStream { derive_trait_inner::(input).unwrap_or_else(|err| err.into_compile_error()) } fn derive_trait_inner(input: DeriveInput) -> Result { let params = Trait::Params::parse(&input)?; Trait::derive(params) } trait Derivable { type Params: DeriveParams; fn derive(params: Self::Params) -> Result; } trait DeriveParams: Sized { fn parse(input: &DeriveInput) -> Result; } fn err(msg: U, span: T) -> Result { return Err(Error::new_spanned(&span, msg)); }