tencent_scf_derive/
lib.rs

1//! The derive helper for [`tencent_scf`]
2//!
3//! # Derives
4//! * `AsJson`: Mark a type as `AsJson`, no method provided.
5//!
6//! # Acknowledgement
7//! The code is adapted from the `heapsize` example from `syn` crate.
8//!
9//! [`tencent_scf`]: https://crates.io/crates/tencent_scf
10
11use quote::quote;
12use syn::{parse_macro_input, DeriveInput};
13
14#[proc_macro_derive(AsJson)]
15pub fn derive_as_json(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
16    // Parse the input
17    let input = parse_macro_input!(input as DeriveInput);
18
19    // Name of the type
20    let name = input.ident;
21
22    // Extract generic parameters
23    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
24
25    let expanded = quote! {
26        // The generated impl.
27        impl #impl_generics tencent_scf::convert::AsJson for #name #ty_generics #where_clause {}
28    };
29
30    // Hand the output tokens back to the compiler
31    proc_macro::TokenStream::from(expanded)
32}