mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-12 15:31:07 +00:00
Restore visit_bytes for identifying variants and fields
This commit is contained in:
+28
-3
@@ -1,5 +1,5 @@
|
|||||||
use syn::{self, aster, Ident};
|
use syn::{self, aster, Ident};
|
||||||
use quote::Tokens;
|
use quote::{self, Tokens};
|
||||||
|
|
||||||
use bound;
|
use bound;
|
||||||
use internals::ast::{Body, Field, Item, Style, Variant};
|
use internals::ast::{Body, Field, Item, Style, Variant};
|
||||||
@@ -648,7 +648,8 @@ fn deserialize_field_visitor(
|
|||||||
item_attrs: &attr::Item,
|
item_attrs: &attr::Item,
|
||||||
is_variant: bool,
|
is_variant: bool,
|
||||||
) -> Tokens {
|
) -> Tokens {
|
||||||
let field_names = fields.iter().map(|&(ref name, _)| name);
|
let field_strs = fields.iter().map(|&(ref name, _)| name);
|
||||||
|
let field_bytes = fields.iter().map(|&(ref name, _)| quote::ByteStr(name));
|
||||||
let field_idents: &Vec<_> = &fields.iter().map(|&(_, ref ident)| ident).collect();
|
let field_idents: &Vec<_> = &fields.iter().map(|&(_, ref ident)| ident).collect();
|
||||||
|
|
||||||
let ignore_variant = if is_variant || item_attrs.deny_unknown_fields() {
|
let ignore_variant = if is_variant || item_attrs.deny_unknown_fields() {
|
||||||
@@ -690,6 +691,16 @@ fn deserialize_field_visitor(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let bytes_to_str = if is_variant || item_attrs.deny_unknown_fields() {
|
||||||
|
Some(quote! {
|
||||||
|
// TODO https://github.com/serde-rs/serde/issues/666
|
||||||
|
// update this to use str::from_utf8(value).unwrap_or("���") on no_std
|
||||||
|
let value = &::std::string::String::from_utf8_lossy(value);
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
enum __Field {
|
enum __Field {
|
||||||
@@ -714,11 +725,25 @@ fn deserialize_field_visitor(
|
|||||||
{
|
{
|
||||||
match value {
|
match value {
|
||||||
#(
|
#(
|
||||||
#field_names => Ok(__Field::#field_idents),
|
#field_strs => Ok(__Field::#field_idents),
|
||||||
)*
|
)*
|
||||||
_ => #fallthrough_arm
|
_ => #fallthrough_arm
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn visit_bytes<__E>(self, value: &[u8]) -> ::std::result::Result<__Field, __E>
|
||||||
|
where __E: _serde::de::Error
|
||||||
|
{
|
||||||
|
match value {
|
||||||
|
#(
|
||||||
|
#field_bytes => Ok(__Field::#field_idents),
|
||||||
|
)*
|
||||||
|
_ => {
|
||||||
|
#bytes_to_str
|
||||||
|
#fallthrough_arm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
deserializer.deserialize_struct_field(__FieldVisitor)
|
deserializer.deserialize_struct_field(__FieldVisitor)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::net;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use serde::de::{Deserialize, Type};
|
use serde::de::Deserialize;
|
||||||
|
|
||||||
extern crate fnv;
|
extern crate fnv;
|
||||||
use self::fnv::FnvHasher;
|
use self::fnv::FnvHasher;
|
||||||
@@ -781,6 +781,13 @@ declare_tests! {
|
|||||||
Token::Unit,
|
Token::Unit,
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
test_enum_unit_bytes {
|
||||||
|
Enum::Unit => &[
|
||||||
|
Token::EnumStart("Enum"),
|
||||||
|
Token::Bytes(b"Unit"),
|
||||||
|
Token::Unit,
|
||||||
|
],
|
||||||
|
}
|
||||||
test_box {
|
test_box {
|
||||||
Box::new(0i32) => &[Token::I32(0)],
|
Box::new(0i32) => &[Token::I32(0)],
|
||||||
}
|
}
|
||||||
@@ -933,12 +940,4 @@ declare_error_tests! {
|
|||||||
],
|
],
|
||||||
Error::InvalidValue("expected variant index 0 <= i < 4".to_owned()),
|
Error::InvalidValue("expected variant index 0 <= i < 4".to_owned()),
|
||||||
}
|
}
|
||||||
test_enum_unit_bytes<Enum> {
|
|
||||||
&[
|
|
||||||
Token::EnumStart("Enum"),
|
|
||||||
Token::Bytes(b"Unit"),
|
|
||||||
Token::Unit,
|
|
||||||
],
|
|
||||||
Error::InvalidType(Type::Bytes),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user