mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-13 17:11:02 +00:00
Change de::VariantVisitor to let deserializers know the variant kind
This allows formats like cbor that encode a unit variant as just a string to work. [breaking-change]
This commit is contained in:
@@ -339,10 +339,8 @@ mod deserializer {
|
|||||||
de::Deserialize::deserialize(self.de)
|
de::Deserialize::deserialize(self.de)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_value<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
fn visit_unit(&mut self) -> Result<(), Error> {
|
||||||
where V: de::Visitor,
|
de::Deserialize::deserialize(self.de)
|
||||||
{
|
|
||||||
de::Deserializer::visit(self.de, visitor)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -360,7 +358,7 @@ mod deserializer {
|
|||||||
de::Deserialize::deserialize(self.de)
|
de::Deserialize::deserialize(self.de)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_value<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor,
|
||||||
{
|
{
|
||||||
visitor.visit_seq(self)
|
visitor.visit_seq(self)
|
||||||
|
|||||||
@@ -417,7 +417,7 @@ fn deserialize_variant(
|
|||||||
match variant.node.kind {
|
match variant.node.kind {
|
||||||
ast::TupleVariantKind(ref args) if args.is_empty() => {
|
ast::TupleVariantKind(ref args) if args.is_empty() => {
|
||||||
quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
try!(visitor.visit_value(::serde::de::impls::UnitVisitor));
|
try!(visitor.visit_unit());
|
||||||
Ok($type_ident::$variant_ident)
|
Ok($type_ident::$variant_ident)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -482,7 +482,7 @@ fn deserialize_tuple_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
visitor.visit_value($visitor_expr)
|
visitor.visit_seq($visitor_expr)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -524,7 +524,7 @@ fn deserialize_struct_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
visitor.visit_value($visitor_expr)
|
visitor.visit_map($visitor_expr)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+35
-4
@@ -385,6 +385,8 @@ impl<'a, V_> MapVisitor for &'a mut V_ where V_: MapVisitor {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// `EnumVisitor` is a visitor that is created by the `Deserialize` and passed to the
|
||||||
|
/// `Deserializer` in order to deserialize enums.
|
||||||
pub trait EnumVisitor {
|
pub trait EnumVisitor {
|
||||||
type Value;
|
type Value;
|
||||||
|
|
||||||
@@ -394,14 +396,33 @@ pub trait EnumVisitor {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// `VariantVisitor` is a visitor that is created by the `Deserializer` and passed to the
|
||||||
|
/// `Deserialize` in order to deserialize a specific enum variant.
|
||||||
pub trait VariantVisitor {
|
pub trait VariantVisitor {
|
||||||
type Error: Error;
|
type Error: Error;
|
||||||
|
|
||||||
|
/// `visit_variant` is called to identify which variant to deserialize.
|
||||||
fn visit_variant<V>(&mut self) -> Result<V, Self::Error>
|
fn visit_variant<V>(&mut self) -> Result<V, Self::Error>
|
||||||
where V: Deserialize;
|
where V: Deserialize;
|
||||||
|
|
||||||
fn visit_value<V>(&mut self, _visitor: V) -> Result<V::Value, Self::Error>
|
/// `visit_unit` is called when deserializing a variant with no values.
|
||||||
where V: Visitor;
|
fn visit_unit(&mut self) -> Result<(), Self::Error> {
|
||||||
|
Err(Error::syntax_error())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `visit_seq` is called when deserializing a tuple-like variant.
|
||||||
|
fn visit_seq<V>(&mut self, _visitor: V) -> Result<V::Value, Self::Error>
|
||||||
|
where V: Visitor
|
||||||
|
{
|
||||||
|
Err(Error::syntax_error())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `visit_map` is called when deserializing a struct-like variant.
|
||||||
|
fn visit_map<V>(&mut self, _visitor: V) -> Result<V::Value, Self::Error>
|
||||||
|
where V: Visitor
|
||||||
|
{
|
||||||
|
Err(Error::syntax_error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> VariantVisitor for &'a mut T where T: VariantVisitor {
|
impl<'a, T> VariantVisitor for &'a mut T where T: VariantVisitor {
|
||||||
@@ -413,10 +434,20 @@ impl<'a, T> VariantVisitor for &'a mut T where T: VariantVisitor {
|
|||||||
(**self).visit_variant()
|
(**self).visit_variant()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_value<V>(&mut self, visitor: V) -> Result<V::Value, T::Error>
|
fn visit_unit(&mut self) -> Result<(), T::Error> {
|
||||||
|
(**self).visit_unit()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, T::Error>
|
||||||
where V: Visitor,
|
where V: Visitor,
|
||||||
{
|
{
|
||||||
(**self).visit_value(visitor)
|
(**self).visit_seq(visitor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, T::Error>
|
||||||
|
where V: Visitor,
|
||||||
|
{
|
||||||
|
(**self).visit_map(visitor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-8
@@ -149,10 +149,8 @@ impl<'a> de::VariantVisitor for StrDeserializer<'a> {
|
|||||||
de::Deserialize::deserialize(self)
|
de::Deserialize::deserialize(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_value<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
fn visit_unit(&mut self) -> Result<(), Error> {
|
||||||
where V: de::Visitor,
|
Ok(())
|
||||||
{
|
|
||||||
visitor.visit_unit()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,10 +195,8 @@ impl<'a> de::VariantVisitor for StringDeserializer {
|
|||||||
de::Deserialize::deserialize(self)
|
de::Deserialize::deserialize(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_value<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
fn visit_unit(&mut self) -> Result<(), Error> {
|
||||||
where V: de::Visitor,
|
Ok(())
|
||||||
{
|
|
||||||
visitor.visit_unit()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+15
-1
@@ -618,7 +618,21 @@ impl<Iter> de::VariantVisitor for Deserializer<Iter>
|
|||||||
de::Deserialize::deserialize(self)
|
de::Deserialize::deserialize(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_value<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
fn visit_unit(&mut self) -> Result<(), Error> {
|
||||||
|
try!(self.parse_object_colon());
|
||||||
|
|
||||||
|
de::Deserialize::deserialize(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||||
|
where V: de::Visitor,
|
||||||
|
{
|
||||||
|
try!(self.parse_object_colon());
|
||||||
|
|
||||||
|
de::Deserializer::visit(self, visitor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor,
|
||||||
{
|
{
|
||||||
try!(self.parse_object_colon());
|
try!(self.parse_object_colon());
|
||||||
|
|||||||
+23
-2
@@ -737,7 +737,18 @@ impl<'a> de::VariantVisitor for SeqDeserializer<'a> {
|
|||||||
de::Deserialize::deserialize(self.de)
|
de::Deserialize::deserialize(self.de)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_value<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
fn visit_unit(&mut self) -> Result<(), Error>
|
||||||
|
{
|
||||||
|
de::Deserialize::deserialize(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||||
|
where V: de::Visitor,
|
||||||
|
{
|
||||||
|
de::Deserializer::visit(self, visitor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor,
|
||||||
{
|
{
|
||||||
de::Deserializer::visit(self, visitor)
|
de::Deserializer::visit(self, visitor)
|
||||||
@@ -836,7 +847,17 @@ impl<'a> de::VariantVisitor for MapDeserializer<'a> {
|
|||||||
de::Deserialize::deserialize(self.de)
|
de::Deserialize::deserialize(self.de)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_value<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
fn visit_unit(&mut self) -> Result<(), Error> {
|
||||||
|
de::Deserialize::deserialize(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||||
|
where V: de::Visitor,
|
||||||
|
{
|
||||||
|
de::Deserializer::visit(self, visitor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor,
|
||||||
{
|
{
|
||||||
de::Deserializer::visit(self, visitor)
|
de::Deserializer::visit(self, visitor)
|
||||||
|
|||||||
+11
-1
@@ -315,7 +315,17 @@ impl<'a> de::VariantVisitor for TokenDeserializerVariantVisitor<'a> {
|
|||||||
de::Deserialize::deserialize(self.de)
|
de::Deserialize::deserialize(self.de)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_value<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
fn visit_unit(&mut self) -> Result<(), Error> {
|
||||||
|
de::Deserialize::deserialize(self.de)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||||
|
where V: de::Visitor,
|
||||||
|
{
|
||||||
|
de::Deserializer::visit(self.de, visitor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor,
|
||||||
{
|
{
|
||||||
de::Deserializer::visit(self.de, visitor)
|
de::Deserializer::visit(self.de, visitor)
|
||||||
|
|||||||
Reference in New Issue
Block a user