Skip to content

Commit

Permalink
Format Unexpected::Float with decimal point
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jan 26, 2024
1 parent b971ef1 commit bef110b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
35 changes: 34 additions & 1 deletion serde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ impl<'a> fmt::Display for Unexpected<'a> {
Bool(b) => write!(formatter, "boolean `{}`", b),
Unsigned(i) => write!(formatter, "integer `{}`", i),
Signed(i) => write!(formatter, "integer `{}`", i),
Float(f) => write!(formatter, "floating point `{}`", f),
Float(f) => write!(formatter, "floating point `{}`", WithDecimalPoint(f)),
Char(c) => write!(formatter, "character `{}`", c),
Str(s) => write!(formatter, "string {:?}", s),
Bytes(_) => write!(formatter, "byte array"),
Expand Down Expand Up @@ -2290,3 +2290,36 @@ impl Display for OneOf {
}
}
}

struct WithDecimalPoint(f64);

impl Display for WithDecimalPoint {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
struct LookForDecimalPoint<'f, 'a> {
formatter: &'f mut fmt::Formatter<'a>,
has_decimal_point: bool,
}

impl<'f, 'a> fmt::Write for LookForDecimalPoint<'f, 'a> {
fn write_str(&mut self, fragment: &str) -> fmt::Result {
self.has_decimal_point |= fragment.contains('.');
self.formatter.write_str(fragment)
}

fn write_char(&mut self, ch: char) -> fmt::Result {
self.has_decimal_point |= ch == '.';
self.formatter.write_char(ch)
}
}

let mut writer = LookForDecimalPoint {
formatter,
has_decimal_point: false,
};
tri!(write!(writer, "{}", self.0));
if !writer.has_decimal_point {
tri!(formatter.write_str(".0"));
}
Ok(())
}
}
2 changes: 1 addition & 1 deletion serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ mod lib {
pub use self::core::cmp::Reverse;
pub use self::core::convert::{self, From, Into};
pub use self::core::default::{self, Default};
pub use self::core::fmt::{self, Debug, Display};
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite};
pub use self::core::marker::{self, PhantomData};
pub use self::core::num::Wrapping;
pub use self::core::ops::{Bound, Range, RangeFrom, RangeInclusive, RangeTo};
Expand Down
2 changes: 1 addition & 1 deletion test_suite/tests/test_de_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ fn test_number_from_string() {
fn test_integer_from_float() {
assert_de_tokens_error::<isize>(
&[Token::F32(0.0)],
"invalid type: floating point `0`, expected isize",
"invalid type: floating point `0.0`, expected isize",
);
}

Expand Down

0 comments on commit bef110b

Please sign in to comment.