scsys_derive/
lib.rs

1/*
2    Appellation: scsys-derive <library>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5extern crate proc_macro;
6extern crate quote;
7extern crate syn;
8
9pub(crate) mod ast;
10pub(crate) mod attrs;
11pub(crate) mod display;
12pub(crate) mod enums;
13pub(crate) mod params;
14pub(crate) mod utils;
15
16use proc_macro::TokenStream;
17use syn::{parse_macro_input, Data, DeriveInput};
18
19#[proc_macro_derive(Display, attributes(display))]
20pub fn display(input: TokenStream) -> TokenStream {
21    // Parse the inputs into the proper struct
22    let ast = parse_macro_input!(input as DeriveInput);
23
24    // Build the impl
25    let gen = display::impl_display(&ast);
26
27    gen.into()
28}
29
30/// This macro generates a parameter struct and an enum of parameter keys.
31#[proc_macro_derive(Params, attributes(param))]
32pub fn params(input: TokenStream) -> TokenStream {
33    // Parse the input tokens into a syntax tree
34    let input = parse_macro_input!(input as DeriveInput);
35
36    let gen = params::impl_params(&input);
37
38    // Return the generated code as a TokenStream
39    gen.into()
40}
41
42/// This macro automatically generates functional constructors for all enclosed variants.
43#[proc_macro_derive(VariantConstructors, attributes(variant))]
44pub fn variant_constructors(input: TokenStream) -> TokenStream {
45    let ast: DeriveInput = syn::parse(input).unwrap();
46
47    match ast.data {
48        Data::Enum(inner) => enums::impl_functional_constructors(&ast.ident, &inner.variants),
49        _ => panic!("This derive macro only works with enums"),
50    }
51    .into()
52}
53
54/*
55 ******** DEPRECATED ********
56*/
57
58#[proc_macro_derive(Keyed, attributes(param))]
59#[deprecated(since = "0.2.2", note = "Use `Params` instead")]
60pub fn keyed(input: TokenStream) -> TokenStream {
61    // Parse the input tokens into a syntax tree
62    let input = parse_macro_input!(input as DeriveInput);
63
64    let gen = params::impl_params(&input);
65
66    // Return the generated code as a TokenStream
67    gen.into()
68}