1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
use proc_macro2::Ident;
use quote::quote;
use syn::{
LitStr, Token, braced,
ext::IdentExt,
parenthesized,
parse::{self, Parse, ParseStream},
punctuated::Punctuated,
token,
};
use crate::{PropertyKind, name_to_ident};
/// `"snowy": Snowy(false)` or `"axis": properties::Axis::Y`
#[derive(Debug)]
pub struct PropertyWithNameAndDefault {
// "snowy" "axis"
pub name: String,
/// The property name, potentially modified so it works better as a struct
/// field.
pub name_ident: Ident,
// Snowy / Axis
pub property_type: Ident,
pub property_value_type: Ident,
/// Whether it's an enum or a boolean.
pub kind: PropertyKind,
// false / properties::Axis::Y
pub default: proc_macro2::TokenStream,
}
impl Parse for PropertyWithNameAndDefault {
fn parse(input: ParseStream) -> parse::Result<Self> {
// `"snowy": Snowy(false)` or `"axis": properties::Axis::Y`
let property_name = input.parse::<LitStr>()?.value();
input.parse::<Token![:]>()?;
let first_ident = input.call(Ident::parse_any)?;
let mut property_default = quote! { #first_ident };
let property_type: Ident;
let property_value_type: Ident;
let mut kind = PropertyKind::Bool;
if input.parse::<Token![::]>().is_ok() {
// enum
kind = PropertyKind::Enum;
property_type = first_ident.clone();
property_value_type = first_ident;
let variant = input.parse::<Ident>()?;
property_default = quote! { properties::#property_default::#variant };
} else {
// must be a unit struct if it's not an enum
let content;
let _paren_token: token::Paren = parenthesized!(content in input);
// we use this instead of .parse so it works with rust keywords like true and
// false
let unit_struct_inner = content.call(Ident::parse_any)?;
let unit_struct_inner_string = unit_struct_inner.to_string();
if matches!(unit_struct_inner_string.as_str(), "true" | "false") {
property_value_type = Ident::new("bool", first_ident.span());
property_type = first_ident;
property_default = quote! { #unit_struct_inner };
} else {
return Err(input.error("Expected a boolean or an enum variant"));
}
};
let property_name_ident = name_to_ident(&property_name);
Ok(PropertyWithNameAndDefault {
name: property_name,
name_ident: property_name_ident,
property_type,
property_value_type,
kind,
default: property_default,
})
}
}
/// `"snowy" => Snowy(bool)`
pub struct PropertyDefinition {
pub name: LitStr,
pub data: PropertyData,
}
impl Parse for PropertyDefinition {
fn parse(input: ParseStream) -> parse::Result<Self> {
// "face" => Face {
// Floor,
// Wall,
// Ceiling
// },
// if you're wondering, the reason it's in quotes is because `type` is
// a keyword in rust so if we don't put it in quotes it results in a
// syntax error
let name = input.parse()?;
input.parse::<Token![=>]>()?;
let property_type = input.parse()?;
input.parse::<Token![,]>()?;
Ok(PropertyDefinition {
name,
data: property_type,
})
}
}
pub enum PropertyData {
/// `Axis { X = "x", Y = "y", Z = "z" }`
Enum {
enum_name: Ident,
variants: Vec<PropertyVariant>,
},
/// `Snowy(bool)`
Bool { struct_name: Ident },
}
impl Parse for PropertyData {
// like `Axis { X = "x", Y = "y", Z = "z" }` or `Waterlogged(bool)`
fn parse(input: ParseStream) -> parse::Result<Self> {
let keyword = Ident::parse(input)?;
fn parse_braced(
input: ParseStream,
) -> parse::Result<Punctuated<PropertyVariant, Token![,]>> {
let content;
braced!(content in input);
let variants = content.parse_terminated(parse_variant, Token![,])?;
Ok(variants)
}
/// Parses something like `X = "x"`
fn parse_variant(input: ParseStream) -> parse::Result<PropertyVariant> {
let ident = Ident::parse(input)?;
input.parse::<Token![=]>()?;
let name = input.parse::<syn::LitStr>()?;
Ok(PropertyVariant { ident, name })
}
fn parse_paren(input: ParseStream) -> parse::Result<Ident> {
let content;
parenthesized!(content in input);
let inner = content.parse::<Ident>()?;
Ok(inner)
}
if let Ok(variants) = parse_braced(input) {
Ok(Self::Enum {
enum_name: keyword,
variants: variants.into_iter().collect(),
})
} else if let Ok(inner) = parse_paren(input) {
assert_eq!(
inner.to_string(),
"bool",
"Currently only bool unit structs are supported"
);
Ok(Self::Bool {
struct_name: keyword,
})
} else {
Err(input.error("Expected a unit struct or an enum"))
}
}
}
pub struct PropertyVariant {
/// The Rust identifier for the property variant, like `X` or `_1`.
pub ident: Ident,
/// The Minecraft name for the property variant, like `"x"` or `"1"`.
pub name: LitStr,
}
/// Parses comma separated `PropertyDefinition`s like `"snowy" => Snowy(bool),`
pub fn parse_property_definitions(input: ParseStream) -> parse::Result<Vec<PropertyDefinition>> {
let mut property_definitions = Vec::new();
while !input.is_empty() {
property_definitions.push(input.parse()?);
}
Ok(property_definitions)
}
|