@@ -4,7 +4,7 @@ fn write_iter(
44 out : & mut impl Write ,
55 value : & [ Formattable < ' _ > ] ,
66 range : Option < Range > ,
7- format : & str ,
7+ format : Option < & str > ,
88 join : Option < & str > ,
99) -> Result {
1010 if value. is_empty ( ) {
@@ -21,18 +21,31 @@ fn write_iter(
2121 . min ( rhs) ;
2222
2323 if rhs > lhs {
24- for value in & value[ lhs..rhs - 1 ] {
25- let mut context = HashMap :: new ( ) ;
26- context. insert ( "it" , value) ;
27- write ( out, format, & context) ?;
28- if let Some ( join) = join {
29- write ! ( out, "{join}" ) . map_err ( |e| Error :: Fmt ( e, 0 ) ) ?;
24+ if let Some ( format) = format {
25+ for value in & value[ lhs..rhs - 1 ] {
26+ let mut context = HashMap :: new ( ) ;
27+ context. insert ( "" , value) ;
28+ write ( out, format, & context) ?;
29+ if let Some ( join) = join {
30+ write ! ( out, "{join}" ) . map_err ( |e| Error :: Fmt ( e, 0 ) ) ?;
31+ }
32+ }
33+ if let Some ( value) = value[ ..rhs] . last ( ) {
34+ let mut context = HashMap :: new ( ) ;
35+ context. insert ( "" , value) ;
36+ write ( out, format, & context) ?;
37+ }
38+ } else {
39+ for value in & value[ lhs..rhs] {
40+ write ! (
41+ out,
42+ "{}" ,
43+ value
44+ . get_display( )
45+ . map_err( |e| Error :: MissingTraitImpl ( e, 0 ) ) ?
46+ )
47+ . map_err ( |e| Error :: Fmt ( e, 0 ) ) ?;
3048 }
31- }
32- if let Some ( value) = value[ ..rhs] . last ( ) {
33- let mut context = HashMap :: new ( ) ;
34- context. insert ( "it" , value) ;
35- write ( out, format, & context) ?;
3649 }
3750 }
3851 Ok ( ( ) )
@@ -49,7 +62,7 @@ pub(crate) fn iter(
4962 hash : bool ,
5063 zero : bool ,
5164 range : Option < Range > ,
52- format : & str ,
65+ format : Option < & str > ,
5366 join : Option < & str > ,
5467) -> Result {
5568 match ( precision, sign, hash, zero) {
@@ -82,17 +95,24 @@ mod test {
8295 let list = & [ & 1 , & 5 ] . map ( Formattable :: display) ;
8396 let context = & hash ! ( "h" => Formattable :: iter( list) ) ;
8497 assert_eq ! (
85- format( "{h:i(`{it :+05}`)#() )#}" , context) . unwrap( ) ,
98+ format( "{h:i(`{:+05}`)#() )#}" , context) . unwrap( ) ,
8699 "`+0001`) `+0005`"
87100 ) ;
88101 assert_eq ! ( format( "{h:i(``)}" , context) . unwrap( ) , "````" ) ;
89- assert_eq ! ( format( "{h:i..({it})}" , context) . unwrap( ) , "15" ) ;
90- assert_eq ! ( format( "{h:i1..({it})}" , context) . unwrap( ) , "5" ) ;
91- assert_eq ! ( format( "{h:i1..1({it})}" , context) . unwrap( ) , "" ) ;
92- assert_eq ! ( format( "{h:i2..1({it})}" , context) . unwrap( ) , "" ) ;
93- assert_eq ! ( format( "{h:i-1..({it})}" , context) . unwrap( ) , "5" ) ;
94- assert_eq ! ( format( "{h:i..-1({it})}" , context) . unwrap( ) , "1" ) ;
95- assert_eq ! ( format( "{h:i..-2({it})}" , context) . unwrap( ) , "" ) ;
96- assert_eq ! ( format( "{h:i-5..-10({it})}" , context) . unwrap( ) , "" ) ;
102+ assert_eq ! ( format( "{h:i..({})}" , context) . unwrap( ) , "15" ) ;
103+ assert_eq ! ( format( "{h:i1..({})}" , context) . unwrap( ) , "5" ) ;
104+ assert_eq ! ( format( "{h:i1..1({})}" , context) . unwrap( ) , "" ) ;
105+ assert_eq ! ( format( "{h:i2..1({})}" , context) . unwrap( ) , "" ) ;
106+ assert_eq ! ( format( "{h:i-1..({})}" , context) . unwrap( ) , "5" ) ;
107+ assert_eq ! ( format( "{h:i..-1({})}" , context) . unwrap( ) , "1" ) ;
108+ assert_eq ! ( format( "{h:i..-2({})}" , context) . unwrap( ) , "" ) ;
109+ assert_eq ! ( format( "{h:i-5..-10({})}" , context) . unwrap( ) , "" ) ;
110+ assert_eq ! ( format( "{h:i-1({})}" , context) . unwrap( ) , "5" ) ;
111+ assert_eq ! ( format( "{h:i1}" , context) . unwrap( ) , "5" ) ;
112+ assert_eq ! ( format( "{h:i..}" , context) . unwrap( ) , "15" ) ;
113+ assert_eq ! ( format( "{h:i}" , context) . unwrap( ) , "15" ) ;
114+ assert_eq ! ( format( "{h:i..1}" , context) . unwrap( ) , "1" ) ;
115+ assert_eq ! ( format( "{h:i1..}" , context) . unwrap( ) , "5" ) ;
116+ assert_eq ! ( format( "{h:i..=-1}" , context) . unwrap( ) , "15" ) ;
97117 }
98118}
0 commit comments