Skip to content

Commit d438c2d

Browse files
authored
Merge pull request #2682 from dtolnay/decimalpoint
Format Unexpected::Float with decimal point
2 parents b971ef1 + bef110b commit d438c2d

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

serde/src/de/mod.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
}

serde/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ mod lib {
182182
pub use self::core::cmp::Reverse;
183183
pub use self::core::convert::{self, From, Into};
184184
pub use self::core::default::{self, Default};
185-
pub use self::core::fmt::{self, Debug, Display};
185+
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite};
186186
pub use self::core::marker::{self, PhantomData};
187187
pub use self::core::num::Wrapping;
188188
pub use self::core::ops::{Bound, Range, RangeFrom, RangeInclusive, RangeTo};

test_suite/tests/test_de_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ fn test_number_from_string() {
14341434
fn test_integer_from_float() {
14351435
assert_de_tokens_error::<isize>(
14361436
&[Token::F32(0.0)],
1437-
"invalid type: floating point `0`, expected isize",
1437+
"invalid type: floating point `0.0`, expected isize",
14381438
);
14391439
}
14401440

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy