Created
May 17, 2024 16:22
-
-
Save oscartbeaumont/14984ca4a8974c5c7d6c89eb533c621c to your computer and use it in GitHub Desktop.
Rust attribute macro for camel case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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