Game stats that reset every frame, inspired by immediate mode GUI.
gamedev bevy stats
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Improved macro error handling.

+15 -14
+7 -9
immediate_stats_macros/src/bevy/butler.rs
··· 1 1 use darling::ast::NestedMeta; 2 2 use darling::{Error, FromMeta}; 3 - use proc_macro::{self, TokenStream}; 4 - use proc_macro2::Ident; 3 + use proc_macro2::{Ident, TokenStream}; 5 4 use quote::{ToTokens, format_ident, quote}; 6 - use syn::{DeriveInput, Expr, Meta, Path, parse_macro_input}; 5 + use syn::{DeriveInput, Expr, Meta, Path}; 7 6 8 7 // Todo Fix error handling and add documentation. 9 - pub fn register_systems(input: TokenStream) -> TokenStream { 10 - let input = parse_macro_input!(input as DeriveInput); 8 + pub fn register_systems(input: DeriveInput) -> darling::Result<TokenStream> { 11 9 let struct_name = &input.ident; 12 10 13 11 let mut butler_attributes = ButlerAttributes::new(struct_name); 14 12 15 13 for attr in input.attrs { 16 14 if attr.path().is_ident("add_component") { 17 - let plugin = PluginPath::from_meta(&attr.meta).unwrap(); 15 + let plugin = PluginPath::from_meta(&attr.meta)?; 18 16 butler_attributes.component_plugin = Some(plugin); 19 17 } else if attr.path().is_ident("add_resource") { 20 - let plugin = PluginPath::from_meta(&attr.meta).unwrap(); 18 + let plugin = PluginPath::from_meta(&attr.meta)?; 21 19 butler_attributes.resource_plugin = Some(plugin); 22 20 } 23 21 } 24 22 25 - butler_attributes.into_token_stream().into() 23 + Ok(butler_attributes.into_token_stream()) 26 24 } 27 25 28 26 pub struct ButlerAttributes<'a> { ··· 42 40 } 43 41 44 42 impl<'a> ToTokens for ButlerAttributes<'a> { 45 - fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { 43 + fn to_tokens(&self, tokens: &mut TokenStream) { 46 44 if let Some(plugin_path) = &self.component_plugin { 47 45 let ident = &self.ident; 48 46 let plugin = &plugin_path.0;
+8 -5
immediate_stats_macros/src/lib.rs
··· 1 1 #[cfg(feature = "bevy")] 2 2 mod bevy; 3 3 4 - use crate::bevy::butler::register_systems; 5 4 use proc_macro_error::{ 6 5 emit_call_site_error, emit_call_site_warning, emit_warning, proc_macro_error, 7 6 }; 8 7 use proc_macro2::{Span, TokenStream}; 9 8 use quote::{ToTokens, quote}; 10 - use syn::{Data, DataEnum, DataStruct, DeriveInput, Field, Ident, Index}; 9 + use syn::{Data, DataEnum, DataStruct, DeriveInput, Field, Ident, Index, parse_macro_input}; 11 10 12 11 #[proc_macro_derive( 13 12 StatContainer, ··· 15 14 )] 16 15 #[proc_macro_error] 17 16 pub fn stat_container_derive(item: proc_macro::TokenStream) -> proc_macro::TokenStream { 18 - let tree: DeriveInput = syn::parse(item.clone()).expect("TokenStream must be valid."); 17 + let tree: DeriveInput = parse_macro_input!(item as DeriveInput); 19 18 20 19 let struct_name = &tree.ident; 21 20 ··· 36 35 37 36 #[cfg(feature = "bevy_butler")] 38 37 { 39 - let systems: TokenStream = register_systems(item).into(); 40 - quote! { #result #systems }.into() 38 + let systems = bevy::butler::register_systems(tree); 39 + 40 + match systems { 41 + Ok(systems) => quote! { #result #systems }.into(), 42 + Err(e) => e.write_errors().into(), 43 + } 41 44 } 42 45 43 46 #[cfg(not(feature = "bevy_butler"))]