mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-23 07:08:02 +00:00
Compare commits
35 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9ecb0839de | |||
| 8a4c116812 | |||
| 1d3e921ba6 | |||
| 4fdba725fe | |||
| 75eed8cdde | |||
| 6801a13650 | |||
| 25ab84d4b9 | |||
| e43d3f3e4f | |||
| b37d47c987 | |||
| eec7101894 | |||
| fd3d1396d3 | |||
| c47b4c8e0b | |||
| 2d793b82f6 | |||
| 237be46e29 | |||
| 3d7aad1e7b | |||
| e792874369 | |||
| 1669c69714 | |||
| 4d5e450054 | |||
| 26b22e647d | |||
| cda1fc46b0 | |||
| c68b959696 | |||
| eab80172e4 | |||
| c1259fbc87 | |||
| 58e30eaee4 | |||
| bafa941004 | |||
| f347b2d363 | |||
| 3f9fc49cca | |||
| c913527944 | |||
| 8fafc7420c | |||
| bea1c5b0f5 | |||
| aa37caf216 | |||
| 2440b59aae | |||
| 873cfbe9ab | |||
| c96efcb87a | |||
| b53026a21b |
+4
-11
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde"
|
||||
version = "1.0.1" # remember to update html_root_url
|
||||
version = "1.0.9" # remember to update html_root_url
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "A generic serialization/deserialization framework"
|
||||
@@ -48,22 +48,15 @@ std = []
|
||||
# https://github.com/serde-rs/serde/issues/812
|
||||
unstable = []
|
||||
|
||||
# Provide impls for types that require memory allocation like Box<T> and Rc<T>.
|
||||
# This is a subset of std but may be enabled without depending on all of std.
|
||||
# Provide impls for types in the Rust core allocation and collections library
|
||||
# including String, Box<T>, Vec<T>, and Cow<T>. This is a subset of std but may
|
||||
# be enabled without depending on all of std.
|
||||
#
|
||||
# Requires a dependency on the unstable core allocation library:
|
||||
#
|
||||
# https://doc.rust-lang.org/alloc/
|
||||
alloc = ["unstable"]
|
||||
|
||||
# Provide impls for collection types like String and Cow<T>. This is a subset of
|
||||
# std but may be enabled without depending on all of std.
|
||||
#
|
||||
# Requires a dependency on the unstable collections library:
|
||||
#
|
||||
# https://doc.rust-lang.org/collections/
|
||||
collections = ["alloc"]
|
||||
|
||||
# Opt into impls for Rc<T> and Arc<T>. Serializing and deserializing these types
|
||||
# does not preserve identity and may result in multiple copies of the same data.
|
||||
# Be sure that this is what you want before enabling this feature.
|
||||
|
||||
+54
-16
@@ -11,12 +11,12 @@ use lib::*;
|
||||
use de::{Deserialize, Deserializer, EnumAccess, Error, SeqAccess, Unexpected, VariantAccess,
|
||||
Visitor};
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
use de::MapAccess;
|
||||
|
||||
use de::from_primitive::FromPrimitive;
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
use private::de::size_hint;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -208,10 +208,10 @@ impl<'de> Deserialize<'de> for char {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
struct StringVisitor;
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de> Visitor<'de> for StringVisitor {
|
||||
type Value = String;
|
||||
|
||||
@@ -254,7 +254,7 @@ impl<'de> Visitor<'de> for StringVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de> Deserialize<'de> for String {
|
||||
fn deserialize<D>(deserializer: D) -> Result<String, D::Error>
|
||||
where
|
||||
@@ -497,7 +497,7 @@ impl<'de, T> Deserialize<'de> for PhantomData<T> {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
macro_rules! seq_impl {
|
||||
(
|
||||
$ty:ident < T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >,
|
||||
@@ -552,7 +552,7 @@ macro_rules! seq_impl {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
seq_impl!(
|
||||
BinaryHeap<T: Ord>,
|
||||
seq,
|
||||
@@ -560,7 +560,7 @@ seq_impl!(
|
||||
BinaryHeap::with_capacity(size_hint::cautious(seq.size_hint())),
|
||||
BinaryHeap::push);
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
seq_impl!(
|
||||
BTreeSet<T: Eq + Ord>,
|
||||
seq,
|
||||
@@ -568,7 +568,7 @@ seq_impl!(
|
||||
BTreeSet::new(),
|
||||
BTreeSet::insert);
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
seq_impl!(
|
||||
LinkedList<T>,
|
||||
seq,
|
||||
@@ -584,7 +584,7 @@ seq_impl!(
|
||||
HashSet::with_capacity_and_hasher(size_hint::cautious(seq.size_hint()), S::default()),
|
||||
HashSet::insert);
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
seq_impl!(
|
||||
Vec<T>,
|
||||
seq,
|
||||
@@ -592,7 +592,7 @@ seq_impl!(
|
||||
Vec::with_capacity(size_hint::cautious(seq.size_hint())),
|
||||
Vec::push);
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
seq_impl!(
|
||||
VecDeque<T>,
|
||||
seq,
|
||||
@@ -790,7 +790,7 @@ tuple_impls! {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
macro_rules! map_impl {
|
||||
(
|
||||
$ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >,
|
||||
@@ -846,7 +846,7 @@ macro_rules! map_impl {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
map_impl!(
|
||||
BTreeMap<K: Ord, V>,
|
||||
map,
|
||||
@@ -897,6 +897,44 @@ parse_impl!(net::SocketAddrV6);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
struct PathVisitor;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'a> Visitor<'a> for PathVisitor {
|
||||
type Value = &'a Path;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a borrowed path")
|
||||
}
|
||||
|
||||
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(v.as_ref())
|
||||
}
|
||||
|
||||
fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
str::from_utf8(v)
|
||||
.map(AsRef::as_ref)
|
||||
.map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de: 'a, 'a> Deserialize<'de> for &'a Path {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(PathVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
struct PathBufVisitor;
|
||||
|
||||
@@ -1072,7 +1110,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de, T> Deserialize<'de> for Box<[T]>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
@@ -1085,7 +1123,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de> Deserialize<'de> for Box<str> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
@@ -1121,7 +1159,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de, 'a, T: ?Sized> Deserialize<'de> for Cow<'a, T>
|
||||
where
|
||||
T: ToOwned,
|
||||
|
||||
+2
-2
@@ -1262,7 +1262,7 @@ pub trait Visitor<'de>: Sized {
|
||||
/// The default implementation forwards to `visit_str` and then drops the
|
||||
/// `String`.
|
||||
#[inline]
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
@@ -1321,7 +1321,7 @@ pub trait Visitor<'de>: Sized {
|
||||
///
|
||||
/// The default implementation forwards to `visit_bytes` and then drops the
|
||||
/// `Vec<u8>`.
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
|
||||
+126
-17
@@ -51,13 +51,13 @@ pub struct Error {
|
||||
err: ErrorImpl,
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
type ErrorImpl = Box<str>;
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||
type ErrorImpl = ();
|
||||
|
||||
impl de::Error for Error {
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
fn custom<T>(msg: T) -> Self
|
||||
where
|
||||
T: Display,
|
||||
@@ -65,7 +65,7 @@ impl de::Error for Error {
|
||||
Error { err: msg.to_string().into_boxed_str() }
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||
fn custom<T>(msg: T) -> Self
|
||||
where
|
||||
T: Display,
|
||||
@@ -85,12 +85,12 @@ impl ser::Error for Error {
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
formatter.write_str(&self.err)
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
formatter.write_str("Serde deserialization error")
|
||||
}
|
||||
@@ -355,15 +355,84 @@ where
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A deserializer holding a `&str` with a lifetime tied to another
|
||||
/// deserializer.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BorrowedStrDeserializer<'de, E> {
|
||||
value: &'de str,
|
||||
marker: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<'de, E> BorrowedStrDeserializer<'de, E> {
|
||||
/// Create a new borrowed deserializer from the given string.
|
||||
pub fn new(value: &'de str) -> BorrowedStrDeserializer<'de, E> {
|
||||
BorrowedStrDeserializer {
|
||||
value: value,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, E> de::Deserializer<'de> for BorrowedStrDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_borrowed_str(self.value)
|
||||
}
|
||||
|
||||
fn deserialize_enum<V>(
|
||||
self,
|
||||
name: &str,
|
||||
variants: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
let _ = name;
|
||||
let _ = variants;
|
||||
visitor.visit_enum(self)
|
||||
}
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
map struct identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, E> de::EnumAccess<'de> for BorrowedStrDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
type Variant = private::UnitOnly<E>;
|
||||
|
||||
fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
|
||||
where
|
||||
T: de::DeserializeSeed<'de>,
|
||||
{
|
||||
seed.deserialize(self).map(private::unit_only)
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A deserializer holding a `String`.
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StringDeserializer<E> {
|
||||
value: String,
|
||||
marker: PhantomData<E>,
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de, E> IntoDeserializer<'de, E> for String
|
||||
where
|
||||
E: de::Error,
|
||||
@@ -378,7 +447,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de, E> de::Deserializer<'de> for StringDeserializer<E>
|
||||
where
|
||||
E: de::Error,
|
||||
@@ -413,7 +482,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de, 'a, E> de::EnumAccess<'de> for StringDeserializer<E>
|
||||
where
|
||||
E: de::Error,
|
||||
@@ -432,14 +501,14 @@ where
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A deserializer holding a `Cow<str>`.
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CowStrDeserializer<'a, E> {
|
||||
value: Cow<'a, str>,
|
||||
marker: PhantomData<E>,
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str>
|
||||
where
|
||||
E: de::Error,
|
||||
@@ -454,7 +523,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de, 'a, E> de::Deserializer<'de> for CowStrDeserializer<'a, E>
|
||||
where
|
||||
E: de::Error,
|
||||
@@ -492,7 +561,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de, 'a, E> de::EnumAccess<'de> for CowStrDeserializer<'a, E>
|
||||
where
|
||||
E: de::Error,
|
||||
@@ -510,6 +579,46 @@ where
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A deserializer holding a `&[u8]` with a lifetime tied to another
|
||||
/// deserializer.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BorrowedBytesDeserializer<'de, E> {
|
||||
value: &'de [u8],
|
||||
marker: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<'de, E> BorrowedBytesDeserializer<'de, E> {
|
||||
/// Create a new borrowed deserializer from the given byte slice.
|
||||
pub fn new(value: &'de [u8]) -> BorrowedBytesDeserializer<'de, E> {
|
||||
BorrowedBytesDeserializer {
|
||||
value: value,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, E> de::Deserializer<'de> for BorrowedBytesDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_borrowed_bytes(self.value)
|
||||
}
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
map struct identifier ignored_any enum
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A deserializer that iterates over a sequence.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SeqDeserializer<I, E> {
|
||||
@@ -618,7 +727,7 @@ impl Expected for ExpectedInSeq {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de, T, E> IntoDeserializer<'de, E> for Vec<T>
|
||||
where
|
||||
T: IntoDeserializer<'de, E>,
|
||||
@@ -631,7 +740,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de, T, E> IntoDeserializer<'de, E> for BTreeSet<T>
|
||||
where
|
||||
T: IntoDeserializer<'de, E> + Eq + Ord,
|
||||
@@ -1036,7 +1145,7 @@ impl Expected for ExpectedInMap {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap<K, V>
|
||||
where
|
||||
K: IntoDeserializer<'de, E> + Eq + Ord,
|
||||
|
||||
+2
-2
@@ -19,7 +19,7 @@ pub use self::string::from_utf8_lossy;
|
||||
mod string {
|
||||
use lib::*;
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
pub fn from_utf8_lossy(bytes: &[u8]) -> Cow<str> {
|
||||
String::from_utf8_lossy(bytes)
|
||||
}
|
||||
@@ -31,7 +31,7 @@ mod string {
|
||||
//
|
||||
// so it is okay for the return type to be different from the std case as long
|
||||
// as the above works.
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||
pub fn from_utf8_lossy(bytes: &[u8]) -> &str {
|
||||
// Three unicode replacement characters if it fails. They look like a
|
||||
// white-on-black question mark. The user will recognize it as invalid
|
||||
|
||||
+12
-16
@@ -79,7 +79,7 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Serde types in rustdoc of other crates get linked to here.
|
||||
#![doc(html_root_url = "https://docs.rs/serde/1.0.1")]
|
||||
#![doc(html_root_url = "https://docs.rs/serde/1.0.9")]
|
||||
|
||||
// Support using Serde without the standard library!
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
@@ -91,7 +91,6 @@
|
||||
#![cfg_attr(feature = "unstable", feature(nonzero, specialization))]
|
||||
#![cfg_attr(all(feature = "std", feature = "unstable"), feature(into_boxed_c_str))]
|
||||
#![cfg_attr(feature = "alloc", feature(alloc))]
|
||||
#![cfg_attr(feature = "collections", feature(collections))]
|
||||
|
||||
// Whitelisted clippy lints.
|
||||
#![cfg_attr(feature = "cargo-clippy", allow(doc_markdown))]
|
||||
@@ -104,18 +103,15 @@
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "collections")]
|
||||
extern crate collections;
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
extern crate alloc;
|
||||
|
||||
#[cfg(all(feature = "unstable", feature = "std"))]
|
||||
extern crate core;
|
||||
|
||||
/// A facade around all the types we need from the `std`, `core`, `alloc`, and
|
||||
/// `collections` crates. This avoids elaborate import wrangling having to
|
||||
/// happen in every module.
|
||||
/// A facade around all the types we need from the `std`, `core`, and `alloc`
|
||||
/// crates. This avoids elaborate import wrangling having to happen in every
|
||||
/// module.
|
||||
mod lib {
|
||||
mod core {
|
||||
#[cfg(feature = "std")]
|
||||
@@ -140,18 +136,18 @@ mod lib {
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::borrow::{Cow, ToOwned};
|
||||
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||
pub use collections::borrow::{Cow, ToOwned};
|
||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||
pub use alloc::borrow::{Cow, ToOwned};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::string::String;
|
||||
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||
pub use collections::string::{String, ToString};
|
||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||
pub use alloc::string::{String, ToString};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::vec::Vec;
|
||||
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||
pub use collections::vec::Vec;
|
||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||
pub use alloc::vec::Vec;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::boxed::Box;
|
||||
@@ -170,8 +166,8 @@ mod lib {
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::collections::{BinaryHeap, BTreeMap, BTreeSet, LinkedList, VecDeque};
|
||||
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||
pub use collections::{BinaryHeap, BTreeMap, BTreeSet, LinkedList, VecDeque};
|
||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||
pub use alloc::{BinaryHeap, BTreeMap, BTreeSet, LinkedList, VecDeque};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::{error, net};
|
||||
|
||||
+227
-120
@@ -10,12 +10,13 @@ use lib::*;
|
||||
|
||||
use de::{Deserialize, Deserializer, IntoDeserializer, Error, Visitor};
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
use de::Unexpected;
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
pub use self::content::{Content, ContentRefDeserializer, ContentDeserializer,
|
||||
TaggedContentVisitor, TagOrContentField, TagOrContentFieldVisitor,
|
||||
TagContentOtherField, TagContentOtherFieldVisitor,
|
||||
InternallyTaggedUnitVisitor, UntaggedUnitVisitor};
|
||||
|
||||
/// If the missing field is of type `Option<T>` then treat is as `None`,
|
||||
@@ -58,7 +59,7 @@ where
|
||||
Deserialize::deserialize(deserializer)
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
pub fn borrow_cow_str<'de: 'a, 'a, D>(deserializer: D) -> Result<Cow<'a, str>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
@@ -127,7 +128,7 @@ where
|
||||
deserializer.deserialize_str(CowStrVisitor)
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
pub fn borrow_cow_bytes<'de: 'a, 'a, D>(deserializer: D) -> Result<Cow<'a, [u8]>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
@@ -209,7 +210,7 @@ pub mod size_hint {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
mod content {
|
||||
// This module is private and nothing here should be used outside of
|
||||
// generated code.
|
||||
@@ -232,7 +233,7 @@ mod content {
|
||||
///
|
||||
/// Not public API. Use serde-value instead.
|
||||
#[derive(Debug)]
|
||||
pub enum Content {
|
||||
pub enum Content<'de> {
|
||||
Bool(bool),
|
||||
|
||||
U8(u8),
|
||||
@@ -250,18 +251,20 @@ mod content {
|
||||
|
||||
Char(char),
|
||||
String(String),
|
||||
Bytes(Vec<u8>),
|
||||
Str(&'de str),
|
||||
ByteBuf(Vec<u8>),
|
||||
Bytes(&'de [u8]),
|
||||
|
||||
None,
|
||||
Some(Box<Content>),
|
||||
Some(Box<Content<'de>>),
|
||||
|
||||
Unit,
|
||||
Newtype(Box<Content>),
|
||||
Seq(Vec<Content>),
|
||||
Map(Vec<(Content, Content)>),
|
||||
Newtype(Box<Content<'de>>),
|
||||
Seq(Vec<Content<'de>>),
|
||||
Map(Vec<(Content<'de>, Content<'de>)>),
|
||||
}
|
||||
|
||||
impl Content {
|
||||
impl<'de> Content<'de> {
|
||||
fn unexpected(&self) -> Unexpected {
|
||||
match *self {
|
||||
Content::Bool(b) => Unexpected::Bool(b),
|
||||
@@ -277,7 +280,9 @@ mod content {
|
||||
Content::F64(f) => Unexpected::Float(f),
|
||||
Content::Char(c) => Unexpected::Char(c),
|
||||
Content::String(ref s) => Unexpected::Str(s),
|
||||
Content::Bytes(ref b) => Unexpected::Bytes(b),
|
||||
Content::Str(s) => Unexpected::Str(s),
|
||||
Content::ByteBuf(ref b) => Unexpected::Bytes(b),
|
||||
Content::Bytes(b) => Unexpected::Bytes(b),
|
||||
Content::None | Content::Some(_) => Unexpected::Option,
|
||||
Content::Unit => Unexpected::Unit,
|
||||
Content::Newtype(_) => Unexpected::NewtypeStruct,
|
||||
@@ -287,21 +292,30 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Content {
|
||||
impl<'de> Deserialize<'de> for Content<'de> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
// Untagged and internally tagged enums are only supported in
|
||||
// self-describing formats.
|
||||
deserializer.deserialize_any(ContentVisitor)
|
||||
let visitor = ContentVisitor { value: PhantomData };
|
||||
deserializer.deserialize_any(visitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct ContentVisitor;
|
||||
struct ContentVisitor<'de> {
|
||||
value: PhantomData<Content<'de>>,
|
||||
}
|
||||
|
||||
impl<'de> Visitor<'de> for ContentVisitor {
|
||||
type Value = Content;
|
||||
impl<'de> ContentVisitor<'de> {
|
||||
fn new() -> Self {
|
||||
ContentVisitor { value: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Visitor<'de> for ContentVisitor<'de> {
|
||||
type Value = Content<'de>;
|
||||
|
||||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str("any value")
|
||||
@@ -398,6 +412,13 @@ mod content {
|
||||
Ok(Content::String(value.into()))
|
||||
}
|
||||
|
||||
fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F>
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
Ok(Content::Str(value))
|
||||
}
|
||||
|
||||
fn visit_string<F>(self, value: String) -> Result<Self::Value, F>
|
||||
where
|
||||
F: de::Error,
|
||||
@@ -409,14 +430,21 @@ mod content {
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
Ok(Content::Bytes(value.into()))
|
||||
Ok(Content::ByteBuf(value.into()))
|
||||
}
|
||||
|
||||
fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F>
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
Ok(Content::Bytes(value))
|
||||
}
|
||||
|
||||
fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F>
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
Ok(Content::Bytes(value))
|
||||
Ok(Content::ByteBuf(value))
|
||||
}
|
||||
|
||||
fn visit_unit<F>(self) -> Result<Self::Value, F>
|
||||
@@ -480,23 +508,24 @@ mod content {
|
||||
/// This is the type of the map keys in an internally tagged enum.
|
||||
///
|
||||
/// Not public API.
|
||||
pub enum TagOrContent {
|
||||
pub enum TagOrContent<'de> {
|
||||
Tag,
|
||||
Content(Content),
|
||||
Content(Content<'de>),
|
||||
}
|
||||
|
||||
struct TagOrContentVisitor {
|
||||
struct TagOrContentVisitor<'de> {
|
||||
name: &'static str,
|
||||
value: PhantomData<TagOrContent<'de>>,
|
||||
}
|
||||
|
||||
impl TagOrContentVisitor {
|
||||
impl<'de> TagOrContentVisitor<'de> {
|
||||
fn new(name: &'static str) -> Self {
|
||||
TagOrContentVisitor { name: name }
|
||||
TagOrContentVisitor { name: name, value: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> DeserializeSeed<'de> for TagOrContentVisitor {
|
||||
type Value = TagOrContent;
|
||||
impl<'de> DeserializeSeed<'de> for TagOrContentVisitor<'de> {
|
||||
type Value = TagOrContent<'de>;
|
||||
|
||||
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||
where
|
||||
@@ -508,8 +537,8 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Visitor<'de> for TagOrContentVisitor {
|
||||
type Value = TagOrContent;
|
||||
impl<'de> Visitor<'de> for TagOrContentVisitor<'de> {
|
||||
type Value = TagOrContent<'de>;
|
||||
|
||||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(fmt, "a type tag `{}` or any other value", self.name)
|
||||
@@ -519,7 +548,7 @@ mod content {
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_bool(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -528,14 +557,14 @@ mod content {
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor.visit_i8(value).map(TagOrContent::Content)
|
||||
ContentVisitor::new().visit_i8(value).map(TagOrContent::Content)
|
||||
}
|
||||
|
||||
fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_i16(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -544,7 +573,7 @@ mod content {
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_i32(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -553,7 +582,7 @@ mod content {
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_i64(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -562,14 +591,14 @@ mod content {
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor.visit_u8(value).map(TagOrContent::Content)
|
||||
ContentVisitor::new().visit_u8(value).map(TagOrContent::Content)
|
||||
}
|
||||
|
||||
fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_u16(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -578,7 +607,7 @@ mod content {
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_u32(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -587,7 +616,7 @@ mod content {
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_u64(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -596,7 +625,7 @@ mod content {
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_f32(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -605,7 +634,7 @@ mod content {
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_f64(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -614,7 +643,7 @@ mod content {
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_char(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -626,12 +655,25 @@ mod content {
|
||||
if value == self.name {
|
||||
Ok(TagOrContent::Tag)
|
||||
} else {
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_str(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F>
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
if value == self.name {
|
||||
Ok(TagOrContent::Tag)
|
||||
} else {
|
||||
ContentVisitor::new()
|
||||
.visit_borrowed_str(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_string<F>(self, value: String) -> Result<Self::Value, F>
|
||||
where
|
||||
F: de::Error,
|
||||
@@ -639,7 +681,7 @@ mod content {
|
||||
if value == self.name {
|
||||
Ok(TagOrContent::Tag)
|
||||
} else {
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_string(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -652,12 +694,25 @@ mod content {
|
||||
if value == self.name.as_bytes() {
|
||||
Ok(TagOrContent::Tag)
|
||||
} else {
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_bytes(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F>
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
if value == self.name.as_bytes() {
|
||||
Ok(TagOrContent::Tag)
|
||||
} else {
|
||||
ContentVisitor::new()
|
||||
.visit_borrowed_bytes(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F>
|
||||
where
|
||||
F: de::Error,
|
||||
@@ -665,7 +720,7 @@ mod content {
|
||||
if value == self.name.as_bytes() {
|
||||
Ok(TagOrContent::Tag)
|
||||
} else {
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_byte_buf(value)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -675,21 +730,21 @@ mod content {
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor.visit_unit().map(TagOrContent::Content)
|
||||
ContentVisitor::new().visit_unit().map(TagOrContent::Content)
|
||||
}
|
||||
|
||||
fn visit_none<F>(self) -> Result<Self::Value, F>
|
||||
where
|
||||
F: de::Error,
|
||||
{
|
||||
ContentVisitor.visit_none().map(TagOrContent::Content)
|
||||
ContentVisitor::new().visit_none().map(TagOrContent::Content)
|
||||
}
|
||||
|
||||
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_some(deserializer)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -698,7 +753,7 @@ mod content {
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_newtype_struct(deserializer)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -707,7 +762,7 @@ mod content {
|
||||
where
|
||||
V: SeqAccess<'de>,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_seq(visitor)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -716,7 +771,7 @@ mod content {
|
||||
where
|
||||
V: MapAccess<'de>,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_map(visitor)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -725,7 +780,7 @@ mod content {
|
||||
where
|
||||
V: EnumAccess<'de>,
|
||||
{
|
||||
ContentVisitor
|
||||
ContentVisitor::new()
|
||||
.visit_enum(visitor)
|
||||
.map(TagOrContent::Content)
|
||||
}
|
||||
@@ -734,33 +789,33 @@ mod content {
|
||||
/// Used by generated code to deserialize an internally tagged enum.
|
||||
///
|
||||
/// Not public API.
|
||||
pub struct TaggedContent<T> {
|
||||
pub struct TaggedContent<'de, T> {
|
||||
pub tag: T,
|
||||
pub content: Content,
|
||||
pub content: Content<'de>,
|
||||
}
|
||||
|
||||
/// Not public API.
|
||||
pub struct TaggedContentVisitor<T> {
|
||||
pub struct TaggedContentVisitor<'de, T> {
|
||||
tag_name: &'static str,
|
||||
tag: PhantomData<T>,
|
||||
value: PhantomData<TaggedContent<'de, T>>,
|
||||
}
|
||||
|
||||
impl<T> TaggedContentVisitor<T> {
|
||||
impl<'de, T> TaggedContentVisitor<'de, T> {
|
||||
/// Visitor for the content of an internally tagged enum with the given tag
|
||||
/// name.
|
||||
pub fn new(name: &'static str) -> Self {
|
||||
TaggedContentVisitor {
|
||||
tag_name: name,
|
||||
tag: PhantomData,
|
||||
value: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T> DeserializeSeed<'de> for TaggedContentVisitor<T>
|
||||
impl<'de, T> DeserializeSeed<'de> for TaggedContentVisitor<'de, T>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
type Value = TaggedContent<T>;
|
||||
type Value = TaggedContent<'de, T>;
|
||||
|
||||
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||
where
|
||||
@@ -772,11 +827,11 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T> Visitor<'de> for TaggedContentVisitor<T>
|
||||
impl<'de, T> Visitor<'de> for TaggedContentVisitor<'de, T>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
type Value = TaggedContent<T>;
|
||||
type Value = TaggedContent<'de, T>;
|
||||
|
||||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str("any value")
|
||||
@@ -863,15 +918,63 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
/// Used by generated code to deserialize an adjacently tagged enum when
|
||||
/// ignoring unrelated fields is allowed.
|
||||
///
|
||||
/// Not public API.
|
||||
pub enum TagContentOtherField {
|
||||
Tag,
|
||||
Content,
|
||||
Other,
|
||||
}
|
||||
|
||||
/// Not public API.
|
||||
pub struct TagContentOtherFieldVisitor {
|
||||
pub tag: &'static str,
|
||||
pub content: &'static str,
|
||||
}
|
||||
|
||||
impl<'de> DeserializeSeed<'de> for TagContentOtherFieldVisitor {
|
||||
type Value = TagContentOtherField;
|
||||
|
||||
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Visitor<'de> for TagContentOtherFieldVisitor {
|
||||
type Value = TagContentOtherField;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "{:?}, {:?}, or other ignored fields", self.tag, self.content)
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
if field == self.tag {
|
||||
Ok(TagContentOtherField::Tag)
|
||||
} else if field == self.content {
|
||||
Ok(TagContentOtherField::Content)
|
||||
} else {
|
||||
Ok(TagContentOtherField::Other)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Not public API
|
||||
pub struct ContentDeserializer<E> {
|
||||
content: Content,
|
||||
pub struct ContentDeserializer<'de, E> {
|
||||
content: Content<'de>,
|
||||
err: PhantomData<E>,
|
||||
}
|
||||
|
||||
/// Used when deserializing an internally tagged enum because the content will
|
||||
/// be used exactly once.
|
||||
impl<'de, E> Deserializer<'de> for ContentDeserializer<E>
|
||||
impl<'de, E> Deserializer<'de> for ContentDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -895,6 +998,9 @@ mod content {
|
||||
Content::F64(v) => visitor.visit_f64(v),
|
||||
Content::Char(v) => visitor.visit_char(v),
|
||||
Content::String(v) => visitor.visit_string(v),
|
||||
Content::Str(v) => visitor.visit_borrowed_str(v),
|
||||
Content::ByteBuf(v) => visitor.visit_byte_buf(v),
|
||||
Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
|
||||
Content::Unit => visitor.visit_unit(),
|
||||
Content::None => visitor.visit_none(),
|
||||
Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
|
||||
@@ -916,7 +1022,6 @@ mod content {
|
||||
try!(map_visitor.end());
|
||||
Ok(value)
|
||||
}
|
||||
Content::Bytes(v) => visitor.visit_byte_buf(v),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -977,7 +1082,7 @@ mod content {
|
||||
}
|
||||
(variant, Some(value))
|
||||
}
|
||||
Content::String(variant) => (Content::String(variant), None),
|
||||
s @ Content::String(_) | s @ Content::Str(_) => (s, None),
|
||||
other => {
|
||||
return Err(de::Error::invalid_type(other.unexpected(), &"string or map"),);
|
||||
}
|
||||
@@ -999,9 +1104,9 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> ContentDeserializer<E> {
|
||||
impl<'de, E> ContentDeserializer<'de, E> {
|
||||
/// private API, don't use
|
||||
pub fn new(content: Content) -> Self {
|
||||
pub fn new(content: Content<'de>) -> Self {
|
||||
ContentDeserializer {
|
||||
content: content,
|
||||
err: PhantomData,
|
||||
@@ -1009,21 +1114,21 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
struct EnumDeserializer<E>
|
||||
struct EnumDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
variant: Content,
|
||||
value: Option<Content>,
|
||||
variant: Content<'de>,
|
||||
value: Option<Content<'de>>,
|
||||
err: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<'de, E> de::EnumAccess<'de> for EnumDeserializer<E>
|
||||
impl<'de, E> de::EnumAccess<'de> for EnumDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
type Variant = VariantDeserializer<Self::Error>;
|
||||
type Variant = VariantDeserializer<'de, Self::Error>;
|
||||
|
||||
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), E>
|
||||
where
|
||||
@@ -1038,15 +1143,15 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
struct VariantDeserializer<E>
|
||||
struct VariantDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
value: Option<Content>,
|
||||
value: Option<Content<'de>>,
|
||||
err: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<'de, E> de::VariantAccess<'de> for VariantDeserializer<E>
|
||||
impl<'de, E> de::VariantAccess<'de> for VariantDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -1102,19 +1207,19 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
struct SeqDeserializer<E>
|
||||
struct SeqDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
iter: <Vec<Content> as IntoIterator>::IntoIter,
|
||||
iter: <Vec<Content<'de>> as IntoIterator>::IntoIter,
|
||||
err: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<E> SeqDeserializer<E>
|
||||
impl<'de, E> SeqDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
fn new(vec: Vec<Content>) -> Self {
|
||||
fn new(vec: Vec<Content<'de>>) -> Self {
|
||||
SeqDeserializer {
|
||||
iter: vec.into_iter(),
|
||||
err: PhantomData,
|
||||
@@ -1122,7 +1227,7 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, E> de::Deserializer<'de> for SeqDeserializer<E>
|
||||
impl<'de, E> de::Deserializer<'de> for SeqDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -1154,7 +1259,7 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, E> de::SeqAccess<'de> for SeqDeserializer<E>
|
||||
impl<'de, E> de::SeqAccess<'de> for SeqDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -1178,20 +1283,20 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
struct MapDeserializer<E>
|
||||
struct MapDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
iter: <Vec<(Content, Content)> as IntoIterator>::IntoIter,
|
||||
value: Option<Content>,
|
||||
iter: <Vec<(Content<'de>, Content<'de>)> as IntoIterator>::IntoIter,
|
||||
value: Option<Content<'de>>,
|
||||
err: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<E> MapDeserializer<E>
|
||||
impl<'de, E> MapDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
fn new(map: Vec<(Content, Content)>) -> Self {
|
||||
fn new(map: Vec<(Content<'de>, Content<'de>)>) -> Self {
|
||||
MapDeserializer {
|
||||
iter: map.into_iter(),
|
||||
value: None,
|
||||
@@ -1200,7 +1305,7 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, E> de::MapAccess<'de> for MapDeserializer<E>
|
||||
impl<'de, E> de::MapAccess<'de> for MapDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -1234,7 +1339,7 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, E> de::Deserializer<'de> for MapDeserializer<E>
|
||||
impl<'de, E> de::Deserializer<'de> for MapDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -1256,14 +1361,14 @@ mod content {
|
||||
}
|
||||
|
||||
/// Not public API.
|
||||
pub struct ContentRefDeserializer<'a, E> {
|
||||
content: &'a Content,
|
||||
pub struct ContentRefDeserializer<'a, 'de: 'a, E> {
|
||||
content: &'a Content<'de>,
|
||||
err: PhantomData<E>,
|
||||
}
|
||||
|
||||
/// Used when deserializing an untagged enum because the content may need to be
|
||||
/// used more than once.
|
||||
impl<'de, 'a, E> Deserializer<'de> for ContentRefDeserializer<'a, E>
|
||||
impl<'de, 'a, E> Deserializer<'de> for ContentRefDeserializer<'a, 'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -1287,6 +1392,9 @@ mod content {
|
||||
Content::F64(v) => visitor.visit_f64(v),
|
||||
Content::Char(v) => visitor.visit_char(v),
|
||||
Content::String(ref v) => visitor.visit_str(v),
|
||||
Content::Str(v) => visitor.visit_borrowed_str(v),
|
||||
Content::ByteBuf(ref v) => visitor.visit_bytes(v),
|
||||
Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
|
||||
Content::Unit => visitor.visit_unit(),
|
||||
Content::None => visitor.visit_none(),
|
||||
Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
|
||||
@@ -1312,7 +1420,6 @@ mod content {
|
||||
try!(map_visitor.end());
|
||||
Ok(value)
|
||||
}
|
||||
Content::Bytes(ref v) => visitor.visit_bytes(v),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1369,7 +1476,7 @@ mod content {
|
||||
}
|
||||
(variant, Some(value))
|
||||
}
|
||||
ref s @ Content::String(_) => (s, None),
|
||||
ref s @ Content::String(_) | ref s @ Content::Str(_) => (s, None),
|
||||
ref other => {
|
||||
return Err(de::Error::invalid_type(other.unexpected(), &"string or map"),);
|
||||
}
|
||||
@@ -1391,9 +1498,9 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, E> ContentRefDeserializer<'a, E> {
|
||||
impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E> {
|
||||
/// private API, don't use
|
||||
pub fn new(content: &'a Content) -> Self {
|
||||
pub fn new(content: &'a Content<'de>) -> Self {
|
||||
ContentRefDeserializer {
|
||||
content: content,
|
||||
err: PhantomData,
|
||||
@@ -1401,21 +1508,21 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
struct EnumRefDeserializer<'a, E>
|
||||
struct EnumRefDeserializer<'a, 'de: 'a, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
variant: &'a Content,
|
||||
value: Option<&'a Content>,
|
||||
variant: &'a Content<'de>,
|
||||
value: Option<&'a Content<'de>>,
|
||||
err: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<'de, 'a, E> de::EnumAccess<'de> for EnumRefDeserializer<'a, E>
|
||||
impl<'de, 'a, E> de::EnumAccess<'de> for EnumRefDeserializer<'a, 'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
type Variant = VariantRefDeserializer<'a, Self::Error>;
|
||||
type Variant = VariantRefDeserializer<'a, 'de, Self::Error>;
|
||||
|
||||
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
|
||||
where
|
||||
@@ -1430,15 +1537,15 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
struct VariantRefDeserializer<'a, E>
|
||||
struct VariantRefDeserializer<'a, 'de: 'a, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
value: Option<&'a Content>,
|
||||
value: Option<&'a Content<'de>>,
|
||||
err: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<'de, 'a, E> de::VariantAccess<'de> for VariantRefDeserializer<'a, E>
|
||||
impl<'de, 'a, E> de::VariantAccess<'de> for VariantRefDeserializer<'a, 'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -1494,19 +1601,19 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
struct SeqRefDeserializer<'a, E>
|
||||
struct SeqRefDeserializer<'a, 'de: 'a, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
iter: <&'a [Content] as IntoIterator>::IntoIter,
|
||||
iter: <&'a [Content<'de>] as IntoIterator>::IntoIter,
|
||||
err: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<'a, E> SeqRefDeserializer<'a, E>
|
||||
impl<'a, 'de, E> SeqRefDeserializer<'a, 'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
fn new(vec: &'a [Content]) -> Self {
|
||||
fn new(vec: &'a [Content<'de>]) -> Self {
|
||||
SeqRefDeserializer {
|
||||
iter: vec.into_iter(),
|
||||
err: PhantomData,
|
||||
@@ -1514,7 +1621,7 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, 'a, E> de::Deserializer<'de> for SeqRefDeserializer<'a, E>
|
||||
impl<'de, 'a, E> de::Deserializer<'de> for SeqRefDeserializer<'a, 'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -1546,7 +1653,7 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, 'a, E> de::SeqAccess<'de> for SeqRefDeserializer<'a, E>
|
||||
impl<'de, 'a, E> de::SeqAccess<'de> for SeqRefDeserializer<'a, 'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -1570,20 +1677,20 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
struct MapRefDeserializer<'a, E>
|
||||
struct MapRefDeserializer<'a, 'de: 'a, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
iter: <&'a [(Content, Content)] as IntoIterator>::IntoIter,
|
||||
value: Option<&'a Content>,
|
||||
iter: <&'a [(Content<'de>, Content<'de>)] as IntoIterator>::IntoIter,
|
||||
value: Option<&'a Content<'de>>,
|
||||
err: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<'a, E> MapRefDeserializer<'a, E>
|
||||
impl<'a, 'de, E> MapRefDeserializer<'a, 'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
fn new(map: &'a [(Content, Content)]) -> Self {
|
||||
fn new(map: &'a [(Content<'de>, Content<'de>)]) -> Self {
|
||||
MapRefDeserializer {
|
||||
iter: map.into_iter(),
|
||||
value: None,
|
||||
@@ -1592,7 +1699,7 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, 'a, E> de::MapAccess<'de> for MapRefDeserializer<'a, E>
|
||||
impl<'de, 'a, E> de::MapAccess<'de> for MapRefDeserializer<'a, 'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -1627,7 +1734,7 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, 'a, E> de::Deserializer<'de> for MapRefDeserializer<'a, E>
|
||||
impl<'de, 'a, E> de::Deserializer<'de> for MapRefDeserializer<'a, 'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -1648,7 +1755,7 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, E> de::IntoDeserializer<'de, E> for ContentDeserializer<E>
|
||||
impl<'de, E> de::IntoDeserializer<'de, E> for ContentDeserializer<'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
@@ -1659,7 +1766,7 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, 'a, E> de::IntoDeserializer<'de, E> for ContentRefDeserializer<'a, E>
|
||||
impl<'de, 'a, E> de::IntoDeserializer<'de, E> for ContentRefDeserializer<'a, 'de, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
|
||||
+13
-13
@@ -10,7 +10,7 @@ use lib::*;
|
||||
|
||||
use ser::{self, Serialize, Serializer, SerializeMap, SerializeStruct, Impossible};
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
use self::content::{SerializeTupleVariantAsMapValue, SerializeStructVariantAsMapValue};
|
||||
|
||||
/// Used to check that serde(getter) attributes return the expected type.
|
||||
@@ -64,7 +64,7 @@ enum Unsupported {
|
||||
Sequence,
|
||||
Tuple,
|
||||
TupleStruct,
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||
Enum,
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ impl Display for Unsupported {
|
||||
Unsupported::Sequence => formatter.write_str("a sequence"),
|
||||
Unsupported::Tuple => formatter.write_str("a tuple"),
|
||||
Unsupported::TupleStruct => formatter.write_str("a tuple struct"),
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||
Unsupported::Enum => formatter.write_str("an enum"),
|
||||
}
|
||||
}
|
||||
@@ -117,14 +117,14 @@ where
|
||||
type SerializeMap = S::SerializeMap;
|
||||
type SerializeStruct = S::SerializeStruct;
|
||||
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||
type SerializeTupleVariant = Impossible<S::Ok, S::Error>;
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
type SerializeTupleVariant = SerializeTupleVariantAsMapValue<S::SerializeMap>;
|
||||
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||
type SerializeStructVariant = Impossible<S::Ok, S::Error>;
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
type SerializeStructVariant = SerializeStructVariantAsMapValue<S::SerializeMap>;
|
||||
|
||||
fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
|
||||
@@ -257,7 +257,7 @@ where
|
||||
Err(self.bad_type(Unsupported::TupleStruct))
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
_: &'static str,
|
||||
@@ -270,7 +270,7 @@ where
|
||||
Err(self.bad_type(Unsupported::Enum))
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
_: &'static str,
|
||||
@@ -300,7 +300,7 @@ where
|
||||
Ok(state)
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
_: &'static str,
|
||||
@@ -313,7 +313,7 @@ where
|
||||
Err(self.bad_type(Unsupported::Enum))
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
_: &'static str,
|
||||
@@ -327,7 +327,7 @@ where
|
||||
Ok(SerializeStructVariantAsMapValue::new(map, inner_variant, len),)
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||
fn collect_str<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: Display,
|
||||
@@ -363,7 +363,7 @@ impl Display for Error {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
mod content {
|
||||
use lib::*;
|
||||
|
||||
|
||||
+23
-11
@@ -56,7 +56,7 @@ impl Serialize for str {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl Serialize for String {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
@@ -177,6 +177,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
macro_rules! seq_impl {
|
||||
($ty:ident < T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)* >) => {
|
||||
impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
|
||||
@@ -195,22 +196,22 @@ macro_rules! seq_impl {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
seq_impl!(BinaryHeap<T: Ord>);
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
seq_impl!(BTreeSet<T: Ord>);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
seq_impl!(HashSet<T: Eq + Hash, H: BuildHasher>);
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
seq_impl!(LinkedList<T>);
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
seq_impl!(Vec<T>);
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
seq_impl!(VecDeque<T>);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -290,6 +291,7 @@ tuple_impls! {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
macro_rules! map_impl {
|
||||
($ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)* >) => {
|
||||
impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
|
||||
@@ -309,7 +311,7 @@ macro_rules! map_impl {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
map_impl!(BTreeMap<K: Ord, V>);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
@@ -343,14 +345,24 @@ deref_impl!(<T> Serialize for Rc<T> where T: Serialize);
|
||||
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
||||
deref_impl!(<T> Serialize for Arc<T> where T: Serialize);
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
deref_impl!(<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned);
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
deref_impl!(<T> Serialize for NonZero<T> where T: Serialize + Zeroable);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl<T> Serialize for NonZero<T>
|
||||
where
|
||||
T: Serialize + Zeroable + Clone,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
self.clone().get().serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for Cell<T>
|
||||
where
|
||||
T: Serialize + Copy,
|
||||
|
||||
@@ -1321,7 +1321,7 @@ pub trait Serializer: Sized {
|
||||
///
|
||||
/// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
|
||||
/// [`serialize_str`]: #tymethod.serialize_str
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: Display,
|
||||
@@ -1358,7 +1358,7 @@ pub trait Serializer: Sized {
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: Display;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_derive"
|
||||
version = "1.0.1" # remember to update html_root_url
|
||||
version = "1.0.9" # remember to update html_root_url
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
||||
@@ -20,5 +20,5 @@ proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
quote = "0.3.8"
|
||||
serde_derive_internals = { version = "=0.15.0", default-features = false, path = "../serde_derive_internals" }
|
||||
serde_derive_internals = { version = "=0.15.1", default-features = false, path = "../serde_derive_internals" }
|
||||
syn = { version = "0.11", features = ["visit"] }
|
||||
|
||||
+62
-17
@@ -23,15 +23,14 @@ pub fn expand_derive_deserialize(input: &syn::DeriveInput) -> Result<Tokens, Str
|
||||
|
||||
let ident = &cont.ident;
|
||||
let params = Parameters::new(&cont);
|
||||
let (de_impl_generics, _, ty_generics, where_clause) = split_with_de_lifetime(¶ms);
|
||||
let dummy_const = Ident::new(format!("_IMPL_DESERIALIZE_FOR_{}", ident));
|
||||
let body = Stmts(deserialize_body(&cont, ¶ms));
|
||||
|
||||
let impl_block = if let Some(remote) = cont.attrs.remote() {
|
||||
let (impl_generics, ty_generics, where_clause) = cont.generics.split_for_impl();
|
||||
let de_lifetime = params.de_lifetime_def();
|
||||
quote! {
|
||||
impl #impl_generics #ident #ty_generics #where_clause {
|
||||
fn deserialize<#de_lifetime, __D>(__deserializer: __D) -> _serde::export::Result<#remote #ty_generics, __D::Error>
|
||||
impl #de_impl_generics #ident #ty_generics #where_clause {
|
||||
fn deserialize<__D>(__deserializer: __D) -> _serde::export::Result<#remote #ty_generics, __D::Error>
|
||||
where __D: _serde::Deserializer<'de>
|
||||
{
|
||||
#body
|
||||
@@ -39,7 +38,6 @@ pub fn expand_derive_deserialize(input: &syn::DeriveInput) -> Result<Tokens, Str
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let (de_impl_generics, _, ty_generics, where_clause) = split_with_de_lifetime(¶ms);
|
||||
quote! {
|
||||
#[automatically_derived]
|
||||
impl #de_impl_generics _serde::Deserialize<'de> for #ident #ty_generics #where_clause {
|
||||
@@ -795,9 +793,19 @@ fn deserialize_adjacently_tagged_enum(
|
||||
|
||||
let expecting = format!("adjacently tagged enum {}", params.type_name());
|
||||
let type_name = cattrs.name().deserialize_name();
|
||||
let deny_unknown_fields = cattrs.deny_unknown_fields();
|
||||
|
||||
/// If unknown fields are allowed, we pick the visitor that can
|
||||
/// step over those. Otherwise we pick the visitor that fails on
|
||||
/// unknown keys.
|
||||
let field_visitor_ty = if deny_unknown_fields {
|
||||
quote! { _serde::private::de::TagOrContentFieldVisitor }
|
||||
} else {
|
||||
quote! { _serde::private::de::TagContentOtherFieldVisitor }
|
||||
};
|
||||
|
||||
let tag_or_content = quote! {
|
||||
_serde::private::de::TagOrContentFieldVisitor {
|
||||
#field_visitor_ty {
|
||||
tag: #tag,
|
||||
content: #content,
|
||||
}
|
||||
@@ -844,9 +852,46 @@ fn deserialize_adjacently_tagged_enum(
|
||||
};
|
||||
}
|
||||
|
||||
let visit_third_key = quote! {
|
||||
// Visit the third key in the map, hopefully there isn't one.
|
||||
match try!(_serde::de::MapAccess::next_key_seed(&mut __map, #tag_or_content)) {
|
||||
/// Advance the map by one key, returning early in case of error.
|
||||
let next_key = quote! {
|
||||
try!(_serde::de::MapAccess::next_key_seed(&mut __map, #tag_or_content))
|
||||
};
|
||||
|
||||
/// When allowing unknown fields, we want to transparently step through keys we don't care
|
||||
/// about until we find `tag`, `content`, or run out of keys.
|
||||
let next_relevant_key = if deny_unknown_fields {
|
||||
next_key
|
||||
} else {
|
||||
quote! {
|
||||
{
|
||||
let mut __rk : _serde::export::Option<_serde::private::de::TagOrContentField> = _serde::export::None;
|
||||
while let _serde::export::Some(__k) = #next_key {
|
||||
match __k {
|
||||
_serde::private::de::TagContentOtherField::Other => {
|
||||
try!(_serde::de::MapAccess::next_value::<_serde::de::IgnoredAny>(&mut __map));
|
||||
continue;
|
||||
},
|
||||
_serde::private::de::TagContentOtherField::Tag => {
|
||||
__rk = _serde::export::Some(_serde::private::de::TagOrContentField::Tag);
|
||||
break;
|
||||
}
|
||||
_serde::private::de::TagContentOtherField::Content => {
|
||||
__rk = _serde::export::Some(_serde::private::de::TagOrContentField::Content);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__rk
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/// Step through remaining keys, looking for duplicates of previously-seen keys.
|
||||
/// When unknown fields are denied, any key that isn't a duplicate will at this
|
||||
/// point immediately produce an error.
|
||||
let visit_remaining_keys = quote! {
|
||||
match #next_relevant_key {
|
||||
_serde::export::Some(_serde::private::de::TagOrContentField::Tag) => {
|
||||
_serde::export::Err(<__A::Error as _serde::de::Error>::duplicate_field(#tag))
|
||||
}
|
||||
@@ -895,14 +940,14 @@ fn deserialize_adjacently_tagged_enum(
|
||||
fn visit_map<__A>(self, mut __map: __A) -> _serde::export::Result<Self::Value, __A::Error>
|
||||
where __A: _serde::de::MapAccess<'de>
|
||||
{
|
||||
// Visit the first key.
|
||||
match try!(_serde::de::MapAccess::next_key_seed(&mut __map, #tag_or_content)) {
|
||||
// Visit the first relevant key.
|
||||
match #next_relevant_key {
|
||||
// First key is the tag.
|
||||
_serde::export::Some(_serde::private::de::TagOrContentField::Tag) => {
|
||||
// Parse the tag.
|
||||
let __field = try!(_serde::de::MapAccess::next_value(&mut __map));
|
||||
// Visit the second key.
|
||||
match try!(_serde::de::MapAccess::next_key_seed(&mut __map, #tag_or_content)) {
|
||||
match #next_relevant_key {
|
||||
// Second key is a duplicate of the tag.
|
||||
_serde::export::Some(_serde::private::de::TagOrContentField::Tag) => {
|
||||
_serde::export::Err(<__A::Error as _serde::de::Error>::duplicate_field(#tag))
|
||||
@@ -915,8 +960,8 @@ fn deserialize_adjacently_tagged_enum(
|
||||
marker: _serde::export::PhantomData,
|
||||
lifetime: _serde::export::PhantomData,
|
||||
}));
|
||||
// Visit the third key, hopefully there isn't one.
|
||||
#visit_third_key
|
||||
// Visit remaining keys, looking for duplicates.
|
||||
#visit_remaining_keys
|
||||
}
|
||||
// There is no second key; might be okay if the we have a unit variant.
|
||||
_serde::export::None => #missing_content
|
||||
@@ -927,7 +972,7 @@ fn deserialize_adjacently_tagged_enum(
|
||||
// Buffer up the content.
|
||||
let __content = try!(_serde::de::MapAccess::next_value::<_serde::private::de::Content>(&mut __map));
|
||||
// Visit the second key.
|
||||
match try!(_serde::de::MapAccess::next_key_seed(&mut __map, #tag_or_content)) {
|
||||
match #next_relevant_key {
|
||||
// Second key is the tag.
|
||||
_serde::export::Some(_serde::private::de::TagOrContentField::Tag) => {
|
||||
let __deserializer = _serde::private::de::ContentDeserializer::<__A::Error>::new(__content);
|
||||
@@ -936,8 +981,8 @@ fn deserialize_adjacently_tagged_enum(
|
||||
// Deserialize the buffered content now that we know the variant.
|
||||
#(#variant_arms)*
|
||||
});
|
||||
// Visit the third key, hopefully there isn't one.
|
||||
#visit_third_key
|
||||
// Visit remaining keys, looking for duplicates.
|
||||
#visit_remaining_keys
|
||||
}
|
||||
// Second key is a duplicate of the content.
|
||||
_serde::export::Some(_serde::private::de::TagOrContentField::Content) => {
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
//!
|
||||
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.1")]
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.9")]
|
||||
|
||||
#![cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))]
|
||||
#![cfg_attr(feature = "cargo-clippy", allow(used_underscore_binding))]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_derive_internals"
|
||||
version = "0.15.0" # remember to update html_root_url
|
||||
version = "0.15.1" # remember to update html_root_url
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "AST representation used by Serde derive macros. Unstable."
|
||||
|
||||
@@ -45,7 +45,7 @@ pub enum Style {
|
||||
}
|
||||
|
||||
impl<'a> Container<'a> {
|
||||
pub fn from_ast(cx: &Ctxt, item: &'a syn::MacroInput) -> Container<'a> {
|
||||
pub fn from_ast(cx: &Ctxt, item: &'a syn::DeriveInput) -> Container<'a> {
|
||||
let attrs = attr::Container::from_ast(cx, item);
|
||||
|
||||
let mut body = match item.body {
|
||||
|
||||
@@ -166,7 +166,7 @@ pub enum Identifier {
|
||||
|
||||
impl Container {
|
||||
/// Extract out the `#[serde(...)]` attributes from an item.
|
||||
pub fn from_ast(cx: &Ctxt, item: &syn::MacroInput) -> Self {
|
||||
pub fn from_ast(cx: &Ctxt, item: &syn::DeriveInput) -> Self {
|
||||
let mut ser_name = Attr::none(cx, "rename");
|
||||
let mut de_name = Attr::none(cx, "rename");
|
||||
let mut deny_unknown_fields = BoolAttr::none(cx, "deny_unknown_fields");
|
||||
@@ -421,7 +421,7 @@ impl Container {
|
||||
|
||||
fn decide_tag(
|
||||
cx: &Ctxt,
|
||||
item: &syn::MacroInput,
|
||||
item: &syn::DeriveInput,
|
||||
untagged: BoolAttr,
|
||||
internal_tag: Attr<String>,
|
||||
content: Attr<String>,
|
||||
@@ -477,7 +477,7 @@ fn decide_tag(
|
||||
|
||||
fn decide_identifier(
|
||||
cx: &Ctxt,
|
||||
item: &syn::MacroInput,
|
||||
item: &syn::DeriveInput,
|
||||
field_identifier: BoolAttr,
|
||||
variant_identifier: BoolAttr,
|
||||
) -> Identifier {
|
||||
@@ -719,6 +719,12 @@ impl Field {
|
||||
skip_deserializing.set_true();
|
||||
}
|
||||
|
||||
// Parse `#[serde(skip)]`
|
||||
MetaItem(Word(ref name)) if name == "skip" => {
|
||||
skip_serializing.set_true();
|
||||
skip_deserializing.set_true();
|
||||
},
|
||||
|
||||
// Parse `#[serde(skip_serializing_if = "...")]`
|
||||
MetaItem(NameValue(ref name, ref lit)) if name == "skip_serializing_if" => {
|
||||
if let Ok(path) = parse_lit_into_path(cx, name.as_ref(), lit) {
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive_internals/0.15.0")]
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive_internals/0.15.1")]
|
||||
|
||||
extern crate syn;
|
||||
#[macro_use]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_test"
|
||||
version = "1.0.1" # remember to update html_root_url
|
||||
version = "1.0.9" # remember to update html_root_url
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Token De/Serializer for testing De/Serialize implementations"
|
||||
|
||||
@@ -155,7 +155,7 @@
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/serde_test/1.0.1")]
|
||||
#![doc(html_root_url = "https://docs.rs/serde_test/1.0.9")]
|
||||
|
||||
#[macro_use]
|
||||
extern crate serde;
|
||||
|
||||
@@ -580,6 +580,41 @@ fn test_skip_serializing_struct() {
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct SkipStruct<B>
|
||||
{
|
||||
a: i8,
|
||||
#[serde(skip)]
|
||||
b: B,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_skip_struct() {
|
||||
assert_ser_tokens(
|
||||
&SkipStruct { a: 1, b: 2 },
|
||||
&[
|
||||
Token::Struct { name: "SkipStruct", len: 1 },
|
||||
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::StructEnd,
|
||||
],
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&SkipStruct { a: 1, b: 0 },
|
||||
&[
|
||||
Token::Struct { name: "SkipStruct", len: 1 },
|
||||
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::StructEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
enum SkipSerializingEnum<'a, B, C>
|
||||
where
|
||||
|
||||
@@ -13,7 +13,7 @@ extern crate serde_derive;
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
use std::net;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
use std::default::Default;
|
||||
use std::ffi::{CString, OsString};
|
||||
@@ -710,6 +710,11 @@ declare_tests! {
|
||||
"1.2.3.4:1234".parse::<net::SocketAddrV4>().unwrap() => &[Token::Str("1.2.3.4:1234")],
|
||||
"[::1]:1234".parse::<net::SocketAddrV6>().unwrap() => &[Token::Str("[::1]:1234")],
|
||||
}
|
||||
test_path {
|
||||
Path::new("/usr/local/lib") => &[
|
||||
Token::BorrowedStr("/usr/local/lib"),
|
||||
],
|
||||
}
|
||||
test_path_buf {
|
||||
PathBuf::from("/usr/local/lib") => &[
|
||||
Token::String("/usr/local/lib"),
|
||||
|
||||
@@ -330,6 +330,34 @@ fn test_gen() {
|
||||
struct EmptyArray {
|
||||
empty: [X; 0],
|
||||
}
|
||||
|
||||
enum Or<A, B> {
|
||||
A(A),
|
||||
B(B),
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(untagged, remote = "Or")]
|
||||
enum OrDef<A, B> {
|
||||
#[allow(dead_code)]
|
||||
A(A),
|
||||
#[allow(dead_code)]
|
||||
B(B),
|
||||
}
|
||||
|
||||
struct Str<'a>(&'a str);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(remote = "Str")]
|
||||
struct StrDef<'a>(&'a str);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Remote<'a> {
|
||||
#[serde(with = "OrDef")]
|
||||
or: Or<u8, bool>,
|
||||
#[serde(borrow, with = "StrDef")]
|
||||
s: Str<'a>,
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -695,6 +695,59 @@ fn test_internally_tagged_enum() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_internally_tagged_struct_variant_containing_unit_variant() {
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub enum Level {
|
||||
Info,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "action")]
|
||||
pub enum Message {
|
||||
Log { level: Level },
|
||||
}
|
||||
|
||||
assert_de_tokens(
|
||||
&Message::Log { level: Level::Info },
|
||||
&[
|
||||
Token::Struct { name: "Message", len: 2 },
|
||||
|
||||
Token::Str("action"),
|
||||
Token::Str("Log"),
|
||||
|
||||
Token::Str("level"),
|
||||
Token::BorrowedStr("Info"),
|
||||
|
||||
Token::StructEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_internally_tagged_borrow() {
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum Input<'a> {
|
||||
Package { name: &'a str },
|
||||
}
|
||||
|
||||
assert_tokens(
|
||||
&Input::Package { name: "borrowed" },
|
||||
&[
|
||||
Token::Struct { name: "Input", len: 2 },
|
||||
|
||||
Token::BorrowedStr("type"),
|
||||
Token::BorrowedStr("Package"),
|
||||
|
||||
Token::BorrowedStr("name"),
|
||||
Token::BorrowedStr("borrowed"),
|
||||
|
||||
Token::StructEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_adjacently_tagged_enum() {
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
@@ -751,6 +804,31 @@ fn test_adjacently_tagged_enum() {
|
||||
],
|
||||
);
|
||||
|
||||
// unit with excess content (f, g, h)
|
||||
assert_de_tokens(
|
||||
&AdjacentlyTagged::Unit::<u8>,
|
||||
&[
|
||||
Token::Struct { name: "AdjacentlyTagged", len: 3 },
|
||||
|
||||
Token::Str("f"),
|
||||
Token::Unit,
|
||||
|
||||
Token::Str("t"),
|
||||
Token::Str("Unit"),
|
||||
|
||||
Token::Str("g"),
|
||||
Token::Unit,
|
||||
|
||||
Token::Str("c"),
|
||||
Token::Unit,
|
||||
|
||||
Token::Str("h"),
|
||||
Token::Unit,
|
||||
|
||||
Token::StructEnd,
|
||||
],
|
||||
);
|
||||
|
||||
// newtype with tag first
|
||||
assert_tokens(
|
||||
&AdjacentlyTagged::Newtype::<u8>(1),
|
||||
@@ -860,6 +938,66 @@ fn test_adjacently_tagged_enum() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_adjacently_tagged_enum_deny_unknown_fields() {
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
#[serde(tag = "t", content = "c", deny_unknown_fields)]
|
||||
enum AdjacentlyTagged {
|
||||
Unit,
|
||||
}
|
||||
|
||||
assert_de_tokens(
|
||||
&AdjacentlyTagged::Unit,
|
||||
&[
|
||||
Token::Struct { name: "AdjacentlyTagged", len: 2},
|
||||
|
||||
Token::Str("t"),
|
||||
Token::Str("Unit"),
|
||||
|
||||
Token::Str("c"),
|
||||
Token::Unit,
|
||||
|
||||
Token::StructEnd,
|
||||
],
|
||||
);
|
||||
|
||||
assert_de_tokens_error::<AdjacentlyTagged>(
|
||||
&[
|
||||
Token::Struct { name: "AdjacentlyTagged", len: 3},
|
||||
|
||||
Token::Str("t"),
|
||||
Token::Str("Unit"),
|
||||
|
||||
Token::Str("c"),
|
||||
Token::Unit,
|
||||
|
||||
Token::Str("h"),
|
||||
],
|
||||
r#"invalid value: string "h", expected "t" or "c""#
|
||||
);
|
||||
|
||||
assert_de_tokens_error::<AdjacentlyTagged>(
|
||||
&[
|
||||
Token::Struct { name: "AdjacentlyTagged", len: 3},
|
||||
|
||||
Token::Str("h"),
|
||||
],
|
||||
r#"invalid value: string "h", expected "t" or "c""#
|
||||
);
|
||||
|
||||
assert_de_tokens_error::<AdjacentlyTagged>(
|
||||
&[
|
||||
Token::Struct { name: "AdjacentlyTagged", len: 3},
|
||||
|
||||
Token::Str("c"),
|
||||
Token::Unit,
|
||||
|
||||
Token::Str("h"),
|
||||
],
|
||||
r#"invalid value: string "h", expected "t" or "c""#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_enum_in_internally_tagged_enum() {
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
|
||||
Reference in New Issue
Block a user