mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-24 20:27:57 +00:00
feat(codegen): Add support for #![serde(skip_serialize_if="$expr")]
This allows end users to use an arbitrary expression to decide whether
or not to serialize some field. This expression has access to all the
fields in the struct, but none of the internal state of the Serialize
implementation. For structs, serde implements this by creating a
temporary trait and implementing the struct for it. For struct variants,
the fields are copied by reference into a temporary struct first
before implementing the temporary trait.
This also fixes a bug where the serde_codegen wasn't making calls to
Serializer::serialize_{tuple,struct}_variant{,_elt}.
This commit is contained in:
+17
-2
@@ -503,6 +503,7 @@ fn deserialize_struct(
|
||||
cx,
|
||||
builder,
|
||||
type_path.clone(),
|
||||
&ty,
|
||||
impl_generics,
|
||||
fields,
|
||||
container_attrs
|
||||
@@ -757,6 +758,7 @@ fn deserialize_struct_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_path,
|
||||
&ty,
|
||||
generics,
|
||||
fields,
|
||||
container_attrs,
|
||||
@@ -920,13 +922,19 @@ fn deserialize_struct_visitor(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_path: ast::Path,
|
||||
container_ty: &P<ast::Ty>,
|
||||
generics: &ast::Generics,
|
||||
fields: &[ast::StructField],
|
||||
container_attrs: &attr::ContainerAttrs,
|
||||
) -> Result<(Vec<P<ast::Item>>, ast::Stmt, P<ast::Expr>), Error> {
|
||||
let field_exprs = fields.iter()
|
||||
.map(|field| {
|
||||
let field_attrs = try!(attr::FieldAttrs::from_field(cx, generics, field));
|
||||
let field_attrs = try!(
|
||||
attr::FieldAttrs::from_field(cx,
|
||||
container_ty,
|
||||
generics,
|
||||
field)
|
||||
);
|
||||
Ok(field_attrs.deserialize_name_expr())
|
||||
})
|
||||
.collect();
|
||||
@@ -942,6 +950,7 @@ fn deserialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
struct_path,
|
||||
container_ty,
|
||||
generics,
|
||||
fields,
|
||||
container_attrs,
|
||||
@@ -972,6 +981,7 @@ fn deserialize_map(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_path: ast::Path,
|
||||
container_ty: &P<ast::Ty>,
|
||||
generics: &ast::Generics,
|
||||
fields: &[ast::StructField],
|
||||
container_attrs: &attr::ContainerAttrs,
|
||||
@@ -1011,7 +1021,12 @@ fn deserialize_map(
|
||||
let extract_values = fields.iter()
|
||||
.zip(field_names.iter())
|
||||
.map(|(field, field_name)| {
|
||||
let field_attr = try!(attr::FieldAttrs::from_field(cx, generics, field));
|
||||
let field_attr = try!(
|
||||
attr::FieldAttrs::from_field(cx,
|
||||
container_ty,
|
||||
generics,
|
||||
field)
|
||||
);
|
||||
let missing_expr = field_attr.expr_is_missing();
|
||||
|
||||
Ok(quote_stmt!(cx,
|
||||
|
||||
Reference in New Issue
Block a user