Crate fv_template

source ·
Expand description

Compile-time support for interpolated string templates using field-value expressions.

Field-value templates

A field-value template is a string literal surrounded by field-value expressions:

   a, b: 42, "Some text {c} and {d: true}", e, f: "text"
   ───┬────  ───────────────┬─────────────  ──────┬─────
before literal           literal             after literal

The template string literal consists of blocks of text with holes between braces, where the value in a hole is a field-value expression:

"Some text {c} and {d: true}"
           ─┬─     ────┬────
            └────┬─────┘
                hole

"Some text {c} and {d: true}"
 ─────┬────   ──┬──
      └────┬────┘
         text

The syntax is similar to Rust’s format_args! macro, but leans entirely on standard field-value expressions for specifying values to interpolate.

Why not format_args!?

Rust’s format_args! macro already defines a syntax for string interpolation, but isn’t suitable for all situations:

  • It’s core purpose is to build strings. format_args! is based on machinery that throws away type-specific information eagerly. It also performs optimizations at compile time that inline certain values into the builder.
  • It doesn’t have a programmatic API. You can only make assumptions about how a format_args! invocation will behave by observing the syntactic tokens passed to it at compile-time. You don’t get any visibility into the format literal itself.
  • Flags are compact for formatting, but don’t scale. The :?#<> tokens used for customizing formatting are compact, but opaque, and don’t naturally allow for arbitrarily complex annotation like attributes do.

When any of those trade-offs in format_args! becomes a problem, field-value templates may be a solution.

Structs

  • An error encountered while parsing a template.
  • A field-value template.

Traits