Skip to content

Instantly share code, notes, and snippets.

@oscartbeaumont
Created May 17, 2024 16:22
Show Gist options
  • Save oscartbeaumont/14984ca4a8974c5c7d6c89eb533c621c to your computer and use it in GitHub Desktop.
Save oscartbeaumont/14984ca4a8974c5c7d6c89eb533c621c to your computer and use it in GitHub Desktop.
Rust attribute macro for camel case
[package]
name = "macro-test"
version = "0.0.0"
edition = "2021"
[dependencies]
macro-test-macros = { path = "./macros" }
serde = { version = "1.0.202", features = ["derive"] }
serde_json = "1.0.117"
specta = "1.0.5"
[workspace]
members = ["./macros"]
[package]
name = "macro-test-macros"
version = "0.0.1"
edition = "2021"
[lib]
proc-macro = true
[dependencies]
proc-macro2 = "1.0.82"
quote = "1.0.36"
use proc_macro::TokenStream;
use quote::quote;
#[proc_macro_attribute]
pub fn my_stuff(_attr: TokenStream, item: TokenStream) -> TokenStream {
let item: proc_macro2::TokenStream = item.into();
quote! {
#[derive(serde::Serialize, specta::Type)]
#[serde(rename_all = "camelCase")]
#item
}
.into()
}
use macro_test_macros::my_stuff;
#[my_stuff]
struct Test {
a_b: i32,
}
fn main() {
println!("{:?}", serde_json::to_string(&Test { a_b: 42 }).unwrap());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment