Skip to main content

zyn

Macro zyn 

Source
zyn!() { /* proc-macro */ }
Expand description

Expands a zyn template into a [proc_macro2::TokenStream].

Everything outside {{ }} and @ directives passes through as literal tokens.

§Interpolation

{{ expr }} inserts any [quote::ToTokens] value:

let name = format_ident!("my_fn");
zyn! { fn {{ name }}() {} }
// output: fn my_fn() {}

§Pipes

{{ expr | pipe }} transforms the value before inserting it. Pipes chain left to right:

zyn! { fn {{ name | snake }}() {} }
// name = "HelloWorld" → fn hello_world() {}

zyn! { fn {{ name | snake | ident:"get_{}" }}() {} }
// name = "HelloWorld" → fn get_hello_world() {}

Built-in pipes:

PipeInputOutput
snakeHelloWorldhello_world
pascalhello_worldHelloWorld
camelhello_worldhelloWorld
screamingHelloWorldHELLO_WORLD
kebabHelloWorld"hello-world" (string literal)
upperhelloHELLO
lowerHELLOhello
strhello"hello" (string literal)
pluraluserusers
singularusersuser
ident:"pattern_{}"hellopattern_hello (ident)
fmt:"pattern_{}"hello"pattern_hello" (string literal)
trim__foo__foo

§@if

zyn! {
    @if (is_async) {
        async fn {{ name }}() {}
    } @else if (is_unsafe) {
        unsafe fn {{ name }}() {}
    } @else {
        fn {{ name }}() {}
    }
}

Without @else, emits nothing when false:

zyn! { @if (is_pub) { pub } fn {{ name }}() {} }
// is_pub = true  → pub fn my_fn() {}
// is_pub = false →     fn my_fn() {}

§@for

Iterator form:

zyn! {
    @for (field in fields.iter()) {
        pub {{ field.ident }}: {{ field.ty }},
    }
}
// output: pub x: f64, pub y: f64,

Count form (no binding, repeats N times):

zyn! { @for (3) { x, }}
// output: x, x, x,

§@match

zyn! {
    @match (kind) {
        Kind::Struct => { struct {{ name }} {} }
        Kind::Enum   => { enum {{ name }} {} }
        _            => {}
    }
}

§Element invocation

Call a #[zyn::element] component with named props:

zyn! {
    @for (field in fields.iter()) {
        @getter(name = field.ident.clone().unwrap(), ty = field.ty.clone())
    }
}

With a children block:

zyn! {
    @wrapper(title = "hello") { inner content }
}