1use darling::FromDeriveInput;
2use proc_macro::TokenStream;
3use quote::quote;
4use syn::{ parse_macro_input, DeriveInput };
5
6#[derive(FromDeriveInput, Default)]
7#[darling(default, attributes(to))]
8struct Opts {
9 direction: Option<u8>
10}
11
12#[proc_macro_derive(BaseParser, attributes(to))]
13pub fn base_parser_derive(input: TokenStream) -> TokenStream {
14 let input = parse_macro_input!(input as DeriveInput);
15 let opts = Opts::from_derive_input(&input).expect("Wrong options");
16 let direction = opts.direction.expect("Missing direction");
17 let name = input.ident;
18 let name_string = name.to_string();
19
20 let expanded = quote! {
21 impl BaseParser for #name {
22 fn parse(packet: &mut HPacket) -> Self {
23 packet.read()
24 }
25
26 fn append_to_packet(&self, packet: &mut HPacket) {
27 packet.append(self.clone());
28 }
29
30 fn get_direction() -> HDirection {
31 if #direction == 0 { HDirection::ToClient } else { HDirection::ToServer }
32 }
33
34 fn get_packet_name() -> String {
35 #name_string.to_string()
36 }
37 }
38 };
39
40 TokenStream::from(expanded)
41}