use serde::{
Deserialize, Deserializer, Serialize, Serializer, de,
de::{DeserializeOwned, DeserializeSeed, IntoDeserializer, Visitor},
ser,
};
use crate::{Case, JsonField, JsonShape};
pub(crate) fn to_vec<T: Serialize>(
value: &T,
shape: &JsonShape,
case: Case,
) -> Result<Vec<u8>, serde_json::Error> {
let mut output = Vec::new();
let mut serializer = serde_json::Serializer::new(&mut output);
value.serialize(CaseSerializer {
serializer: &mut serializer,
shape: Some(shape),
case,
})?;
Ok(output)
}
pub(crate) fn from_slice<T: DeserializeOwned>(
bytes: &[u8],
shape: &JsonShape,
case: Case,
) -> Result<T, serde_json::Error> {
let mut deserializer = serde_json::Deserializer::from_slice(bytes);
let value = T::deserialize(CaseDeserializer {
deserializer: &mut deserializer,
shape: Some(shape),
case,
})?;
deserializer.end()?;
Ok(value)
}
struct CaseValue<'value, T: ?Sized> {
value: &'value T,
shape: Option<&'value JsonShape>,
case: Case,
}
impl<T: Serialize + ?Sized> Serialize for CaseValue<'_, T> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.value.serialize(CaseSerializer {
serializer,
shape: self.shape,
case: self.case,
})
}
}
struct CaseSerializer<'shape, S> {
serializer: S,
shape: Option<&'shape JsonShape>,
case: Case,
}
impl<'shape, S: Serializer> Serializer for CaseSerializer<'shape, S> {
type Ok = S::Ok;
type Error = S::Error;
type SerializeSeq = Sequence<'shape, S::SerializeSeq>;
type SerializeTuple = Tuple<'shape, S::SerializeTuple>;
type SerializeTupleStruct = TupleStruct<'shape, S::SerializeTupleStruct>;
type SerializeTupleVariant = TupleVariant<'shape, S::SerializeTupleVariant>;
type SerializeMap = Map<S::SerializeMap>;
type SerializeStruct = Struct<'shape, S::SerializeStruct>;
type SerializeStructVariant = StructVariant<'shape, S::SerializeStructVariant>;
fn serialize_bool(self, value: bool) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_bool(value)
}
fn serialize_i8(self, value: i8) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_i8(value)
}
fn serialize_i16(self, value: i16) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_i16(value)
}
fn serialize_i32(self, value: i32) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_i32(value)
}
fn serialize_i64(self, value: i64) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_i64(value)
}
fn serialize_i128(self, value: i128) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_i128(value)
}
fn serialize_u8(self, value: u8) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_u8(value)
}
fn serialize_u16(self, value: u16) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_u16(value)
}
fn serialize_u32(self, value: u32) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_u32(value)
}
fn serialize_u64(self, value: u64) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_u64(value)
}
fn serialize_u128(self, value: u128) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_u128(value)
}
fn serialize_f32(self, value: f32) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_f32(value)
}
fn serialize_f64(self, value: f64) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_f64(value)
}
fn serialize_char(self, value: char) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_char(value)
}
fn serialize_str(self, value: &str) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_str(value)
}
fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_bytes(value)
}
fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_none()
}
fn serialize_some<T: Serialize + ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_some(&CaseValue {
value,
shape: self.shape,
case: self.case,
})
}
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_unit()
}
fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_unit_struct(name)
}
fn serialize_unit_variant(
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
) -> Result<Self::Ok, Self::Error> {
self.serializer
.serialize_unit_variant(name, variant_index, variant)
}
fn serialize_newtype_struct<T: Serialize + ?Sized>(
self,
name: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error> {
self.serializer.serialize_newtype_struct(
name,
&CaseValue {
value,
shape: self.shape,
case: self.case,
},
)
}
fn serialize_newtype_variant<T: Serialize + ?Sized>(
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error> {
let shape = self
.shape
.and_then(|shape| shape.variant(variant_index))
.map(|variant| variant.shape());
self.serializer.serialize_newtype_variant(
name,
variant_index,
variant,
&CaseValue {
value,
shape,
case: self.case,
},
)
}
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Ok(Sequence {
sequence: self.serializer.serialize_seq(len)?,
shape: self.shape.and_then(JsonShape::items),
case: self.case,
})
}
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
Ok(Tuple {
tuple: self.serializer.serialize_tuple(len)?,
shape: self.shape.and_then(JsonShape::items),
case: self.case,
})
}
fn serialize_tuple_struct(
self,
name: &'static str,
len: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error> {
Ok(TupleStruct {
tuple: self.serializer.serialize_tuple_struct(name, len)?,
shape: self.shape.and_then(JsonShape::items),
case: self.case,
})
}
fn serialize_tuple_variant(
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
let shape = self
.shape
.and_then(|shape| shape.variant(variant_index))
.map(|variant| variant.shape())
.and_then(JsonShape::items);
Ok(TupleVariant {
tuple: self
.serializer
.serialize_tuple_variant(name, variant_index, variant, len)?,
shape,
case: self.case,
})
}
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
Ok(Map {
map: self.serializer.serialize_map(len)?,
})
}
fn serialize_struct(
self,
name: &'static str,
len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
Ok(Struct {
structure: self.serializer.serialize_struct(name, len)?,
fields: self.shape.and_then(JsonShape::fields),
case: self.case,
})
}
fn serialize_struct_variant(
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
let fields = self
.shape
.and_then(|shape| shape.variant(variant_index))
.and_then(|variant| variant.shape().fields());
Ok(StructVariant {
structure: self.serializer.serialize_struct_variant(
name,
variant_index,
variant,
len,
)?,
fields,
case: self.case,
})
}
fn collect_str<T: std::fmt::Display + ?Sized>(
self,
value: &T,
) -> Result<Self::Ok, Self::Error> {
self.serializer.collect_str(value)
}
}
struct Sequence<'shape, S> {
sequence: S,
shape: Option<&'shape JsonShape>,
case: Case,
}
impl<S: ser::SerializeSeq> ser::SerializeSeq for Sequence<'_, S> {
type Ok = S::Ok;
type Error = S::Error;
fn serialize_element<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> {
self.sequence.serialize_element(&CaseValue {
value,
shape: self.shape,
case: self.case,
})
}
fn end(self) -> Result<Self::Ok, Self::Error> {
self.sequence.end()
}
}
struct Tuple<'shape, S> {
tuple: S,
shape: Option<&'shape JsonShape>,
case: Case,
}
impl<S: ser::SerializeTuple> ser::SerializeTuple for Tuple<'_, S> {
type Ok = S::Ok;
type Error = S::Error;
fn serialize_element<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> {
self.tuple.serialize_element(&CaseValue {
value,
shape: self.shape,
case: self.case,
})
}
fn end(self) -> Result<Self::Ok, Self::Error> {
self.tuple.end()
}
}
struct TupleStruct<'shape, S> {
tuple: S,
shape: Option<&'shape JsonShape>,
case: Case,
}
impl<S: ser::SerializeTupleStruct> ser::SerializeTupleStruct for TupleStruct<'_, S> {
type Ok = S::Ok;
type Error = S::Error;
fn serialize_field<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> {
self.tuple.serialize_field(&CaseValue {
value,
shape: self.shape,
case: self.case,
})
}
fn end(self) -> Result<Self::Ok, Self::Error> {
self.tuple.end()
}
}
struct TupleVariant<'shape, S> {
tuple: S,
shape: Option<&'shape JsonShape>,
case: Case,
}
impl<S: ser::SerializeTupleVariant> ser::SerializeTupleVariant for TupleVariant<'_, S> {
type Ok = S::Ok;
type Error = S::Error;
fn serialize_field<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> {
self.tuple.serialize_field(&CaseValue {
value,
shape: self.shape,
case: self.case,
})
}
fn end(self) -> Result<Self::Ok, Self::Error> {
self.tuple.end()
}
}
struct Map<S> {
map: S,
}
impl<S: ser::SerializeMap> ser::SerializeMap for Map<S> {
type Ok = S::Ok;
type Error = S::Error;
fn serialize_key<T: Serialize + ?Sized>(&mut self, key: &T) -> Result<(), Self::Error> {
self.map.serialize_key(key)
}
fn serialize_value<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> {
self.map.serialize_value(value)
}
fn serialize_entry<K: Serialize + ?Sized, V: Serialize + ?Sized>(
&mut self,
key: &K,
value: &V,
) -> Result<(), Self::Error> {
self.map.serialize_entry(key, value)
}
fn end(self) -> Result<Self::Ok, Self::Error> {
self.map.end()
}
}
struct Struct<'shape, S> {
structure: S,
fields: Option<&'shape [JsonField]>,
case: Case,
}
impl<S: ser::SerializeStruct> ser::SerializeStruct for Struct<'_, S> {
type Ok = S::Ok;
type Error = S::Error;
fn serialize_field<T: Serialize + ?Sized>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error> {
let Some(field) = self.fields.and_then(|fields| {
fields
.iter()
.find(|field| field.name().serde_serialize() == key)
}) else {
return self.structure.serialize_field(key, value);
};
self.structure.serialize_field(
field.serialize_name(self.case),
&CaseValue {
value,
shape: Some(field.shape()),
case: self.case,
},
)
}
fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
let key = self
.fields
.and_then(|fields| {
fields
.iter()
.find(|field| field.name().serde_serialize() == key)
})
.map(|field| field.serialize_name(self.case))
.unwrap_or(key);
self.structure.skip_field(key)
}
fn end(self) -> Result<Self::Ok, Self::Error> {
self.structure.end()
}
}
struct StructVariant<'shape, S> {
structure: S,
fields: Option<&'shape [JsonField]>,
case: Case,
}
impl<S: ser::SerializeStructVariant> ser::SerializeStructVariant for StructVariant<'_, S> {
type Ok = S::Ok;
type Error = S::Error;
fn serialize_field<T: Serialize + ?Sized>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error> {
let Some(field) = self.fields.and_then(|fields| {
fields
.iter()
.find(|field| field.name().serde_serialize() == key)
}) else {
return self.structure.serialize_field(key, value);
};
self.structure.serialize_field(
field.serialize_name(self.case),
&CaseValue {
value,
shape: Some(field.shape()),
case: self.case,
},
)
}
fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
let key = self
.fields
.and_then(|fields| {
fields
.iter()
.find(|field| field.name().serde_serialize() == key)
})
.map(|field| field.serialize_name(self.case))
.unwrap_or(key);
self.structure.skip_field(key)
}
fn end(self) -> Result<Self::Ok, Self::Error> {
self.structure.end()
}
}
struct CaseDeserializer<'shape, D> {
deserializer: D,
shape: Option<&'shape JsonShape>,
case: Case,
}
impl<'de, D: Deserializer<'de>> Deserializer<'de> for CaseDeserializer<'_, D> {
type Error = D::Error;
fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
self.deserializer.deserialize_any(visitor)
}
fn deserialize_option<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
self.deserializer.deserialize_option(OptionVisitor {
visitor,
shape: self.shape,
case: self.case,
})
}
fn deserialize_newtype_struct<V: Visitor<'de>>(
self,
name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error> {
self.deserializer.deserialize_newtype_struct(
name,
NewtypeVisitor {
visitor,
shape: self.shape,
case: self.case,
},
)
}
fn deserialize_seq<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
self.deserializer.deserialize_seq(SequenceVisitor {
visitor,
shape: self.shape.and_then(JsonShape::items),
case: self.case,
})
}
fn deserialize_tuple<V: Visitor<'de>>(
self,
len: usize,
visitor: V,
) -> Result<V::Value, Self::Error> {
self.deserializer.deserialize_tuple(
len,
SequenceVisitor {
visitor,
shape: self.shape.and_then(JsonShape::items),
case: self.case,
},
)
}
fn deserialize_tuple_struct<V: Visitor<'de>>(
self,
name: &'static str,
len: usize,
visitor: V,
) -> Result<V::Value, Self::Error> {
self.deserializer.deserialize_tuple_struct(
name,
len,
SequenceVisitor {
visitor,
shape: self.shape.and_then(JsonShape::items),
case: self.case,
},
)
}
fn deserialize_map<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
match self.shape.and_then(JsonShape::fields) {
Some(fields) => self.deserializer.deserialize_map(ObjectVisitor {
visitor,
fields: Some(fields),
case: self.case,
}),
None => self.deserializer.deserialize_map(visitor),
}
}
fn deserialize_struct<V: Visitor<'de>>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error> {
self.deserializer.deserialize_struct(
name,
fields,
ObjectVisitor {
visitor,
fields: self.shape.and_then(JsonShape::fields),
case: self.case,
},
)
}
fn deserialize_enum<V: Visitor<'de>>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error> {
match self.shape.and_then(JsonShape::variants_slice) {
Some(json_variants) => self.deserializer.deserialize_enum(
name,
variants,
EnumVisitor {
visitor,
variants: json_variants,
case: self.case,
},
),
None => self.deserializer.deserialize_enum(name, variants, visitor),
}
}
serde::forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf unit unit_struct identifier ignored_any
}
}
struct OptionVisitor<'shape, V> {
visitor: V,
shape: Option<&'shape JsonShape>,
case: Case,
}
impl<'de, V: Visitor<'de>> Visitor<'de> for OptionVisitor<'_, V> {
type Value = V::Value;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.visitor.expecting(formatter)
}
fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
self.visitor.visit_none()
}
fn visit_some<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Value, D::Error> {
self.visitor.visit_some(CaseDeserializer {
deserializer,
shape: self.shape,
case: self.case,
})
}
fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
self.visitor.visit_unit()
}
}
struct NewtypeVisitor<'shape, V> {
visitor: V,
shape: Option<&'shape JsonShape>,
case: Case,
}
impl<'de, V: Visitor<'de>> Visitor<'de> for NewtypeVisitor<'_, V> {
type Value = V::Value;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.visitor.expecting(formatter)
}
fn visit_newtype_struct<D: Deserializer<'de>>(
self,
deserializer: D,
) -> Result<Self::Value, D::Error> {
self.visitor.visit_newtype_struct(CaseDeserializer {
deserializer,
shape: self.shape,
case: self.case,
})
}
}
struct SequenceVisitor<'shape, V> {
visitor: V,
shape: Option<&'shape JsonShape>,
case: Case,
}
impl<'de, V: Visitor<'de>> Visitor<'de> for SequenceVisitor<'_, V> {
type Value = V::Value;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.visitor.expecting(formatter)
}
fn visit_seq<A: de::SeqAccess<'de>>(self, sequence: A) -> Result<Self::Value, A::Error> {
self.visitor.visit_seq(SequenceAccess {
sequence,
shape: self.shape,
case: self.case,
})
}
}
struct SequenceAccess<'shape, A> {
sequence: A,
shape: Option<&'shape JsonShape>,
case: Case,
}
impl<'de, A: de::SeqAccess<'de>> de::SeqAccess<'de> for SequenceAccess<'_, A> {
type Error = A::Error;
fn next_element_seed<T: DeserializeSeed<'de>>(
&mut self,
seed: T,
) -> Result<Option<T::Value>, Self::Error> {
match self.shape {
Some(shape) => self.sequence.next_element_seed(CaseSeed {
seed,
shape,
case: self.case,
}),
None => self.sequence.next_element_seed(seed),
}
}
fn size_hint(&self) -> Option<usize> {
self.sequence.size_hint()
}
}
struct ObjectVisitor<'shape, V> {
visitor: V,
fields: Option<&'shape [JsonField]>,
case: Case,
}
impl<'de, V: Visitor<'de>> Visitor<'de> for ObjectVisitor<'_, V> {
type Value = V::Value;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.visitor.expecting(formatter)
}
fn visit_map<A: de::MapAccess<'de>>(self, map: A) -> Result<Self::Value, A::Error> {
self.visitor.visit_map(ObjectAccess {
map,
fields: self.fields,
next_shape: None,
case: self.case,
})
}
}
struct ObjectAccess<'shape, A> {
map: A,
fields: Option<&'shape [JsonField]>,
next_shape: Option<&'shape JsonShape>,
case: Case,
}
impl<'de, A: de::MapAccess<'de>> de::MapAccess<'de> for ObjectAccess<'_, A> {
type Error = A::Error;
fn next_key_seed<K: DeserializeSeed<'de>>(
&mut self,
seed: K,
) -> Result<Option<K::Value>, Self::Error> {
let mut next_shape = None;
let key = self.map.next_key_seed(FieldSeed {
seed,
fields: self.fields,
next_shape: &mut next_shape,
case: self.case,
})?;
self.next_shape = next_shape;
Ok(key)
}
fn next_value_seed<V: DeserializeSeed<'de>>(
&mut self,
seed: V,
) -> Result<V::Value, Self::Error> {
match self.next_shape.take() {
Some(shape) => self.map.next_value_seed(CaseSeed {
seed,
shape,
case: self.case,
}),
None => self.map.next_value_seed(seed),
}
}
fn size_hint(&self) -> Option<usize> {
self.map.size_hint()
}
}
struct FieldSeed<'shape, 'next, S> {
seed: S,
fields: Option<&'shape [JsonField]>,
next_shape: &'next mut Option<&'shape JsonShape>,
case: Case,
}
impl<'de, S: DeserializeSeed<'de>> DeserializeSeed<'de> for FieldSeed<'_, '_, S> {
type Value = S::Value;
fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Value, D::Error> {
let key = String::deserialize(deserializer)?;
let field = self.fields.and_then(|fields| {
fields
.iter()
.find(|field| field.deserialize_name(self.case) == key)
});
*self.next_shape = field.map(JsonField::shape);
let key = field
.map(|field| field.name().serde_deserialize().to_owned())
.unwrap_or(key);
self.seed.deserialize(key.into_deserializer())
}
}
struct CaseSeed<'shape, S> {
seed: S,
shape: &'shape JsonShape,
case: Case,
}
impl<'de, S: DeserializeSeed<'de>> DeserializeSeed<'de> for CaseSeed<'_, S> {
type Value = S::Value;
fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Value, D::Error> {
self.seed.deserialize(CaseDeserializer {
deserializer,
shape: Some(self.shape),
case: self.case,
})
}
}
struct EnumVisitor<'shape, V> {
visitor: V,
variants: &'shape [crate::JsonVariant],
case: Case,
}
impl<'de, V: Visitor<'de>> Visitor<'de> for EnumVisitor<'_, V> {
type Value = V::Value;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.visitor.expecting(formatter)
}
fn visit_enum<A: de::EnumAccess<'de>>(self, access: A) -> Result<Self::Value, A::Error> {
self.visitor.visit_enum(EnumAccess {
access,
variants: self.variants,
case: self.case,
})
}
}
struct EnumAccess<'shape, A> {
access: A,
variants: &'shape [crate::JsonVariant],
case: Case,
}
impl<'de, 'shape, A: de::EnumAccess<'de>> de::EnumAccess<'de> for EnumAccess<'shape, A> {
type Error = A::Error;
type Variant = VariantAccess<'shape, A::Variant>;
fn variant_seed<V: DeserializeSeed<'de>>(
self,
seed: V,
) -> Result<(V::Value, Self::Variant), Self::Error> {
let selected = std::cell::Cell::new(None);
let (value, variant) = self.access.variant_seed(VariantSeed {
seed,
variants: self.variants,
selected: &selected,
case: self.case,
})?;
let shape = selected
.get()
.and_then(|index| self.variants.get(index))
.map(|variant| variant.shape());
Ok((
value,
VariantAccess {
variant,
shape,
case: self.case,
},
))
}
}
struct VariantSeed<'shape, 'selected, S> {
seed: S,
variants: &'shape [crate::JsonVariant],
selected: &'selected std::cell::Cell<Option<usize>>,
case: Case,
}
impl<'de, S: DeserializeSeed<'de>> DeserializeSeed<'de> for VariantSeed<'_, '_, S> {
type Value = S::Value;
fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Value, D::Error> {
let key = String::deserialize(deserializer)?;
let selected = self
.variants
.iter()
.position(|variant| variant.name().deserialize(Some(self.case)) == key);
self.selected.set(selected);
let key = selected
.and_then(|index| self.variants.get(index))
.map(|variant| variant.name().serde_deserialize().to_owned())
.unwrap_or(key);
self.seed.deserialize(key.into_deserializer())
}
}
struct VariantAccess<'shape, A> {
variant: A,
shape: Option<&'shape JsonShape>,
case: Case,
}
impl<'de, A: de::VariantAccess<'de>> de::VariantAccess<'de> for VariantAccess<'_, A> {
type Error = A::Error;
fn unit_variant(self) -> Result<(), Self::Error> {
self.variant.unit_variant()
}
fn newtype_variant_seed<T: DeserializeSeed<'de>>(
self,
seed: T,
) -> Result<T::Value, Self::Error> {
match self.shape {
Some(shape) => self.variant.newtype_variant_seed(CaseSeed {
seed,
shape,
case: self.case,
}),
None => self.variant.newtype_variant_seed(seed),
}
}
fn tuple_variant<V: Visitor<'de>>(
self,
len: usize,
visitor: V,
) -> Result<V::Value, Self::Error> {
self.variant.tuple_variant(
len,
SequenceVisitor {
visitor,
shape: self.shape.and_then(JsonShape::items),
case: self.case,
},
)
}
fn struct_variant<V: Visitor<'de>>(
self,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error> {
self.variant.struct_variant(
fields,
ObjectVisitor {
visitor,
fields: self.shape.and_then(JsonShape::fields),
case: self.case,
},
)
}
}