Skip to main content

alux_shape_macros/
lib.rs

1#![doc = include_str!("../README.md")]
2
3mod construct;
4mod layout;
5mod shape;
6mod words;
7mod written;
8
9use proc_macro::TokenStream;
10use syn::{DeriveInput, parse_macro_input};
11
12/// States `ShapeOf` for a layout, reading its members and their names from the type, and how a
13/// value is made from them: `Product` for a struct, `Sum` for an enum of two to sixteen alternatives.
14#[proc_macro_derive(Shape, attributes(written, serde))]
15pub fn derive_shape(input: TokenStream) -> TokenStream {
16    let input = parse_macro_input!(input as DeriveInput);
17
18    shape::expand(input).unwrap_or_else(syn::Error::into_compile_error).into()
19}
20
21/// Emits the layouts a block of shape declarations states, beside the declarations themselves.
22///
23/// Written outside `#[ext(… defunc(via = shape))]`, which it re-emits untouched:
24///
25/// ```rust ignore
26/// #[shape_layout]
27/// #[ext(name = UserShapeExt, defunc(via = shape))]
28/// pub impl<This> This
29/// where
30///     This: ShapeAlg + FieldAlg,
31/// {
32///     fn user_shape(&self) {
33///         self.record().field::<String>(display_name, self.text())
34///     }
35/// }
36/// ```
37///
38/// Each member states the type carrying it, which is what a layout is emitted from. An optional
39/// argument states how the layout writes its names, defaulting to `"camelCase"`.
40///
41/// The emitted layout derives `serde::Serialize` and `serde::Deserialize`, so a crate using this needs
42/// `serde` among its dependencies.
43#[proc_macro_attribute]
44pub fn shape_layout(attr: TokenStream, item: TokenStream) -> TokenStream {
45    layout::expand(attr.into(), item.into()).unwrap_or_else(syn::Error::into_compile_error).into()
46}