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) use self::impls::*;
10
11pub(crate) mod impls;
12pub(crate) mod utils;
13
14pub(crate) mod ast {
15    #[allow(unused_imports)]
16    pub use self::getter::GetterAst;
17
18    mod getter;
19}
20
21pub(crate) mod attrs {
22    #[doc(inline)]
23    pub use self::prelude::*;
24
25    pub mod display_attrs;
26    pub mod nested;
27    pub mod scsys;
28    pub mod variants;
29
30    pub(crate) mod prelude {
31        pub use super::display_attrs::*;
32        pub use super::nested::*;
33        pub use super::scsys::*;
34        pub use super::variants::*;
35    }
36}
37
38use proc_macro::TokenStream;
39use syn::{Data, DeriveInput, parse_macro_input};
40
41#[proc_macro_derive(Display, attributes(scsys))]
42pub fn display(input: TokenStream) -> TokenStream {
43    // Parse the inputs into the proper struct
44    let ast = parse_macro_input!(input as DeriveInput);
45
46    // Build the impl
47    let res = impl_display(&ast);
48
49    res.into()
50}
51/// This macro automatically generates functional constructors for all enclosed variants.
52#[proc_macro_derive(VariantConstructors, attributes(scsys))]
53pub fn variant_constructors(input: TokenStream) -> TokenStream {
54    let ast: DeriveInput = syn::parse(input).unwrap();
55
56    match ast.data {
57        Data::Enum(inner) => impl_functional_constructors(&ast.ident, &inner.variants),
58        _ => panic!("This derive macro only works with enums"),
59    }
60    .into()
61}
62
63#[proc_macro_derive(Getter)]
64pub fn getter_derive(input: TokenStream) -> TokenStream {
65    let input = parse_macro_input!(input as DeriveInput);
66    impl_getter(&input).into()
67}
68
69#[proc_macro_derive(Set)]
70pub fn set_derive(input: TokenStream) -> TokenStream {
71    let input = parse_macro_input!(input as DeriveInput);
72    impl_set(&input).into()
73}
74
75#[proc_macro_derive(With)]
76pub fn with_derive(input: TokenStream) -> TokenStream {
77    let input = parse_macro_input!(input as DeriveInput);
78    impl_with(&input).into()
79}