Compose-idents: a new `macro_rules!` alternative

⚓ Rust    📅 2025-09-06    👤 surdeus    👁️ 1      

surdeus

compose-idents - an advanced token-manipulation macro library and a simpler alternative to macro_rules!.

It could be seen as a merger between paste and duplicate crates. The long-term plan for the project is to turn it into a fully fledged codegen-specific template engine.

Current features:

  • Identifier generation

    • Making new identifiers from parts using built-in DSL , e.g. concat(lower(FOO), _, to_ident("bar"))foo_bar.
    • Turning non-ident-ish tokens into idents: normalize(&'static str)static_str.
  • Code repetition

    Making multiple different variations of the code (e.g., many tests for different types, bindings to C APIs, etc.).

  • String formatting

    It is possible to format string literals, including in doc-attributes: #[doc = "This is a docstring for % my_fn %"].

  • Casing manipulation

    Conversions between camelCase, snake_case, etc. are supported.

  • Unique/temporary identifier generation

    Useful for globals whose names must not collide.

Here is a short example:

use compose_idents::compose_item;

#[compose_item(
    for (suffix, (interjection, noun)) in [
        (BAR, (Hello, "world")),
        (baz, ("Hallo", "WELT")),
    ]

    my_fn = concat(foo, _, lower(suffix)),
    greeting = concat(to_str(interjection), ", ", lower(noun), "!"),
)]
#[doc = "Makes a greeting: % greeting %"]
fn my_fn() -> &'static str {
    greeting
}

assert_eq!(foo_bar(), "Hello, world!");
assert_eq!(foo_baz(), "Hallo, welt!");

Links

Name URL
Docs.rs https://docs.rs/compose-idents/
GitHub https://github.com/AndreiPashkin/compose-idents

3 posts - 2 participants

Read full topic

🏷️ Rust_feed