strfmt: rust library for formatting dynamic strings
Note: this library is fairly stable and tested, but new features are in the early stages of development and feedback (positive or negative) would be much appreciated. If you use this library and liked it or decided not to use it, please ping me at @vitiral on twitter or vitiral@gmail.com via email to tell me about your experience. I would particularily like to see the code where it is being used. Thankyou!
This library is for rust developers who want to bring rust-like formatting to non-static strings.
Basic use of formatting Display types
extern crate strfmt;
use strfmt;
use HashMap;
In addition to the strfmt function, this library has the Format trait which adds the
format method to str and String types.
assert_eq!
You can use this library any time you have dynamic strings you want to format, such as if you are providing your users a ui or are reading configuration files.
strfmt does not support empty identifiers (i.e. {} or {:<10}. Integer identifiers
will be read as str keys to the hashmap (i.e. {1:<10} will have key == "1")
Legacy
In the 0.2.0 update, the signature of strfmt and Format::format changed to fix a bug with numeric formatting.
For easy migration the strfmt_display and Format::format_dispaly function provide the old behaviour.
BETA: Formatting numeric types
This feature is in Beta and may change. I expect it to be fairly stable at this point but would appreciate feedback on development.
In addition, "signed 0 padding" (i.e. +000042) is not yet supported for numeric types
Using strfmt_map it is also possible to format integers and floats:
let mut vars: = new;
vars.insert;
vars.insert;
vars.insert;
let f = ;
assert_eq!;
assert_eq!;
assert_eq!;
Status and Goals
strfmt aims to support all of the formatting options defined in
std::fmt. Currently it officially only supports
the format options for strings (beta support for i64 and f64)
See the syntax for how to create a formatted string
Current Status (in order of priority)
- get strfmt_map out of Beta and create Format.format_map method
- handle sign aware zero padding for numeric types
- format any Display type
- stabilize
strfmt_mapand addformat_mapto theFormattrait. - add
f64method toFormatterallowing those usingstrfmt_mapto format f64s according to the spec - add
i64method toFormatterallowing those usingstrfmt_mapto format i64s according to the spec - use DisplayStr trait for formatting, permitting proper formatting of integer types.
- look for a rust library has "unbounded float" (like python) and add that to the formatter
- look for a rust library has "unbounded integer" (like python) and add that to the formatter
- Implement
vecmethod toFormatterallowing those usinstrfmt_mapto format types ofVec<Display>in a way that uses precision and width (precision will limit the number of elements displayed, width the width of each element) - special suppport to format HashMap<String, String> for improved speed
- special suppport to format HashMap<String, &str> for improved speed
- special suppport to format HashMap<&str, &str> for improved speed
HELP
Adding functionality should be fairly easy, the main piece of work is checking and handling
the flags correctly and creating comprehensive tests. Hopefully I will be creating the f64
method soon to show how it can be done, but I could really use all the help I can get on
making this libray complete.