azalea_inventory_macros/
location_enum.rs

1use proc_macro2::TokenStream;
2use quote::quote;
3use syn::Ident;
4
5use crate::{parse_macro::DeclareMenus, utils::to_pascal_case};
6
7pub fn generate(input: &DeclareMenus) -> TokenStream {
8    // pub enum MenuLocation {
9    //     Player(PlayerMenuLocation),
10    //     ...
11    // }
12    // pub enum PlayerMenuLocation {
13    //     CraftResult,
14    //     Craft,
15    //     Armor,
16    //     Inventory,
17    //     Offhand,
18    // }
19    // ...
20
21    let mut menu_location_variants = quote! {};
22    let mut enums = quote! {};
23    for menu in &input.menus {
24        let name_snake_case = &menu.name;
25        let variant_name = Ident::new(
26            &to_pascal_case(&name_snake_case.to_string()),
27            name_snake_case.span(),
28        );
29        let enum_name = Ident::new(&format!("{variant_name}MenuLocation"), variant_name.span());
30        menu_location_variants.extend(quote! {
31            #variant_name(#enum_name),
32        });
33        let mut individual_menu_location_variants = quote! {};
34        for field in &menu.fields {
35            let field_name = &field.name;
36            let variant_name =
37                Ident::new(&to_pascal_case(&field_name.to_string()), field_name.span());
38            individual_menu_location_variants.extend(quote! {
39                #variant_name,
40            });
41        }
42        enums.extend(quote! {
43            #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
44            pub enum #enum_name {
45                #individual_menu_location_variants
46            }
47        });
48    }
49
50    quote! {
51        pub enum MenuLocation {
52            #menu_location_variants
53        }
54
55        #enums
56    }
57}