Skip to content

Commit

Permalink
partial
Browse files Browse the repository at this point in the history
  • Loading branch information
0123456789-jpg committed Mar 20, 2024
1 parent 03c32a4 commit cdc38ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/util/edcode-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ maintenance = { status = "passively-maintained" }

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

[dev-dependencies]
rimecraft-edcode = { path = "../edcode" }
Expand Down
28 changes: 26 additions & 2 deletions crates/util/edcode-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +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 {
TokenStream::new()
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()
}
}
}

0 comments on commit cdc38ad

Please sign in to comment.