Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Trait Reference

Render

pub trait Render {
    fn render(&self, input: &Input) -> zyn::TokenStream;
}

Implemented by types generated by #[zyn::element]. Called inside zyn!(...) for every @element invocation. The input parameter carries the proc macro’s Input context — attributes, ident, generics, visibility of the annotated item. input is provided automatically by #[zyn::derive], #[zyn::attribute], and #[zyn::element]. For manual usage, define let input: Input = ...; before calling zyn!.

Elements are always infallible — diagnostics are expressed via error!, warn!, note!, help!, and bail! macros generated by #[zyn::element], available inside element function bodies.

FromInput

pub trait FromInput: Sized {
    fn from_input(input: &Input) -> zyn::Result<Self>;
}

Extracts typed data from an Input context. Implemented by:

  • #[derive(Attribute)] structs (attribute mode) — searches input.attrs() for a named attribute
  • syn::Ident — returns input.ident()
  • syn::Generics — returns input.generics()
  • syn::Visibility — returns input.vis()
  • Fields<T> — extracts struct fields
  • Variants — extracts enum variants
  • Data<T: FromData> — re-parses the full input as T
  • Extract<T: FromInput> and Attr<T: FromInput> — wrapper delegates to T

FromFields

pub trait FromFields: Sized {
    fn from_fields(fields: syn::Fields) -> zyn::Result<Self>;
}

Converts syn::Fields into a specific shape. Implemented for syn::Fields (identity), syn::FieldsNamed (errors on non-named), syn::FieldsUnnamed (errors on non-unnamed). Used as the type parameter of Fields<T>.

Pipe

pub trait Pipe {
    type Input;
    type Output: quote::ToTokens;

    fn pipe(&self, input: Self::Input) -> Self::Output;
}

Implemented by types generated by #[zyn::pipe] and by all built-in pipe structs. Output must implement ToTokens so the final value in a pipe chain can be emitted to the token stream.

All built-in pipes accept String as Input. Custom pipes in a chain also receive String because the intermediate value is re-stringified between each pipe via .to_string().

Expand (internal)

pub trait Expand {
    fn expand(
        &self,
        output: &syn::Ident,
        idents: &mut ident::Iter,
    ) -> zyn::TokenStream;
}

Internal trait implemented by every AST node type. output is the name of the current accumulator variable (__zyn_ts_N). idents is a shared counter used to allocate unique variable names for nested structures. Not part of the public API; relevant only when implementing new AST node types.