From b8228a036016fa58cab4b00a2e62298df299d41f Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Sat, 27 Aug 2022 20:31:21 -0500 Subject: Azalea registry (#20) * make azalea-registry crate * add trait feature to az-block * registr * registry macro * impl Display for registry things * registries --- azalea-block/block-macros/src/lib.rs | 82 +++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 38 deletions(-) (limited to 'azalea-block/block-macros/src') diff --git a/azalea-block/block-macros/src/lib.rs b/azalea-block/block-macros/src/lib.rs index 907fd241..e6585600 100644 --- a/azalea-block/block-macros/src/lib.rs +++ b/azalea-block/block-macros/src/lib.rs @@ -426,41 +426,43 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { quote! { BlockState::#block_name_pascal_case } }; - let block_struct = quote! { - #[derive(Debug)] - pub struct #block_struct_name { - #block_struct_fields - } - - impl Block for #block_struct_name { - fn behavior(&self) -> BlockBehavior { - #block_behavior + if cfg!(feature = "trait") { + let block_struct = quote! { + #[derive(Debug)] + pub struct #block_struct_name { + #block_struct_fields } - fn id(&self) -> &'static str { - #block_id + + impl Block for #block_struct_name { + fn behavior(&self) -> BlockBehavior { + #block_behavior + } + fn id(&self) -> &'static str { + #block_id + } } - } - impl From<#block_struct_name> for BlockState { - fn from(b: #block_struct_name) -> Self { - #from_block_to_state_match + impl From<#block_struct_name> for BlockState { + fn from(b: #block_struct_name) -> Self { + #from_block_to_state_match + } } - } - impl Default for #block_struct_name { - fn default() -> Self { - Self { - #block_default_fields + impl Default for #block_struct_name { + fn default() -> Self { + Self { + #block_default_fields + } } } - } - }; + }; - block_structs.extend(block_struct); + block_structs.extend(block_struct); + } } let last_state_id = (state_id - 1) as u32; - quote! { + let mut generated = quote! { #property_enums #[repr(u32)] @@ -469,18 +471,6 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { #block_state_enum_variants } - #block_structs - - impl From for Box { - fn from(b: BlockState) -> Self { - let b = b as usize; - match b { - #from_state_to_block_match - _ => panic!("Invalid block state: {}", b), - } - } - } - impl BlockState { /// Returns the highest possible state #[inline] @@ -488,7 +478,23 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { #last_state_id } } - + }; + + if cfg!(feature = "trait") { + generated.extend(quote! { + #block_structs + + impl From for Box { + fn from(b: BlockState) -> Self { + let b = b as usize; + match b { + #from_state_to_block_match + _ => panic!("Invalid block state: {}", b), + } + } + } + }); } - .into() + + generated.into() } -- cgit v1.2.3