diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index b86ebe5ed..99df983aa 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -292,6 +292,12 @@ macro_rules! declare_error_trait { fn duplicate_field(field: &'static str) -> Self { Error::custom(format_args!("duplicate field `{}`", field)) } + + /// Raised when trying to deserialize type that not supported. + #[cold] + fn unsupported(ty: &'static str) -> Self { + Error::custom(format_args!("{} is not supported", ty)) + } } } } @@ -927,32 +933,56 @@ pub trait Deserializer<'de>: Sized { /// many others. fn deserialize_any(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("any")) + } /// Hint that the `Deserialize` type is expecting a `bool` value. fn deserialize_bool(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("bool")) + } /// Hint that the `Deserialize` type is expecting an `i8` value. fn deserialize_i8(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("i8")) + } /// Hint that the `Deserialize` type is expecting an `i16` value. fn deserialize_i16(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("i16")) + } /// Hint that the `Deserialize` type is expecting an `i32` value. fn deserialize_i32(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("i32")) + } /// Hint that the `Deserialize` type is expecting an `i64` value. fn deserialize_i64(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("i64")) + } /// Hint that the `Deserialize` type is expecting an `i128` value. /// @@ -962,28 +992,44 @@ pub trait Deserializer<'de>: Sized { V: Visitor<'de>, { let _ = visitor; - Err(Error::custom("i128 is not supported")) + Err(Error::unsupported("i128")) } /// Hint that the `Deserialize` type is expecting a `u8` value. fn deserialize_u8(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("u8")) + } /// Hint that the `Deserialize` type is expecting a `u16` value. fn deserialize_u16(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("u16")) + } /// Hint that the `Deserialize` type is expecting a `u32` value. fn deserialize_u32(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("u32")) + } /// Hint that the `Deserialize` type is expecting a `u64` value. fn deserialize_u64(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("u64")) + } /// Hint that the `Deserialize` type is expecting an `u128` value. /// @@ -993,23 +1039,35 @@ pub trait Deserializer<'de>: Sized { V: Visitor<'de>, { let _ = visitor; - Err(Error::custom("u128 is not supported")) + Err(Error::unsupported("u128")) } /// Hint that the `Deserialize` type is expecting a `f32` value. fn deserialize_f32(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("f32")) + } /// Hint that the `Deserialize` type is expecting a `f64` value. fn deserialize_f64(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("f64")) + } /// Hint that the `Deserialize` type is expecting a `char` value. fn deserialize_char(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("char")) + } /// Hint that the `Deserialize` type is expecting a string value and does /// not benefit from taking ownership of buffered data owned by the @@ -1020,7 +1078,10 @@ pub trait Deserializer<'de>: Sized { /// instead. fn deserialize_str(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + self.deserialize_string(visitor) + } /// Hint that the `Deserialize` type is expecting a string value and would /// benefit from taking ownership of buffered data owned by the @@ -1031,7 +1092,11 @@ pub trait Deserializer<'de>: Sized { /// instead. fn deserialize_string(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("string")) + } /// Hint that the `Deserialize` type is expecting a byte array and does not /// benefit from taking ownership of buffered data owned by the @@ -1042,7 +1107,11 @@ pub trait Deserializer<'de>: Sized { /// instead. fn deserialize_bytes(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("bytes slice")) + } /// Hint that the `Deserialize` type is expecting a byte array and would /// benefit from taking ownership of buffered data owned by the @@ -1053,7 +1122,11 @@ pub trait Deserializer<'de>: Sized { /// instead. fn deserialize_byte_buf(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + self.deserialize_bytes(visitor) + } /// Hint that the `Deserialize` type is expecting an optional value. /// @@ -1062,12 +1135,20 @@ pub trait Deserializer<'de>: Sized { /// `Some(value)`. fn deserialize_option(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("optional value")) + } /// Hint that the `Deserialize` type is expecting a unit value. fn deserialize_unit(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("unit value")) + } /// Hint that the `Deserialize` type is expecting a unit struct with a /// particular name. @@ -1077,7 +1158,11 @@ pub trait Deserializer<'de>: Sized { visitor: V, ) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = (name, visitor); + Err(Error::unsupported("unit struct")) + } /// Hint that the `Deserialize` type is expecting a newtype struct with a /// particular name. @@ -1087,18 +1172,30 @@ pub trait Deserializer<'de>: Sized { visitor: V, ) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = (name, visitor); + Err(Error::unsupported("newtype struct")) + } /// Hint that the `Deserialize` type is expecting a sequence of values. fn deserialize_seq(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("sequence")) + } /// Hint that the `Deserialize` type is expecting a sequence of values and /// knows how many values there are without looking at the serialized data. fn deserialize_tuple(self, len: usize, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = (len, visitor); + Err(Error::unsupported("tuple")) + } /// Hint that the `Deserialize` type is expecting a tuple struct with a /// particular name and number of fields. @@ -1109,12 +1206,20 @@ pub trait Deserializer<'de>: Sized { visitor: V, ) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = (name, len, visitor); + Err(Error::unsupported("tuple struct")) + } /// Hint that the `Deserialize` type is expecting a map of key-value pairs. fn deserialize_map(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("map")) + } /// Hint that the `Deserialize` type is expecting a struct with a particular /// name and fields. @@ -1125,7 +1230,11 @@ pub trait Deserializer<'de>: Sized { visitor: V, ) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = (name, fields, visitor); + Err(Error::unsupported("struct")) + } /// Hint that the `Deserialize` type is expecting an enum value with a /// particular name and possible variants. @@ -1136,13 +1245,21 @@ pub trait Deserializer<'de>: Sized { visitor: V, ) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = (name, variants, visitor); + Err(Error::unsupported("enum")) + } /// Hint that the `Deserialize` type is expecting the name of a struct /// field or the discriminant of an enum variant. fn deserialize_identifier(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("identifier hint")) + } /// Hint that the `Deserialize` type needs to deserialize a value whose type /// doesn't matter because it is ignored. @@ -1150,7 +1267,11 @@ pub trait Deserializer<'de>: Sized { /// Deserializers for non-self-describing formats may not support this mode. fn deserialize_ignored_any(self, visitor: V) -> Result where - V: Visitor<'de>; + V: Visitor<'de>, + { + let _ = visitor; + Err(Error::unsupported("ignored any")) + } /// Determine whether `Deserialize` implementations should expect to /// deserialize their human-readable form. diff --git a/serde/src/ser/mod.rs b/serde/src/ser/mod.rs index fb0033ec0..03f2a7d0c 100644 --- a/serde/src/ser/mod.rs +++ b/serde/src/ser/mod.rs @@ -178,6 +178,11 @@ macro_rules! declare_error_trait { fn custom(msg: T) -> Self where T: Display; + + /// Raised when trying to serialize type that not supported. + fn unsupported(ty: &'static str) -> Self { + Self::custom(format_args!("{} is not supported", ty)) + } } } } @@ -409,7 +414,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_bool(self, v: bool) -> Result; + fn serialize_bool(self, v: bool) -> Result { + let _ = v; + Err(Error::unsupported("bool")) + } /// Serialize an `i8` value. /// @@ -431,7 +439,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_i8(self, v: i8) -> Result; + fn serialize_i8(self, v: i8) -> Result { + let _ = v; + Err(Error::unsupported("i8")) + } /// Serialize an `i16` value. /// @@ -453,7 +464,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_i16(self, v: i16) -> Result; + fn serialize_i16(self, v: i16) -> Result { + let _ = v; + Err(Error::unsupported("i16")) + } /// Serialize an `i32` value. /// @@ -475,7 +489,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_i32(self, v: i32) -> Result; + fn serialize_i32(self, v: i32) -> Result { + let _ = v; + Err(Error::unsupported("i32")) + } /// Serialize an `i64` value. /// @@ -493,7 +510,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_i64(self, v: i64) -> Result; + fn serialize_i64(self, v: i64) -> Result { + let _ = v; + Err(Error::unsupported("i64")) + } /// Serialize an `i128` value. /// @@ -515,7 +535,7 @@ pub trait Serializer: Sized { /// The default behavior unconditionally returns an error. fn serialize_i128(self, v: i128) -> Result { let _ = v; - Err(Error::custom("i128 is not supported")) + Err(Error::unsupported("i128")) } /// Serialize a `u8` value. @@ -538,7 +558,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_u8(self, v: u8) -> Result; + fn serialize_u8(self, v: u8) -> Result { + let _ = v; + Err(Error::unsupported("u8")) + } /// Serialize a `u16` value. /// @@ -560,7 +583,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_u16(self, v: u16) -> Result; + fn serialize_u16(self, v: u16) -> Result { + let _ = v; + Err(Error::unsupported("u16")) + } /// Serialize a `u32` value. /// @@ -582,7 +608,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_u32(self, v: u32) -> Result; + fn serialize_u32(self, v: u32) -> Result { + let _ = v; + Err(Error::unsupported("u32")) + } /// Serialize a `u64` value. /// @@ -600,7 +629,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_u64(self, v: u64) -> Result; + fn serialize_u64(self, v: u64) -> Result { + let _ = v; + Err(Error::unsupported("u64")) + } /// Serialize a `u128` value. /// @@ -622,7 +654,7 @@ pub trait Serializer: Sized { /// The default behavior unconditionally returns an error. fn serialize_u128(self, v: u128) -> Result { let _ = v; - Err(Error::custom("u128 is not supported")) + Err(Error::unsupported("u128")) } /// Serialize an `f32` value. @@ -645,7 +677,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_f32(self, v: f32) -> Result; + fn serialize_f32(self, v: f32) -> Result { + let _ = v; + Err(Error::unsupported("f32")) + } /// Serialize an `f64` value. /// @@ -663,7 +698,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_f64(self, v: f64) -> Result; + fn serialize_f64(self, v: f64) -> Result { + let _ = v; + Err(Error::unsupported("f64")) + } /// Serialize a character. /// @@ -684,7 +722,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_char(self, v: char) -> Result; + fn serialize_char(self, v: char) -> Result { + let _ = v; + Err(Error::unsupported("char")) + } /// Serialize a `&str`. /// @@ -702,7 +743,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_str(self, v: &str) -> Result; + fn serialize_str(self, v: &str) -> Result { + let _ = v; + Err(Error::unsupported("str")) + } /// Serialize a chunk of raw byte data. /// @@ -737,7 +781,10 @@ pub trait Serializer: Sized { /// # } /// # } /// ``` - fn serialize_bytes(self, v: &[u8]) -> Result; + fn serialize_bytes(self, v: &[u8]) -> Result { + let _ = v; + Err(Error::unsupported("bytes slice")) + } /// Serialize a [`None`] value. /// @@ -770,7 +817,9 @@ pub trait Serializer: Sized { /// ``` /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None - fn serialize_none(self) -> Result; + fn serialize_none(self) -> Result { + Err(Error::unsupported("none value")) + } /// Serialize a [`Some(T)`] value. /// @@ -805,7 +854,11 @@ pub trait Serializer: Sized { /// [`Some(T)`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some fn serialize_some(self, value: &T) -> Result where - T: ?Sized + Serialize; + T: ?Sized + Serialize, + { + let _ = value; + Err(Error::unsupported("Some(T) value")) + } /// Serialize a `()` value. /// @@ -823,7 +876,9 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_unit(self) -> Result; + fn serialize_unit(self) -> Result { + Err(Error::unsupported("unit value")) + } /// Serialize a unit struct like `struct Unit` or `PhantomData`. /// @@ -843,7 +898,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_unit_struct(self, name: &'static str) -> Result; + fn serialize_unit_struct(self, name: &'static str) -> Result { + let _ = name; + Err(Error::unsupported("unit struct")) + } /// Serialize a unit variant like `E::A` in `enum E { A, B }`. /// @@ -876,7 +934,10 @@ pub trait Serializer: Sized { name: &'static str, variant_index: u32, variant: &'static str, - ) -> Result; + ) -> Result { + let _ = (name, variant_index, variant); + Err(Error::unsupported("unit variant")) + } /// Serialize a newtype struct like `struct Millimeters(u8)`. /// @@ -904,7 +965,11 @@ pub trait Serializer: Sized { value: &T, ) -> Result where - T: ?Sized + Serialize; + T: ?Sized + Serialize, + { + let _ = (name, value); + Err(Error::unsupported("newtype struct")) + } /// Serialize a newtype variant like `E::N` in `enum E { N(u8) }`. /// @@ -940,7 +1005,11 @@ pub trait Serializer: Sized { value: &T, ) -> Result where - T: ?Sized + Serialize; + T: ?Sized + Serialize, + { + let _ = (name, variant_index, variant, value); + Err(Error::unsupported("newtype variant")) + } /// Begin to serialize a variably sized sequence. This call must be /// followed by zero or more calls to `serialize_element`, then a call to @@ -988,7 +1057,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_seq(self, len: Option) -> Result; + fn serialize_seq(self, len: Option) -> Result { + let _ = len; + Err(Error::unsupported("sequence")) + } /// Begin to serialize a statically sized sequence whose length will be /// known at deserialization time without looking at the serialized data. @@ -1044,7 +1116,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_tuple(self, len: usize) -> Result; + fn serialize_tuple(self, len: usize) -> Result { + let _ = len; + Err(Error::unsupported("tuple value")) + } /// Begin to serialize a tuple struct like `struct Rgb(u8, u8, u8)`. This /// call must be followed by zero or more calls to `serialize_field`, then a @@ -1075,7 +1150,10 @@ pub trait Serializer: Sized { self, name: &'static str, len: usize, - ) -> Result; + ) -> Result { + let _ = (name, len); + Err(Error::unsupported("tuple struct")) + } /// Begin to serialize a tuple variant like `E::T` in `enum E { T(u8, u8) /// }`. This call must be followed by zero or more calls to @@ -1122,7 +1200,10 @@ pub trait Serializer: Sized { variant_index: u32, variant: &'static str, len: usize, - ) -> Result; + ) -> Result { + let _ = (name, variant_index, variant, len); + Err(Error::unsupported("tuple variant")) + } /// Begin to serialize a map. This call must be followed by zero or more /// calls to `serialize_key` and `serialize_value`, then a call to `end`. @@ -1170,7 +1251,10 @@ pub trait Serializer: Sized { /// } /// } /// ``` - fn serialize_map(self, len: Option) -> Result; + fn serialize_map(self, len: Option) -> Result { + let _ = len; + Err(Error::unsupported("map")) + } /// Begin to serialize a struct like `struct Rgb { r: u8, g: u8, b: u8 }`. /// This call must be followed by zero or more calls to `serialize_field`, @@ -1206,7 +1290,10 @@ pub trait Serializer: Sized { self, name: &'static str, len: usize, - ) -> Result; + ) -> Result { + let _ = (name, len); + Err(Error::unsupported("struct")) + } /// Begin to serialize a struct variant like `E::S` in `enum E { S { r: u8, /// g: u8, b: u8 } }`. This call must be followed by zero or more calls to @@ -1252,7 +1339,10 @@ pub trait Serializer: Sized { variant_index: u32, variant: &'static str, len: usize, - ) -> Result; + ) -> Result { + let _ = (name, variant_index, variant, len); + Err(Error::unsupported("struct variant")) + } /// Collect an iterator as a sequence. ///