Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edcode derive proc-macros for enum #38

Merged
merged 13 commits into from
Mar 23, 2024
27 changes: 27 additions & 0 deletions crates/util/edcode-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "rimecraft-edcode-macros"
version = "0.1.0"
edition = "2021"
authors = ["C191239 <[email protected]>"]
description = "Derive macros for rimecraft-edcode"
repository = "https://github.com/rimecraft-rs/rimecraft/"
license = "AGPL-3.0-or-later"
categories = ["encoding"]

[badges]
maintenance = { status = "passively-maintained" }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
syn = { version = "2.0" }
quote = { version = "1.0" }

[dev-dependencies]
rimecraft-edcode = { path = "../edcode" }

[lints]
workspace = true

[lib]
proc-macro = true
33 changes: 33 additions & 0 deletions crates/util/edcode-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! Proc-macros for deriving [`rimecraft_edcode`] traits.

use proc_macro::TokenStream;
use syn::{parse_macro_input, Data, DeriveInput};

macro_rules! unsupported_error {
($tr:literal, $ty:literal) => {
"deriving `$tr` to `$ty` is not supported"
};
}

/// Derive [`rimecraft_edcode::Encode`] to types.
#[proc_macro_derive(Encode)]
pub fn derive_encode(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
match input.data {
Data::Enum(data) => {
// TODO: derive `Encode` for `enum`.
todo!()
}
Data::Struct(data) => syn::Error::new(
data.struct_token.span,
unsupported_error!("Encode", "struct"),
)
.to_compile_error()
.into(),
Data::Union(data) => {
syn::Error::new(data.union_token.span, unsupported_error!("Encode", "union"))
.to_compile_error()
.into()
}
}
}
2 changes: 2 additions & 0 deletions crates/util/edcode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ maintenance = { status = "passively-maintained" }
[dependencies]
bytes = "1.5"
serde = { version = "1.0", optional = true }
rimecraft-edcode-macros = { path = "../edcode-macros", optional = true }
# custom formats
fastnbt = { version = "2.4", optional = true }
serde_json = { version = "1.0", optional = true }
Expand All @@ -24,6 +25,7 @@ glam = { version = "0.25", optional = true }
[features]
# default = ["serde", "nbt", "json", "uuid", "glam"]
serde = ["dep:serde"]
macros = ["dep:rimecraft-edcode-macros"]
# custom formats
fastnbt = ["serde", "dep:fastnbt"]
json = ["serde", "dep:serde_json"]
Expand Down
3 changes: 3 additions & 0 deletions crates/util/edcode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use std::io;

pub use bytes;

#[cfg(feature = "macros")]
pub use rimecraft_edcode_macros::Encode;

/// Describes types that can be encoded into a packet buffer.
pub trait Encode {
/// Encode into a buffer.
Expand Down