exact_format

Crates.ioexact_format
lib.rsexact_format
version0.2.1
created_at2025-03-07 16:36:24.105762+00
updated_at2025-03-07 17:20:10.52474+00
descriptionA procedural macro for compile time string replacement without using the standard format placeholder syntax (`{}`). This is most useful when dealing with strings that contain `{ }` blocks you do no wish to interpolate e.g. writing javascript.
homepage
repositoryhttps://github.com/mcmah309/exact_format
max_upload_size
id1583342
size12,983
Henry (mcmah309)

documentation

README

exact_format

A procedural macro for compile time string replacement without using the standard format placeholder syntax ({}). This is most useful when dealing with strings that contain { } blocks you do no wish to interpolate e.g. writing javascript.

Usage

The exact_format! macro allows you to replace exact string matches within a template:

use exact_format::exact_format;

// Basic replacement
let result = exact_format!("Hello {name}", "{name}" => "World");
assert_eq!(result, "Hello World");

// JavaScript-style template string replacement
let user_id = 42;
let user_name = "John";
let result = exact_format!("const user = { id: USERID, name: 'USERNAME' };",
                         "USERID" => user_id.to_string(),
                         "USERNAME" => user_name);
assert_eq!(result, "const user = { id: 42, name: 'John' };");

How It Works

The macro expands to a format! call that handles the replacements. For example:

exact_format!("const user = { id: USERID, name: 'USERNAME' };",
             "USERID" => user_id.to_string(),
             "USERNAME" => user_name);

Expands to:

{
    let __value0__ = user_id.to_string();
    let __value1__ = user_name;
    format!("{}{}{}{}{}",
            "const user = { id: ",
            __value0__,
            ", name: '",
            __value1__,
            "' };"
        )
}

Rules

  1. The first argument must be a string literal
  2. Each replacement key must be a string literal
  3. Replacement values can be any expression that can be formatted with {}
  4. The order of replacements matters when keys overlap
Commit count: 9

cargo fmt