@@ -402,7 +402,7 @@ impl<'a> fmt::Display for Unexpected<'a> {
402402 Bool ( b) => write ! ( formatter, "boolean `{}`" , b) ,
403403 Unsigned ( i) => write ! ( formatter, "integer `{}`" , i) ,
404404 Signed ( i) => write ! ( formatter, "integer `{}`" , i) ,
405- Float ( f) => write ! ( formatter, "floating point `{}`" , f ) ,
405+ Float ( f) => write ! ( formatter, "floating point `{}`" , WithDecimalPoint ( f ) ) ,
406406 Char ( c) => write ! ( formatter, "character `{}`" , c) ,
407407 Str ( s) => write ! ( formatter, "string {:?}" , s) ,
408408 Bytes ( _) => write ! ( formatter, "byte array" ) ,
@@ -2290,3 +2290,36 @@ impl Display for OneOf {
22902290 }
22912291 }
22922292}
2293+
2294+ struct WithDecimalPoint ( f64 ) ;
2295+
2296+ impl Display for WithDecimalPoint {
2297+ fn fmt ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
2298+ struct LookForDecimalPoint < ' f , ' a > {
2299+ formatter : & ' f mut fmt:: Formatter < ' a > ,
2300+ has_decimal_point : bool ,
2301+ }
2302+
2303+ impl < ' f , ' a > fmt:: Write for LookForDecimalPoint < ' f , ' a > {
2304+ fn write_str ( & mut self , fragment : & str ) -> fmt:: Result {
2305+ self . has_decimal_point |= fragment. contains ( '.' ) ;
2306+ self . formatter . write_str ( fragment)
2307+ }
2308+
2309+ fn write_char ( & mut self , ch : char ) -> fmt:: Result {
2310+ self . has_decimal_point |= ch == '.' ;
2311+ self . formatter . write_char ( ch)
2312+ }
2313+ }
2314+
2315+ let mut writer = LookForDecimalPoint {
2316+ formatter,
2317+ has_decimal_point : false ,
2318+ } ;
2319+ tri ! ( write!( writer, "{}" , self . 0 ) ) ;
2320+ if !writer. has_decimal_point {
2321+ tri ! ( formatter. write_str( ".0" ) ) ;
2322+ }
2323+ Ok ( ( ) )
2324+ }
2325+ }
0 commit comments