zyn_derive/lib.rs
1//! Procedural macros for the zyn framework.
2//!
3//! Re-exported through the root `zyn` crate. All macros are accessed as
4//! `zyn::zyn!`, `#[zyn::element]`, etc.
5//!
6//! # Quick reference
7//!
8//! ```ignore
9//! // Template expansion
10//! zyn::zyn! { fn {{ name | snake }}() {} }
11//!
12//! // Reusable component
13//! #[zyn::element]
14//! fn my_getter(name: syn::Ident, ty: syn::Type) -> zyn::TokenStream { ... }
15//!
16//! // Derive macro entry point
17//! #[zyn::derive]
18//! fn my_derive(
19//! #[zyn(input)] ident: zyn::Extract<zyn::syn::Ident>,
20//! #[zyn(input)] fields: zyn::Fields,
21//! ) -> zyn::TokenStream { ... }
22//!
23//! // Typed attribute parsing
24//! #[derive(zyn::Attribute)]
25//! #[zyn("my_attr")]
26//! struct MyAttr { skip: bool, rename: Option<String> }
27//! ```
28
29mod attribute;
30mod common;
31mod macros;
32
33/// Expands a zyn template into a `TokenStream`.
34#[proc_macro]
35pub fn zyn(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
36 macros::template::expand(input.into()).into()
37}
38
39/// Expands a zyn template with diagnostic output for debugging.
40#[proc_macro]
41pub fn debug(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
42 macros::debug::expand(input.into()).into()
43}
44
45/// Defines a reusable template component that generates a struct implementing `Render`.
46#[proc_macro_attribute]
47pub fn element(
48 args: proc_macro::TokenStream,
49 input: proc_macro::TokenStream,
50) -> proc_macro::TokenStream {
51 macros::element::expand(args.into(), input.into()).into()
52}
53
54/// Defines a custom pipe transform that generates a struct implementing `Pipe`.
55#[proc_macro_attribute]
56pub fn pipe(
57 args: proc_macro::TokenStream,
58 input: proc_macro::TokenStream,
59) -> proc_macro::TokenStream {
60 macros::pipe::expand(args.into(), input.into()).into()
61}
62
63/// Defines a derive macro entry point that auto-parses `DeriveInput` into `Input`.
64#[proc_macro_attribute]
65pub fn derive(
66 args: proc_macro::TokenStream,
67 input: proc_macro::TokenStream,
68) -> proc_macro::TokenStream {
69 macros::derive::expand(args.into(), input.into()).into()
70}
71
72/// Defines an attribute macro entry point that auto-parses the annotated item into `Input`.
73#[proc_macro_attribute]
74pub fn attribute(
75 args: proc_macro::TokenStream,
76 input: proc_macro::TokenStream,
77) -> proc_macro::TokenStream {
78 macros::attribute::expand(args.into(), input.into()).into()
79}
80
81/// Derives the `Attribute` trait for typed attribute parsing from `#[attr(...)]` syntax.
82#[proc_macro_derive(Attribute, attributes(zyn))]
83pub fn derive_attribute(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
84 attribute::expand(input.into()).into()
85}