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 gsw;
14pub(crate) mod utils;
15
16use proc_macro::TokenStream;
17use syn::{Data, DeriveInput, parse_macro_input};
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 res = display::impl_display(&ast);
26
27    res.into()
28}
29/// This macro automatically generates functional constructors for all enclosed variants.
30#[proc_macro_derive(VariantConstructors, attributes(variant))]
31pub fn variant_constructors(input: TokenStream) -> TokenStream {
32    let ast: DeriveInput = syn::parse(input).unwrap();
33
34    match ast.data {
35        Data::Enum(inner) => enums::impl_functional_constructors(&ast.ident, &inner.variants),
36        _ => panic!("This derive macro only works with enums"),
37    }
38    .into()
39}
40
41#[proc_macro_derive(Getter)]
42pub fn getter_derive(input: TokenStream) -> TokenStream {
43    let input = parse_macro_input!(input as DeriveInput);
44    gsw::impl_getter(&input).into()
45}
46
47#[proc_macro_derive(Set)]
48pub fn set_derive(input: TokenStream) -> TokenStream {
49    let input = parse_macro_input!(input as DeriveInput);
50    gsw::impl_set(&input).into()
51}
52
53#[proc_macro_derive(With)]
54pub fn with_derive(input: TokenStream) -> TokenStream {
55    let input = parse_macro_input!(input as DeriveInput);
56    gsw::impl_with(&input).into()
57}