azalea_inventory_macros/
parse_macro.rs

1use syn::{
2    Ident, LitInt, Token, braced,
3    parse::{Parse, ParseStream, Result},
4};
5
6/// An identifier, colon, and number
7/// `craft_result: 1`
8pub struct Field {
9    pub name: Ident,
10    pub length: usize,
11}
12impl Parse for Field {
13    fn parse(input: ParseStream) -> Result<Self> {
14        let name = input.parse::<Ident>()?;
15        let _ = input.parse::<Token![:]>()?;
16        let length = input.parse::<LitInt>()?.base10_parse()?;
17        Ok(Self { name, length })
18    }
19}
20
21/// An identifier and a list of `Field` in curly brackets
22/// ```rust,ignore
23/// Player {
24///     craft_result: 1,
25///     ...
26/// }
27/// ```
28pub struct Menu {
29    /// The menu name, e.g. `Player`
30    pub name: Ident,
31    pub fields: Vec<Field>,
32}
33
34impl Parse for Menu {
35    fn parse(input: ParseStream) -> Result<Self> {
36        let name = input.parse::<Ident>()?;
37
38        let content;
39        braced!(content in input);
40        let fields = content
41            .parse_terminated(Field::parse, Token![,])?
42            .into_iter()
43            .collect();
44
45        Ok(Self { name, fields })
46    }
47}
48
49/// A list of `Menu`s
50/// ```rust,ignore
51/// Player {
52///     craft_result: 1,
53///     ...
54/// },
55/// ...
56/// ```
57pub struct DeclareMenus {
58    pub menus: Vec<Menu>,
59}
60impl Parse for DeclareMenus {
61    fn parse(input: ParseStream) -> Result<Self> {
62        let menus = input
63            .parse_terminated(Menu::parse, Token![,])?
64            .into_iter()
65            .collect();
66        Ok(Self { menus })
67    }
68}