Debian Codegraph @

README.md

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