11# dynfmt - Dynamic Formatting in Rust
22
3- ` dynfmt ` provides several implementations for formats that implement a subset of
4- the [ ` std::fmt ` ] facilities. Parsing of the format string and arguments checks
5- are performed at runtime. There is also the option to implement new formats.
3+ A crate for formatting strings dynamically.
64
7- The public API is exposed via the [ ` Format ` ] trait, which contains formatting
8- helper functions and lower-level utilities to interface with format strings. See
9- the Features section for a list of provided implementations.
5+ ` dynfmt ` provides several implementations for formats that implement a subset of the
6+ [ ` std::fmt ` ] facilities. Parsing of the format string and arguments checks are performed at
7+ runtime. There is also the option to implement new formats.
8+
9+ The public API is exposed via the [ ` Format ` ] trait, which contains formatting helper functions
10+ and lower-level utilities to interface with format strings. See the Features section for a list
11+ of provided implementations.
1012
1113## Usage
1214
@@ -17,7 +19,60 @@ let formatted = NoopFormat.format("hello, world", &["unused"]);
1719assert_eq! (" hello, world" , formatted . expect (" formatting failed" ));
1820```
1921
20- See the [ Documentation] ( https://docs.rs/dynfmt ) for more information.
22+ See the [ ` Format ` ] trait for more methods.
23+
24+ ## Features
25+
26+ This crate ships with a set of features that either activate formatting capabilities or new
27+ format implementations:
28+
29+ - ` json ` ** (default)** : Implements the serialization of complex structures via JSON. Certain
30+ formats, such as Python, also have a _ representation_ format (` %r ` ) that makes use of this
31+ feature, if enabled. Without this feature, such values will cause an error.
32+ - ` python ` : Implements the ` printf ` -like format that python 2 used for formatting strings. See
33+ [ ` PythonFormat ` ] for more information.
34+ - ` curly ` : A simple format string syntax using curly braces for arguments. Similar to .NET and
35+ Rust, but much less capable. See [ ` SimpleCurlyFormat ` ] for mor information.
36+
37+ ## Extensibility
38+
39+ Implement the [ ` Format ` ] trait to create a new format. The only required method is ` iter_args ` ,
40+ which must return an iterator over [ ` ArgumentSpec ` ] structs. Based on the capabilities of the
41+ format, the specs can be parameterized with formatting parameters.
42+
43+ ``` rust
44+ use std :: str :: MatchIndices ;
45+ use dynfmt :: {ArgumentSpec , Format , Error };
46+
47+ struct HashFormat ;
48+
49+ impl <'f > Format <'f > for HashFormat {
50+ type Iter = HashIter <'f >;
51+
52+ fn iter_args (& self , format : & 'f str ) -> Result <Self :: Iter , Error <'f >> {
53+ Ok (HashIter (format . match_indices ('#' )))
54+ }
55+ }
56+
57+ struct HashIter <'f >(MatchIndices <'f , char >);
58+
59+ impl <'f > Iterator for HashIter <'f > {
60+ type Item = Result <ArgumentSpec <'f >, Error <'f >>;
61+
62+ fn next (& mut self ) -> Option <Self :: Item > {
63+ self . 0. next (). map (| (index , _ )| Ok (ArgumentSpec :: new (index , index + 1 )))
64+ }
65+ }
66+
67+ let formatted = HashFormat . format (" hello, #" , & [" world" ]);
68+ assert_eq! (" hello, world" , formatted . expect (" formatting failed" ));
69+ ```
2170
2271[ `std::fmt` ] : https://doc.rust-lang.org/stable/std/fmt/
23- [ `format` ] : https://docs.rs/dynfmt/latest/dynfmt/trait.Format.html
72+ [ `serde::Serialize` ] : https://docs.rs/serde/latest/serde/trait.Serialize.html
73+ [ `Format` ] : https://docs.rs/dynfmt/latest/dynfmt/trait.Format.html
74+ [ `ArgumentSpec` ] : https://docs.rs/dynfmt/latest/dynfmt/struct.ArgumentSpec.html
75+ [ `PythonFormat` ] : https://docs.rs/dynfmt/latest/dynfmt/python/struct.PythonFormat.html
76+ [ `SimpleCurlyFormat` ] : https://docs.rs/dynfmt/latest/dynfmt/curly/struct.SimpleCurlyFormat.html
77+
78+ License: MIT
0 commit comments