Skip to main content

contained_derive/
lib.rs

1/*
2    Appellation: contained-derive <library>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5//! derive macros for the contained crate
6extern crate proc_macro;
7extern crate quote;
8extern crate syn;
9
10#[allow(dead_code)]
11pub(crate) mod attrs;
12
13pub(crate) mod impls {
14    #[doc(inline)]
15    pub use self::{gsw::*, wrapper::*};
16
17    mod gsw;
18    mod wrapper;
19}
20
21use proc_macro::TokenStream;
22use syn::{DeriveInput, parse_macro_input};
23
24/// The [`Wrapper`] macro is designed for single-field structs, implementing additional methods
25/// supporting interactions with the inner value
26#[proc_macro_derive(Wrapper, attributes(wrap))]
27pub fn wrapper(input: TokenStream) -> TokenStream {
28    // Parse the inputs into the proper struct
29    let ast = parse_macro_input!(input as DeriveInput);
30
31    // Build the impl
32    let res = impls::impl_wrapper(&ast);
33
34    res.into()
35}
36
37/// The [`Get`] derive macros is designed to streamline the process of creating getter methods
38/// for structs. Coupled with the custom attributes, one can toggle the generation of mutable
39/// getters and define alternative method names for accessing the inner value.
40#[proc_macro_derive(Get, attributes(gsw))]
41pub fn get(input: TokenStream) -> TokenStream {
42    // Parse the inputs into the proper struct
43    let ast = parse_macro_input!(input as DeriveInput);
44
45    // Build the impl
46    let res = impls::impl_get(&ast);
47
48    res.into()
49}
50
51/// The [`SetWith`] macros is used to generate setter methods for struct fields.
52#[proc_macro_derive(SetWith, attributes(gsw))]
53pub fn set(input: TokenStream) -> TokenStream {
54    // Parse the inputs into the proper struct
55    let ast = parse_macro_input!(input as DeriveInput);
56
57    // Build the impl
58    let res = impls::impl_wrapper(&ast);
59
60    res.into()
61}