Skip to content

Commit

Permalink
cargo fmt, my opp.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sweattypalms committed Jan 4, 2025
1 parent b1339a1 commit 0bc7313
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/lib/derive_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ pub fn get_registry_entry(input: TokenStream) -> TokenStream {
#[proc_macro]
pub fn get_packet_entry(input: TokenStream) -> TokenStream {
static_loading::packets::get(input)
}
}
9 changes: 6 additions & 3 deletions src/lib/derive_macros/src/net/encode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::helpers::{get_derive_attributes, StructInfo};
use crate::net::packets::get_packet_details_from_attributes;
use crate::static_loading::packets::PacketBoundiness;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput, Fields};
use crate::static_loading::packets::PacketBoundiness;

// Generate packet ID encoding snippets
fn generate_packet_id_snippets(
Expand Down Expand Up @@ -136,8 +136,11 @@ pub(crate) fn derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);

let packet_attr = get_derive_attributes(&input, "packet");
let (packet_id_snippet, async_packet_id_snippet) =
generate_packet_id_snippets(get_packet_details_from_attributes(packet_attr.as_slice(), PacketBoundiness::Clientbound).unzip().1);
let (packet_id_snippet, async_packet_id_snippet) = generate_packet_id_snippets(
get_packet_details_from_attributes(packet_attr.as_slice(), PacketBoundiness::Clientbound)
.unzip()
.1,
);

let (sync_impl, async_impl) = match &input.data {
syn::Data::Struct(data) => {
Expand Down
25 changes: 19 additions & 6 deletions src/lib/derive_macros/src/net/packets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ fn parse_packet_attribute(attr: &Attribute) -> Option<(String, String)> {
}

/// Returns: (state, packet_id)
pub(crate) fn get_packet_details_from_attributes(attrs: &[Attribute], bound_to: PacketBoundiness) -> Option<(String, u8)> {
pub(crate) fn get_packet_details_from_attributes(
attrs: &[Attribute],
bound_to: PacketBoundiness,
) -> Option<(String, u8)> {
let mut val = Option::<(String, String)>::None;

for attr in attrs {
Expand All @@ -38,7 +41,8 @@ pub(crate) fn get_packet_details_from_attributes(attrs: &[Attribute], bound_to:

let (state, packet_id) = val?;

let packet_id = parse_packet_id(state.as_str(), packet_id, bound_to).expect("parse_packet_id failed");
let packet_id =
parse_packet_id(state.as_str(), packet_id, bound_to).expect("parse_packet_id failed");

Some((state, packet_id))
}
Expand Down Expand Up @@ -85,11 +89,12 @@ pub fn bake_registry(input: TokenStream) -> TokenStream {
let path = entry.path();
let file_name = path.file_name().expect("file_name failed").to_os_string();


println!(
" {} {}",
"[FERRUMC_MACROS]".bold().blue(),
format!("Parsing file: {}", file_name.to_string_lossy()).white().bold()
format!("Parsing file: {}", file_name.to_string_lossy())
.white()
.bold()
);

if !path.is_file() {
Expand All @@ -105,12 +110,20 @@ pub fn bake_registry(input: TokenStream) -> TokenStream {
};

// If the struct does not have the #[packet(...)] attribute, then skip it.
if !item_struct.attrs.iter().any(|attr| attr.path().is_ident("packet")) {
if !item_struct
.attrs
.iter()
.any(|attr| attr.path().is_ident("packet"))
{
continue;
}

// format: #[packet(packet_id = 0x00, state = "handshake")]
let (state, packet_id) = get_packet_details_from_attributes(&item_struct.attrs, PacketBoundiness::Serverbound).expect(
let (state, packet_id) = get_packet_details_from_attributes(
&item_struct.attrs,
PacketBoundiness::Serverbound,
)
.expect(
"parse_packet_attribute failed\
\nPlease provide the packet_id and state fields in the #[packet(...)] attribute.\
\nExample: #[packet(packet_id = 0x00, state = \"handshake\")]",
Expand Down
3 changes: 1 addition & 2 deletions src/lib/derive_macros/src/static_loading/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

pub(crate) mod packets;
pub(crate) mod registry;
pub(crate) mod packets;
8 changes: 6 additions & 2 deletions src/lib/derive_macros/src/static_loading/packets.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::TokenStream;
use quote::quote;
use std::fmt::Display;
use std::sync::LazyLock;
use proc_macro2::TokenStream;
use syn::parse::Parse;
use syn::{parse_macro_input, LitStr, Token};

Expand All @@ -10,7 +10,11 @@ pub(crate) static PACKETS_JSON: LazyLock<serde_json::Value> = LazyLock::new(|| {
serde_json::from_str(json_str).unwrap()
});

pub(crate) fn get_packet_id(state: impl Into<PacketState>, bound: PacketBoundiness, packet_name: &str) -> u8 {
pub(crate) fn get_packet_id(
state: impl Into<PacketState>,
bound: PacketBoundiness,
packet_name: &str,
) -> u8 {
let mut current_value = &*PACKETS_JSON;

// remove `"` from start and end of the packet_name:
Expand Down
10 changes: 6 additions & 4 deletions src/lib/derive_macros/src/static_loading/registry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::sync::LazyLock;
use quote::quote;
use std::sync::LazyLock;
use syn::{parse_macro_input, LitStr};

static JSON_CONTENT: LazyLock<serde_json::Value> = LazyLock::new(|| {
Expand All @@ -15,11 +15,13 @@ pub(crate) fn get(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

for part in parts {
// current_value = &current_value[part];
current_value = current_value.get(part)
current_value = current_value
.get(part)
.unwrap_or_else(|| panic!("Could not find key: {}", part));
}

let protocol_id = current_value.get("protocol_id")
let protocol_id = current_value
.get("protocol_id")
.and_then(|v| v.as_u64())
.unwrap_or_else(|| panic!("Could not find key: {}", "protocol_id"));

Expand All @@ -28,4 +30,4 @@ pub(crate) fn get(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
};

proc_macro::TokenStream::from(expanded)
}
}

0 comments on commit 0bc7313

Please sign in to comment.