Skip to content

Commit

Permalink
Make methods to create Unexpected i128/u128 values public
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Oct 21, 2024
1 parent adf5a3f commit 2af38d3
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions serde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,30 @@ pub enum Unexpected<'a> {
Other(&'a str),
}

impl<'a> Unexpected<'a> {
/// The input contained a signed integer `i128` that was not expected.
pub fn invalid_i128<E>(v: i128, expected: &Expected) -> E
where
E: Error,
{
let mut buf = [0u8; 58];
let mut writer = crate::format::Buf::new(&mut buf);
fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as i128", v)).unwrap();
Error::invalid_type(Unexpected::Other(writer.as_str()), expected)
}

/// The input contained an unsigned integer `u128` that was not expected.
pub fn invalid_u128<E>(v: u128, expected: &Expected) -> E
where
E: Error,
{
let mut buf = [0u8; 57];
let mut writer = crate::format::Buf::new(&mut buf);
fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as u128", v)).unwrap();
Error::invalid_type(Unexpected::Other(writer.as_str()), expected)
}
}

impl<'a> fmt::Display for Unexpected<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
use self::Unexpected::*;
Expand Down Expand Up @@ -1372,13 +1396,7 @@ pub trait Visitor<'de>: Sized {
where
E: Error,
{
let mut buf = [0u8; 58];
let mut writer = crate::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,
))
Err(Unexpected::invalid_i128(v, &self))
}

/// The input contains a `u8`.
Expand Down Expand Up @@ -1434,13 +1452,7 @@ pub trait Visitor<'de>: Sized {
where
E: Error,
{
let mut buf = [0u8; 57];
let mut writer = crate::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,
))
Err(Unexpected::invalid_u128(v, &self))
}

/// The input contains an `f32`.
Expand Down

0 comments on commit 2af38d3

Please sign in to comment.