dynfmt2 - Dynamic Formatting in Rust
A crate for formatting strings dynamically.
dynfmt2is a fork of thedynfmtcrate, which has not been updated since Mar 2021, with some improvements and changes:
- the
lazy_staticdependency is removed, and the code is updated to work usingstd::sync::OnceLockinstead- MSRV is set to Rust 1.70.0 to use
std::sync::OnceLock- Rust 2021 edition
- bumped dependencies
- numerous clippy lints applied
dynfmt2 provides several implementations for formats that implement a subset of the
std::fmt facilities. Parsing of the format string and arguments checks are performed at
runtime. There is also the option to implement new formats.
The public API is exposed via the Format trait, which contains formatting helper functions
and lower-level utilities to interface with format strings. See the Features section for a list
of provided implementations.
Usage
use ;
let formatted = NoopFormat.format;
assert_eq!;
See the Format trait for more methods.
Features
This crate ships with a set of features that either activate formatting capabilities or new format implementations:
json(default): Implements the serialization of complex structures via JSON. Certain formats, such as Python, also have a representation format (%r) that makes use of this feature, if enabled. Without this feature, such values will cause an error.python: Implements theprintf-like format that python 2 used for formatting strings. SeePythonFormatfor more information.curly: A simple format string syntax using curly braces for arguments. Similar to .NET and Rust, but much less capable. SeeSimpleCurlyFormatfor more information.
Extensibility
Implement the Format trait to create a new format. The only required method is iter_args,
which must return an iterator over ArgumentSpec structs. Based on the capabilities of the
format, the specs can be parameterized with formatting parameters.
use MatchIndices;
use ;
;
;
let formatted = HashFormat.format;
assert_eq!;
License: MIT