godot_netpacket_macros/
lib.rs

1/// Implement derive macros to serialize and deserialize structs
2mod netpacket;
3
4use netpacket::{
5    const_size::impl_derive_const_size, deserialize::impl_derive_deserialize,
6    serialize::impl_derive_serialize,
7};
8use proc_macro::TokenStream;
9use syn::{DeriveInput, Error, parse_macro_input};
10
11/// Derive macro to evaluate the static size of a serialized struct in bytes
12#[proc_macro_derive(ConstSize)]
13pub fn derive_const_size(input: TokenStream) -> TokenStream {
14    let input = parse_macro_input!(input as DeriveInput);
15    match impl_derive_const_size(&input) {
16        Ok(tokens) => tokens.into(),
17        Err(err) => Error::from(err).to_compile_error().into(),
18    }
19}
20
21/// Derive macro to serialize a struct into a packet
22#[proc_macro_derive(Serialize)]
23pub fn derive_serialize(input: TokenStream) -> TokenStream {
24    let input = parse_macro_input!(input as DeriveInput);
25    match impl_derive_serialize(&input) {
26        Ok(tokens) => tokens.into(),
27        Err(err) => Error::from(err).to_compile_error().into(),
28    }
29}
30
31/// Derive macro to deserialize a struct from packet
32#[proc_macro_derive(Deserialize)]
33pub fn derive_deserialize(input: TokenStream) -> TokenStream {
34    let input = parse_macro_input!(input as DeriveInput);
35    match impl_derive_deserialize(&input) {
36        Ok(tokens) => tokens.into(),
37        Err(err) => Error::from(err).to_compile_error().into(),
38    }
39}