Dynamic String Formatting for Rust
The dyf crate brings dynamic string formatting to Rust while supporting the whole variety of string formats available in Rust.
It provides an easy way to implement dynamic formatting for custom types with the implementation of the DynDisplay trait.
Features
- Support for (almost) all standard Rust format specifiers
- Dynamic formatting for custom types via the
DynDisplaytrait - Macro support for convenient usage
- Support for various standard library types
Usage
Add the crate to your project:
Serde Support
The dyf crate provides optional support for serialization and deserialization using the serde crate.
To enable this feature, add the serde feature when adding the crate to your project:
Once the serde feature is enabled, the FormatString structure derives the Serialize and Deserialize traits.
This allows you to easily serialize and deserialize FormatString instances using the serde crate.
Basic Formatting
use ;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
let num_fmt = from_string.unwrap;
let num = 42;
let result = dformat!.unwrap;
assert_eq!;
Advanced Formatting
use ;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
Custom Type Formatting
use ;
let point = Point ;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
Integer Formatting
use ;
// Decimal formatting
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
// Hexadecimal formatting
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
// Octal formatting
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
// Binary formatting
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
Float Formatting
use ;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
String Formatting
use ;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
let fmt = from_string.unwrap;
let result = dformat!.unwrap;
assert_eq!;
Supported Format Specifiers
The crate supports several standard Rust format specifiers, including:
| Category | Specifiers |
|---|---|
| Fill/Alignment | < > ^ |
| Sign | + - |
| Alternate | # |
| Zero-padding | 0 |
| Width | {:5} |
| Precision | {:.2} |
| Type | ? x X o b e E p |
Performance Considerations
The crate is designed with performance in mind. The FormatString can be created once and reused multiple times with different arguments.
This is particularly useful in scenarios where the same format string is used repeatedly.
use ;
let fmt = from_string.unwrap;
let result1 = dformat!.unwrap;
let result2 = dformat!.unwrap;
assert_eq!;
assert_eq!;
Limitations
While this crate aims to support all standard Rust format specifiers, there might be some edge cases that are not yet covered. If you encounter any unsupported format specifiers or have suggestions for improvements, please open an issue on the GitHub repository.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.
License
This project is licensed under the GNU General Public License v3.0 (GPL-3.0). By using this software, you agree to the terms and conditions of this license.
The full license text is available in the LICENSE file in the project root or at: https://www.gnu.org/licenses/gpl-3.0.html