Skip to content

Commit

Permalink
Packet proc macro
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas0008 committed Aug 5, 2024
1 parent 4fa1cce commit a5110eb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = [ "pumpkin-protocol/", "pumpkin-registry/", "pumpkin-world", "pumpkin/"]
members = [ "pumpkin-macros", "pumpkin-protocol/", "pumpkin-registry/", "pumpkin-world", "pumpkin/"]

[workspace.package]
version = "0.1.0"
Expand Down
12 changes: 12 additions & 0 deletions pumpkin-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "pumpkin-macros"
version.workspace = true
edition.workspace = true

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0.86"
quote = "1.0.36"
syn = "2.0.72"
24 changes: 24 additions & 0 deletions pumpkin-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use proc_macro::TokenStream;
use quote::quote;

extern crate proc_macro;
#[proc_macro_attribute]
pub fn packet(input: TokenStream, item: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(item.clone()).unwrap();

let name = &ast.ident;

let (impl_generics, ty_generics, _) = ast.generics.split_for_impl();

let input: proc_macro2::TokenStream = input.into();
let item: proc_macro2::TokenStream = item.into();

let gen = quote! {
#item
impl #impl_generics crate::bytebuf::packet_id::Packet for #name #ty_generics {
const PACKET_ID: i32 = #input;
}
};

gen.into()
}

0 comments on commit a5110eb

Please sign in to comment.