formati
Enhanced Rust formatting macros with dotted notation and expression interpolation
formati is a collection of procedural macros that extend Rust's standard formatting facilities to handle dotted notation:
format!;
Features
- Dotted notation: Access fields and elements with natural dot notation
- Expression evaluation: Run arbitrary expressions
- Argument deduplication: Simplifies repeated evaluation of the same arguments
- Fully backwards compatible: Works with all standard format specifiers (
{:?},{:.2}, etc.) - Integration wrappers: Drop-in replacements for
std::io(e.g.println!), anyhow, tracing and log.
Installation
Add formati to your Cargo.toml:
[]
= "0.1"
Usage
Basic Formatting
use format;
Format Specifiers
use format;
print! / println!
Requires stdio feature:
[]
= { = "0.1", = ["stdio"] }
use ;
Integration Wrappers
Anyhow
Requires anyhow feature:
[]
= { = "0.1", = ["anyhow"] }
formati-style versions of anyhow macros:
use ;
Log
Requires log feature:
(NOTE: the log feature cannot be enabled together with tracing)
[]
= { = "0.1", = ["log"] }
formati-style versions of log macros:
use ; // instead of log::{…}
use LevelFilter;
Tracing
Requires tracing feature:
(NOTE: the tracing feature cannot be enabled together with log)
[]
= { = "0.1", = ["tracing"] }
formati-style versions of tracing macros:
use ; // use in place of tracing::{debug, error, info, trace}
use FmtSubscriber;
How It Works
The macros processes format strings at compile time to:
- Find placeholders with dotted notation (
{example.field}) - Extract these expressions and deduplicate them
- Replace them with indexed placeholders
- Add the extracted expressions as arguments to the underlying format macro
This approach avoids evaluating the same expression multiple times and makes your format strings more readable.
Backwards compatibility
The macros are all backwards compatible and can be used as a drop-in replacement.
let point = Point ;
let info = format!;
The format! macro would expand to:
must_use
Tests
Test formati::format! macro:
cargo test
Test stdio integration:
cargo test-stdio
Test anyhow integration:
cargo test-anyhow
Test log integration:
cargo test-log
Test tracing integration:
cargo test-tracing
License
This project is licensed under the MIT License.