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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use crate::attrs::{
is_vec_u8, parse_field_attrs, parse_repr_type, parse_type_attrs, parse_variant_attrs,
transform_expr_for_local,
};
use quote::quote;
use syn::{Data, DeriveInput, Error, Fields, Result};
pub fn derive_decode(input: &DeriveInput) -> Result<proc_macro2::TokenStream> {
let name = &input.ident;
let type_attrs = parse_type_attrs(&input.attrs)?;
match &input.data {
Data::Struct(data) => match &data.fields {
Fields::Named(fields) => {
let decode_fields = fields
.named
.iter()
.map(|f| {
let field_name = &f.ident;
let field_attrs = parse_field_attrs(&f.attrs)?;
let decode_expr = if field_attrs.varint32 {
quote! {
let #field_name = voidmc_codec::VarI32::decode(buf)?.0;
}
} else if field_attrs.varint64 {
quote! {
let #field_name = voidmc_codec::VarI64::decode(buf)?.0;
}
} else if field_attrs.json {
quote! {
let json_str = String::decode(buf)?;
let #field_name = serde_json::from_str(&json_str).map_err(|_| voidmc_codec::DecodeError::InvalidLength)?;
}
} else if let Some(len_expr) = &field_attrs.fixed_length {
let transformed_expr = transform_expr_for_local(len_expr);
// Use optimized path for Vec<u8>
if is_vec_u8(f) {
quote! {
let #field_name = {
let expected_len = ((#transformed_expr) as i64) as usize;
voidmc_codec::decode_fixed_length_vec_u8(expected_len, buf)?
};
}
} else {
quote! {
let #field_name = {
let expected_len = ((#transformed_expr) as i64) as usize;
voidmc_codec::decode_fixed_length_vec(expected_len, buf)?
};
}
}
} else if field_attrs.remaining {
// Remaining attribute: consume all remaining bytes on decode
if is_vec_u8(f) {
quote! {
let #field_name = voidmc_codec::decode_remaining_vec_u8(buf)?;
}
} else {
// Error: remaining only works with Vec<u8>
return Err(Error::new_spanned(
f,
"remaining attribute only works with Vec<u8>",
));
}
} else {
quote! {
let #field_name = <_>::decode(buf)?;
}
};
Ok(decode_expr)
})
.collect::<Result<Vec<_>>>()?;
let field_names = fields.named.iter().map(|f| &f.ident);
let expanded = quote! {
impl voidmc_codec::Decode for #name {
fn decode(buf: &mut &[u8]) -> Result<Self, voidmc_codec::DecodeError> {
#(#decode_fields)*
Ok(Self {
#(#field_names),*
})
}
}
};
Ok(expanded)
}
Fields::Unnamed(_) => Err(Error::new_spanned(
input,
"Decode derive for tuple structs is not supported",
)),
Fields::Unit => {
let expanded = quote! {
impl voidmc_codec::Decode for #name {
fn decode(_buf: &mut &[u8]) -> Result<Self, voidmc_codec::DecodeError> {
Ok(Self)
}
}
};
Ok(expanded)
}
},
Data::Enum(data) => {
let repr_type = parse_repr_type(&input.attrs)?;
if type_attrs.tagged {
// Tagged enum with explicit packet IDs
let decode_variants = data
.variants
.iter()
.map(|v| {
let variant_name = &v.ident;
let variant_attrs = parse_variant_attrs(&v.attrs)?;
let packet_id = match variant_attrs.packet_id {
Some(id) => id,
None => {
return Err(Error::new_spanned(
v,
"tagged enum variant must have #[codec(packet_id = ...)] attribute",
));
}
};
Ok(match &v.fields {
Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
quote! {
#packet_id => {
let inner = <_>::decode(buf)?;
Self::#variant_name(inner)
}
}
}
_ => {
return Err(Error::new_spanned(
v,
"Enum variant must have exactly one unnamed field",
));
}
})
})
.collect::<Result<Vec<_>>>()?;
let expanded = quote! {
impl voidmc_codec::Decode for #name {
fn decode(buf: &mut &[u8]) -> Result<Self, voidmc_codec::DecodeError> {
let packet_id = u8::decode(buf)?;
Ok(match packet_id {
#(#decode_variants),*
_ => return Err(voidmc_codec::DecodeError::InvalidPacketId(Some(packet_id))),
})
}
}
};
Ok(expanded)
} else if let Some(repr) = repr_type {
// Repr enum with explicit discriminants
let repr_type_ident = syn::Ident::new(&repr, proc_macro2::Span::call_site());
let decode_variants = data
.variants
.iter()
.map(|v| {
let variant_name = &v.ident;
match &v.fields {
Fields::Unit => {
let discriminant = v
.discriminant
.as_ref()
.map(|(_, expr)| {
quote! { #expr }
})
.ok_or_else(|| {
Error::new_spanned(
v,
"repr enum variant must have explicit discriminant",
)
})?;
Ok(quote! {
#discriminant => Self::#variant_name,
})
}
_ => Err(Error::new_spanned(
v,
"repr enum variant must be a unit variant",
)),
}
})
.collect::<Result<Vec<_>>>()?;
let encode_part = if type_attrs.varint32 {
quote! { voidmc_codec::VarI32::decode(buf)?.0 as #repr_type_ident }
} else if type_attrs.varint64 {
quote! { voidmc_codec::VarI64::decode(buf)?.0 as #repr_type_ident }
} else {
quote! { #repr_type_ident::decode(buf)? }
};
let expanded = quote! {
impl voidmc_codec::Decode for #name {
fn decode(buf: &mut &[u8]) -> Result<Self, voidmc_codec::DecodeError> {
let discriminant = #encode_part;
Ok(match discriminant {
#(#decode_variants)*
_ => return Err(voidmc_codec::DecodeError::InvalidPacketId(None)),
})
}
}
};
Ok(expanded)
} else {
Err(Error::new_spanned(
input,
"Enum Decode derive requires #[codec(tagged)] attribute or #[repr(...)] attribute",
))
}
}
Data::Union(_) => Err(Error::new_spanned(input, "Unions are not supported")),
}
}