• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-1,295997

.cargo-checksum.jsonH A D03-May-202289 11

.gitignoreH A D07-Dec-201718 32

.travis.ymlH A D08-Dec-201799 98

Cargo.tomlH A D01-Jan-1970845 2421

Cargo.toml.orig-cargoH A D04-Jan-2018321 1210

README.mdH A D07-Dec-20174.4 KiB9980

design.rskH A D07-Dec-20171.3 KiB6451

README.md

1# strfmt: rust library for formatting dynamic strings
2
3> Note: this library is fairly stable and tested, but new features are in the early stages of development and feedback (positive or negative)
4> would be much appreciated. If you use this library and liked it or decided not to use it,
5> please ping me at [@vitiral](https://twitter.com/vitiral) on twitter or vitiral@gmail.com via email to tell me about your
6> experience. I would particularily like to see the code where it is being used. Thankyou!
7
8This library is for rust developers who want to bring rust-like
9formatting to non-static strings.
10
11## Basic use of formatting Display types
12```
13extern crate strfmt;
14use strfmt::strfmt;
15use std::collections::HashMap;
16
17#[test]
18fn it_works() {
19    let mut vars = HashMap::new();
20    vars.insert("name".to_string(), "bob");
21    vars.insert("job".to_string(), "python developer");
22
23    let fmt = "hi, my name is {name} and I am a {job}!".to_string();
24    assert_eq!(strfmt(&fmt, &vars).unwrap(), "hi, my name is bob and I am a python developer!")
25}
26```
27
28In addition to the `strfmt` function, this library has the `Format` trait which adds the
29`format` method to `str` and `String` types.
30
31```
32assert_eq!("hi, my name is {name}".format(&vars), "hi, my name is bob")
33```
34
35You can use this library any time you have dynamic strings you want to format, such as
36if you are providing your users a ui or are reading configuration files.
37
38strfmt does not support empty identifiers (i.e. `{}` or `{:<10}`. Integer identifiers
39will be read as str keys to the hashmap (i.e. `{1:<10}` will have key == "1")
40
41## **BETA**: Formatting numeric types
42> This feature is in Beta and may change. I expect it to be fairly stable
43> at this point but would appreciate feedback on development.
44>
45> In addition, "signed 0 padding" (i.e. +000042) is not yet supported
46> for numeric types
47
48Using `strfmt_map` it is also possible to format integers and floats:
49```
50let mut vars: HashMap<String, f64> = HashMap::new();
51vars.insert("x".to_string(), 42.4242);
52vars.insert("y".to_string(), -100.11111);
53vars.insert("z".to_string(), 0.);
54
55let f = |mut fmt: Formatter| {
56    fmt.f64(*vars.get(fmt.key).unwrap())
57};
58assert_eq!(strfmt_map("{x:<7.2}", f).unwrap(), "42.42  ");
59assert_eq!(strfmt_map("{y:+.2E}", f).unwrap(), "-1.00E2");
60assert_eq!(strfmt_map("{z:+.2E}", f).unwrap(), "+0.00E0");
61```
62
63# Status and Goals
64
65**strfmt** aims to support all of the formatting options defined in
66[`std::fmt`](https://doc.rust-lang.org/std/fmt/). Currently it officially only supports
67the format options for strings (beta support for i64 and f64)
68
69See the [syntax](https://doc.rust-lang.org/std/fmt/#syntax) for how to create a formatted string
70
71### Current Status (in order of priority)
72- [ ]: get strfmt_map out of Beta and create Format.format_map method
73- [ ]: handle sign aware zero padding for numeric types
74- [x]: format any Display type
75- [x]: stabalize `strfmt_map` and add `format_map` to the `Format` trait.
76- [x]: add `f64` method to `Formatter` allowing those using `strfmt_map` to format
77-   f64s according to the spec
78- [x]: add `i64` method to `Formatter` allowing those using `strfmt_map` to format
79-   i64s according to the spec
80- [ ]: add `format_f64(&self, HashMap<String, f64>` method to `Format` allowing users
81-   to easily format a hashmap of i64 values
82- [ ]: add `format_i64(&self, HashMap<String, i64>` method to `Format` allowing users
83-   to easily format a hashmap of i64 values
84- [ ]: look for a rust library has "unbounded float" (like python) and add that to the formatter
85- [ ]: look for a rust library has "unbounded integer" (like python) and add that to the formatter
86- [ ]: Implement `vec` method to `Formatter` allowing those usin `strfmt_map` to format
87-   types of `Vec<Display>` in a way that uses precision and width
88-   (precision will limit the number of elements displayed, width the width of each element)
89- [ ]: special suppport to format HashMap<String, String> for improved speed
90- [ ]: special suppport to format HashMap<String, &str> for improved speed
91- [ ]: special suppport to format HashMap<&str, &str> for improved speed
92
93
94### HELP
95Adding functionality should be fairly easy, the main piece of work is checking and handling
96the flags correctly and creating comprehensive tests. Hopefully I will be creating the `f64`
97method soon to show how it can be done, but I could really use all the help I can get on
98making this libray complete.
99