Skip to main content

zyn_core/
lib.rs

1pub mod ast;
2pub mod case;
3pub mod ident;
4pub mod pipes;
5
6pub use pipes::*;
7
8/// Internal trait for expanding AST nodes into generated code.
9///
10/// Each AST node implements this to produce a `TokenStream` that builds
11/// the output incrementally using unique identifier variables.
12pub trait Expand {
13    fn expand(
14        &self,
15        output: &proc_macro2::Ident,
16        idents: &mut ident::Iter,
17    ) -> proc_macro2::TokenStream;
18}
19
20/// Trait for renderable elements invoked via `@element_name(props)` in templates.
21///
22/// Implement this on a struct to make it usable as a template element.
23/// The struct fields become the element's props, and `render()` produces
24/// the output token stream.
25///
26/// # Example
27///
28/// ```ignore
29/// struct FieldDecl {
30///     vis: syn::Visibility,
31///     name: syn::Ident,
32///     ty: syn::Type,
33/// }
34///
35/// impl zyn::Render for FieldDecl {
36///     fn render(&self) -> syn::Result<proc_macro2::TokenStream> {
37///         let vis = &self.vis;
38///         let name = &self.name;
39///         let ty = &self.ty;
40///         Ok(zyn::zyn! { {{ vis }} {{ name }}: {{ ty }}, })
41///     }
42/// }
43/// ```
44///
45/// Or use the `#[zyn::element]` attribute macro to generate this automatically.
46pub trait Render {
47    fn render(&self) -> syn::Result<proc_macro2::TokenStream>;
48}
49
50/// Trait for pipe transforms used via `{{ expr | pipe_name }}` in templates.
51///
52/// Pipes transform interpolated values before they are emitted as tokens.
53/// The `Input` type is what the pipe receives (typically `String` from `.to_string()`),
54/// and `Output` is what gets emitted to the token stream.
55///
56/// # Example
57///
58/// ```ignore
59/// struct Prefix;
60///
61/// impl zyn::Pipe for Prefix {
62///     type Input = String;
63///     type Output = proc_macro2::Ident;
64///
65///     fn pipe(&self, input: String) -> proc_macro2::Ident {
66///         proc_macro2::Ident::new(
67///             &format!("pfx_{}", input),
68///             proc_macro2::Span::call_site(),
69///         )
70///     }
71/// }
72/// ```
73///
74/// Or use the `#[zyn::pipe]` attribute macro to generate this automatically.
75pub trait Pipe {
76    type Input;
77    type Output: quote::ToTokens;
78
79    fn pipe(&self, input: Self::Input) -> Self::Output;
80}