Skip to content

Commit

Permalink
Merge branch 'main' into 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Jun 14, 2024
2 parents f7a8c6d + 7b5ed58 commit da40e2f
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions azalea-block/azalea-block-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ struct PropertyDefinitions {
/// `"snowy": Snowy(false)` or `"axis": properties::Axis::Y`
#[derive(Debug)]
struct PropertyWithNameAndDefault {
// "snowy" / "axis"
// "snowy" "axis"
name: String,
/// The property name, potentially modified so it works better as a struct
/// field.
name_ident: Ident,
// Snowy / Axis
property_type: Ident,
property_value_type: Ident,
Expand Down Expand Up @@ -100,8 +103,11 @@ impl Parse for PropertyWithNameAndDefault {
}
};

let property_name_ident = name_to_ident(&property_name);

Ok(PropertyWithNameAndDefault {
name: property_name,
name_ident: property_name_ident,
property_type,
property_value_type,
is_enum,
Expand Down Expand Up @@ -433,6 +439,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
write!(property_name, "_{index}").unwrap();
}
properties_with_name.push(PropertyWithNameAndDefault {
name_ident: name_to_ident(&property_name),
name: property_name,
property_type: property.property_type.clone(),
property_value_type: property.property_value_type.clone(),
Expand All @@ -452,12 +459,11 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
let mut block_struct_fields = quote! {};
for PropertyWithNameAndDefault {
property_value_type,
name,
name_ident,
is_enum,
..
} in &properties_with_name
{
let name_ident = Ident::new_raw(name, proc_macro2::Span::call_site());
block_struct_fields.extend(if *is_enum {
quote! { pub #name_ident: properties::#property_value_type, }
} else {
Expand Down Expand Up @@ -496,7 +502,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
let mut from_block_to_state_combination_match_inner = quote! {};
for i in 0..properties_with_name.len() {
let property = &properties_with_name[i];
let property_name = &property.name;
let property_name_ident = &property.name_ident;
let property_value_name_ident = &property.property_type;
let variant =
Ident::new(&combination[i].to_string(), proc_macro2::Span::call_site());
Expand All @@ -519,8 +525,6 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
quote! {#variant}
};

let property_name_ident =
Ident::new_raw(property_name, proc_macro2::Span::call_site());
from_block_to_state_combination_match_inner.extend(quote! {
#property_name_ident: #property_variant,
});
Expand Down Expand Up @@ -582,7 +586,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
for i in (0..properties_with_name.len()).rev() {
let PropertyWithNameAndDefault {
property_type: property_struct_name_ident,
name: property_name,
name_ident: property_name_ident,
property_value_type,
..
} = &properties_with_name[i];
Expand All @@ -598,7 +602,6 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
quote! {properties::#property_struct_name_ident::from((b / #division) % #property_variants_count)}
}
};
let property_name_ident = Ident::new_raw(property_name, proc_macro2::Span::call_site());
from_state_to_block_inner.extend(quote! {
#property_name_ident: #conversion_code,
});
Expand Down Expand Up @@ -627,12 +630,11 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {

let mut block_default_fields = quote! {};
for PropertyWithNameAndDefault {
name,
name_ident,
default: property_default,
..
} in properties_with_name
{
let name_ident = Ident::new_raw(&name, proc_macro2::Span::call_site());
block_default_fields.extend(quote! { #name_ident: #property_default, });
}

Expand Down Expand Up @@ -821,3 +823,13 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {

generated.into()
}

/// Convert a name to a Rust identifier, replacing some Rust keywords with
/// alternatives (e.g. `type` -> `kind`).
fn name_to_ident(name: &str) -> Ident {
let ident_str = match name {
"type" => "kind",
_ => &name,

Check warning on line 832 in azalea-block/azalea-block-macros/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> azalea-block/azalea-block-macros/src/lib.rs:832:14 | 832 | _ => &name, | ^^^^^ help: change this to: `name` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default

Check warning on line 832 in azalea-block/azalea-block-macros/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> azalea-block/azalea-block-macros/src/lib.rs:832:14 | 832 | _ => &name, | ^^^^^ help: change this to: `name` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
};
Ident::new(ident_str, proc_macro2::Span::call_site())
}

0 comments on commit da40e2f

Please sign in to comment.