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

Pipes

Pipes transform interpolated values. Add them after a |:

zyn! {
    fn {{ name | snake }}() {}
}
// output: fn hello_world() {}

Pipe names are written in snake_case in templates — they resolve to PascalCase structs automatically.

Built-in Pipes

PipeInputOutputExample
upperHelloWorldHELLOWORLD{{ name | upper }}
lowerHELLOhello{{ name | lower }}
snakeHelloWorldhello_world{{ name | snake }}
camelhello_worldhelloWorld{{ name | camel }}
pascalhello_worldHelloWorld{{ name | pascal }}
screamingHelloWorldHELLO_WORLD{{ name | screaming }}
kebabHelloWorld"hello-world"{{ name | kebab }}
strhello"hello"{{ name | str }}
trim__foo__foo{{ name | trim }}
pluralUserUsers{{ name | plural }}
singularusersuser{{ name | singular }}

Warning

kebab and str return string literals, not identifiers, because their output may contain characters invalid in Rust identifiers.

Chaining

Pipes can be chained. Each pipe receives the output of the previous one:

zyn! { {{ name | snake | upper }} }
// HelloWorld -> hello_world -> HELLO_WORLD

Format Pipes

The ident and fmt pipes take a format pattern via : syntax. Use {} as the placeholder:

zyn! {
    fn {{ name | ident:"get_{}" }}() {}     // hello -> get_hello (as ident)
    fn {{ name | ident:"{}_impl" }}() {}    // hello -> hello_impl (as ident)
    const NAME: &str = {{ name | fmt:"{}" }};  // hello -> "hello" (as string literal)
}

ident produces an identifier, fmt produces a string literal.

Combine with case pipes:

zyn! { {{ name | snake | ident:"get_{}" }} }
// HelloWorld -> hello_world -> get_hello_world

Custom Pipes

Define custom pipes with #[zyn::pipe]. The first parameter is the input, the return type is the output:

#[zyn::pipe]
fn prefix(input: String) -> syn::Ident {
    syn::Ident::new(
        &format!("pfx_{}", input),
        zyn::Span::call_site(),
    )
}

This generates a unit struct Prefix implementing the Pipe trait. Use it by its snake_case name:

zyn! { {{ name | prefix }} }
// hello -> pfx_hello

Custom Names

Override the template name:

#[zyn::pipe("yell")]
fn make_loud(input: String) -> syn::Ident {
    syn::Ident::new(
        &format!("{}__LOUD", input.to_uppercase()),
        zyn::Span::call_site(),
    )
}

zyn! { {{ name | yell }} }
// hello -> HELLO__LOUD

Chaining with Built-ins

Custom pipes chain with built-in pipes:

zyn! { {{ name | snake | prefix }} }
// HelloWorld -> hello_world -> pfx_hello_world