From 0d71ac84b57428bc5959f068fa3369929d2c1646 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 25 Jan 2022 12:04:24 -0800 Subject: [PATCH] Render 128-bit integer value into Visitor errors --- serde/src/de/format.rs | 30 ++++++++++++++++++++++++++++++ serde/src/de/mod.rs | 14 ++++++++++---- test_suite/tests/test_de_error.rs | 4 ++-- 3 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 serde/src/de/format.rs diff --git a/serde/src/de/format.rs b/serde/src/de/format.rs new file mode 100644 index 000000000..58ec0968d --- /dev/null +++ b/serde/src/de/format.rs @@ -0,0 +1,30 @@ +use lib::fmt::{self, Write}; +use lib::str; + +pub struct Buf<'a> { + bytes: &'a mut [u8], + offset: usize, +} + +impl<'a> Buf<'a> { + pub fn new(bytes: &'a mut [u8]) -> Self { + Buf { bytes, offset: 0 } + } + + pub fn as_str(&self) -> &str { + let slice = &self.bytes[..self.offset]; + unsafe { str::from_utf8_unchecked(slice) } + } +} + +impl<'a> Write for Buf<'a> { + fn write_str(&mut self, s: &str) -> fmt::Result { + if self.offset + s.len() > self.bytes.len() { + Err(fmt::Error) + } else { + self.bytes[self.offset..self.offset + s.len()].copy_from_slice(s.as_bytes()); + self.offset += s.len(); + Ok(()) + } + } +} diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index db6644daa..4ae477386 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -118,6 +118,8 @@ use lib::*; pub mod value; +#[cfg(not(no_integer128))] +mod format; mod ignored_any; mod impls; mod utf8; @@ -1366,8 +1368,10 @@ pub trait Visitor<'de>: Sized { where E: Error, { - let _ = v; - Err(Error::invalid_type(Unexpected::Other("i128"), &self)) + let mut buf = [0u8; 58]; + let mut writer = format::Buf::new(&mut buf); + fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as i128", v)).unwrap(); + Err(Error::invalid_type(Unexpected::Other(writer.as_str()), &self)) } } @@ -1426,8 +1430,10 @@ pub trait Visitor<'de>: Sized { where E: Error, { - let _ = v; - Err(Error::invalid_type(Unexpected::Other("u128"), &self)) + let mut buf = [0u8; 57]; + let mut writer = format::Buf::new(&mut buf); + fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as u128", v)).unwrap(); + Err(Error::invalid_type(Unexpected::Other(writer.as_str()), &self)) } } diff --git a/test_suite/tests/test_de_error.rs b/test_suite/tests/test_de_error.rs index adc6fc16f..d37c20c91 100644 --- a/test_suite/tests/test_de_error.rs +++ b/test_suite/tests/test_de_error.rs @@ -192,7 +192,7 @@ fn test_i128() { let error = <&str>::deserialize(deserializer).unwrap_err(); assert_eq!( error.to_string(), - "invalid type: i128, expected a borrowed string", + "invalid type: integer `1` as i128, expected a borrowed string", ); } @@ -372,7 +372,7 @@ fn test_u128() { let error = <&str>::deserialize(deserializer).unwrap_err(); assert_eq!( error.to_string(), - "invalid type: u128, expected a borrowed string", + "invalid type: integer `1` as u128, expected a borrowed string", ); }