|
19 | 19 | //! assert_eq!(formatted, format!("{:+05}", 12));
|
20 | 20 | //! # return Ok::<(), interpolator::Error>(())
|
21 | 21 | //! ```
|
22 |
| -//! |
23 |
| -//! # `i` iter format |
24 |
| -//! |
25 |
| -//! The feature `iter` enables an additional format trait `i`, it allows to |
26 |
| -//! format a list of values with a format string and an optional join |
27 |
| -//! expression. |
28 |
| -//! |
29 |
| -//! The syntax is `{list:i(the format string, '{it}' is the array element)(the |
30 |
| -//! optional join)}`, an empty join can also be omitted `{list:i({it})}`. Should |
31 |
| -//! you need to use `)` inside your format string or join, you can add `#` |
32 |
| -//! similar to rust's [raw string](https://doc.rust-lang.org/reference/tokens.html#raw-string-literals). |
33 |
| -//! |
34 |
| -//! It is also possible to only iterate a sub-slice specified through a range |
35 |
| -//! before the format string, i.e. `{list:i1..4({it})}`. For open ranges range |
36 |
| -//! bounds can also be omitted. To index from the end, you can use negative |
37 |
| -//! range bounds. |
38 |
| -//! |
39 |
| -//! A [`Formattable`] implementing iter is created using [`Formattable::iter`]: |
40 |
| -//! |
41 |
| -//! ``` |
42 |
| -//! // HashMap macro |
43 |
| -//! use collection_literals::hash; |
44 |
| -//! use interpolator::{format, Formattable}; |
45 |
| -//! // Needs to be a slice of references so because `Formattable::display` expects a |
46 |
| -//! // reference |
47 |
| -//! let items = [&"hello", &"hi", &"hey"].map(Formattable::display); |
48 |
| -//! let items = Formattable::iter(&items); |
49 |
| -//! let format_str = "Greetings: {items:i..-1(`{it}`)(, )} and {items:i-1..(`{it}`)}"; |
50 |
| -//! assert_eq!( |
51 |
| -//! format(format_str, &hash!("items" => items))?, |
52 |
| -//! "Greetings: `hello`, `hi` and `hey`" |
53 |
| -//! ); |
54 |
| -//! # return Ok::<(), interpolator::Error>(()) |
55 |
| -//! ``` |
56 |
| -//! |
| 22 | +
|
| 23 | +#. |
| 36 | +
|
| 37 | +It is also possible to only iterate a sub-slice specified through a range |
| 38 | +before the format string, i.e. `{list:i1..4({it})}`. For open ranges range |
| 39 | +bounds can also be omitted. To index from the end, you can use negative |
| 40 | +range bounds. |
| 41 | +
|
| 42 | +A [`Formattable`] implementing iter is created using [`Formattable::iter`]: |
| 43 | +
|
| 44 | +``` |
| 45 | +// HashMap macro |
| 46 | +use collection_literals::hash; |
| 47 | +use interpolator::{format, Formattable}; |
| 48 | +// Needs to be a slice of references so because `Formattable::display` expects a |
| 49 | +// reference |
| 50 | +let items = [&"hello", &"hi", &"hey"].map(Formattable::display); |
| 51 | +let items = Formattable::iter(&items); |
| 52 | +let format_str = "Greetings: {items:i..-1(`{it}`)(, )} and {items:i-1..(`{it}`)}"; |
| 53 | +assert_eq!( |
| 54 | + format(format_str, &hash!("items" => items))?, |
| 55 | + "Greetings: `hello`, `hi` and `hey`" |
| 56 | +); |
| 57 | +# return Ok::<(), interpolator::Error>(()) |
| 58 | +```"# |
| 59 | +)] |
| 60 | + |
57 | 61 | //! See [`format`](format()) and [`write`](write()) for details.
|
58 | 62 | //!
|
59 | 63 | //! # Features
|
@@ -209,29 +213,3 @@ pub fn write<'a, K: Borrow<str> + Eq + Hash, F: Borrow<Formattable<'a>>>(
|
209 | 213 | }
|
210 | 214 | Ok(())
|
211 | 215 | }
|
212 |
| - |
213 |
| -#[cfg(test)] |
214 |
| -mod test { |
215 |
| - use collection_literals::hash; |
216 |
| - |
217 |
| - use super::*; |
218 |
| - |
219 |
| - #[test] |
220 |
| - fn iter() { |
221 |
| - let list = &[&1, &5].map(Formattable::display); |
222 |
| - let context = &hash!("h"=> Formattable::iter(list)); |
223 |
| - assert_eq!( |
224 |
| - format("{h:i(`{it:+05}`)#() )#}", context).unwrap(), |
225 |
| - "`+0001`) `+0005`" |
226 |
| - ); |
227 |
| - assert_eq!(format("{h:i(``)}", context).unwrap(), "````"); |
228 |
| - assert_eq!(format("{h:i..({it})}", context).unwrap(), "15"); |
229 |
| - assert_eq!(format("{h:i1..({it})}", context).unwrap(), "5"); |
230 |
| - assert_eq!(format("{h:i1..1({it})}", context).unwrap(), ""); |
231 |
| - assert_eq!(format("{h:i2..1({it})}", context).unwrap(), ""); |
232 |
| - assert_eq!(format("{h:i-1..({it})}", context).unwrap(), "5"); |
233 |
| - assert_eq!(format("{h:i..-1({it})}", context).unwrap(), "1"); |
234 |
| - assert_eq!(format("{h:i..-2({it})}", context).unwrap(), ""); |
235 |
| - assert_eq!(format("{h:i-5..-10({it})}", context).unwrap(), ""); |
236 |
| - } |
237 |
| -} |
0 commit comments