From f500db6e91d46a9f61a7e9775137deb2ba45fed2 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 14 Feb 2017 17:36:50 -0800 Subject: [PATCH] Pair serialize_with and deserialize_with into one attribute --- serde_codegen_internals/src/attr.rs | 12 ++++++++++++ test_suite/tests/test_gen.rs | 18 +++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/serde_codegen_internals/src/attr.rs b/serde_codegen_internals/src/attr.rs index 224bb939..2ed7bf42 100644 --- a/serde_codegen_internals/src/attr.rs +++ b/serde_codegen_internals/src/attr.rs @@ -453,6 +453,18 @@ impl Field { } } + // Parse `#[serde(with="...")]` + MetaItem(NameValue(ref name, ref lit)) if name == "with" => { + if let Ok(path) = parse_lit_into_path(cx, name.as_ref(), lit) { + let mut ser_path = path.clone(); + ser_path.segments.push("serialize".into()); + serialize_with.set(ser_path); + let mut de_path = path; + de_path.segments.push("deserialize".into()); + deserialize_with.set(de_path); + } + } + // Parse `#[serde(bound="D: Serialize")]` MetaItem(NameValue(ref name, ref lit)) if name == "bound" => { if let Ok(where_predicates) = diff --git a/test_suite/tests/test_gen.rs b/test_suite/tests/test_gen.rs index 2f53a00e..d008c0f2 100644 --- a/test_suite/tests/test_gen.rs +++ b/test_suite/tests/test_gen.rs @@ -30,6 +30,14 @@ fn test_gen() { } assert::>(); + #[derive(Serialize, Deserialize)] + struct WithTogether { + t: T, + #[serde(with="both_x")] + x: X, + } + assert::>(); + #[derive(Serialize, Deserialize)] struct WithRef<'a, T: 'a> { #[serde(skip_deserializing)] @@ -307,16 +315,20 @@ trait DeserializeWith: Sized { } // Implements neither Serialize nor Deserialize -struct X; +pub struct X; -fn ser_x(_: &X, _: S) -> StdResult { +pub fn ser_x(_: &X, _: S) -> StdResult { unimplemented!() } -fn de_x(_: D) -> StdResult { +pub fn de_x(_: D) -> StdResult { unimplemented!() } +mod both_x { + pub use super::{ser_x as serialize, de_x as deserialize}; +} + impl SerializeWith for X { fn serialize_with(_: &Self, _: S) -> StdResult { unimplemented!()