Crate metamatch

Source
Expand description

§metamatch!

A zero dependency proc-macro for practical metaprogramming.

Macro expressions using familiar Rust syntax, evaluated by a tiny interpreter.

§Supported Rust Syntax:

  • let statements and pattern matching.
  • loop, while, while let, and for loops, including continue and break
  • if and else blocks
  • Functions (fn) and lambdas (|..|)
  • Arrays ([1,2,3])
  • Ranges (0..=10)
  • All expression operators like + or <<.

§Not supported:

  • struct, enum, type, trait, generics, …

§Builtin functions:

  • lowercase(str) -> str
  • uppercase(str) -> str
  • capitalize(str) -> str
  • enumerate([T]) -> [(int, T)]
  • zip([A], [B], ..) -> [(A, B, ..)]
  • map([T], Fn(T) -> U) -> [U]
  • chars(str) -> [char]
  • bytes(str) -> [int]
  • ident(str) -> token
  • str(any) -> str
  • len([T]) -> int

String functions also work on tokens.

All functions support UFCS, so [1,2,3].len() == len([1,2,3])

§Special Purpose Macros:

  • quote! {..} -> [token]: nested version of quote!.
  • raw! {..} -> [token]: raw Rust, no template tags or expanded meta variables

Just like Rust macros, you can use any of {}, [], and () interchangably for the macro invocations.

Attribute Macros§

replicate
Generate repetitive syntax like trait impls without giving up on rustfmt or rust-analyzer.

Macros§

metamatch
Generate repetitive match arms for differently typed variants.
unquote
Evaluates arbitrary expressions.
quote
Like unquote!, but starts out in quoted mode, meaning that tokens will be pasted verbatim.