Skip to content

Commit 83351d1

Browse files
Árpád Goretity zonyitoo
Árpád Goretity 
authored andcommitted
Fix serialization of newtype structs, simplify serialization of newtype and tuple variants (#104)
* Add unit tests for differentiating between newtype and tuple structs and variants * Fix serialization of newtype struct, and simplify serialization of newtype and tuple variants
1 parent 4058d4f commit 83351d1

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

src/encoder/serde.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ impl Serializer for Encoder {
207207
fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, value: &T) -> EncoderResult<Bson>
208208
where T: Serialize
209209
{
210-
let mut ser = TupleStructSerializer { inner: Array::new(), };
211-
ser.serialize_field(value)?;
212-
ser.end()
210+
value.serialize(self)
213211
}
214212

215213
#[inline]
@@ -221,10 +219,9 @@ impl Serializer for Encoder {
221219
-> EncoderResult<Bson>
222220
where T: Serialize
223221
{
224-
let mut ser = TupleVariantSerializer { inner: Array::new(),
225-
name: variant, };
226-
ser.serialize_field(value)?;
227-
ser.end()
222+
let mut newtype_variant = Document::new();
223+
newtype_variant.insert(variant, to_bson(value)?);
224+
Ok(newtype_variant.into())
228225
}
229226

230227
#[inline]
@@ -350,13 +347,8 @@ impl SerializeTupleVariant for TupleVariantSerializer {
350347

351348
fn end(self) -> EncoderResult<Bson> {
352349
let mut tuple_variant = Document::new();
353-
if self.inner.len() == 1 {
354-
tuple_variant.insert(self.name, self.inner.into_iter().next().unwrap());
355-
} else {
356-
tuple_variant.insert(self.name, Bson::Array(self.inner));
357-
}
358-
359-
Ok(Bson::Document(tuple_variant))
350+
tuple_variant.insert(self.name, self.inner);
351+
Ok(tuple_variant.into())
360352
}
361353
}
362354

tests/serde.rs

+72
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,75 @@ fn test_serde_bytes() {
169169
let f = bson::from_bson::<Foo>(b).unwrap();
170170
assert_eq!(x, f);
171171
}
172+
173+
#[test]
174+
fn test_serde_newtype_struct() {
175+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
176+
struct Email(String);
177+
178+
let email_1 = Email(String::from("[email protected]"));
179+
let b = bson::to_bson(&email_1).unwrap();
180+
assert_eq!(b, Bson::String(email_1.0));
181+
182+
let s = String::from("[email protected]");
183+
let de = Bson::String(s.clone());
184+
let email_2 = bson::from_bson::<Email>(de).unwrap();
185+
assert_eq!(email_2, Email(s));
186+
}
187+
188+
#[test]
189+
fn test_serde_tuple_struct() {
190+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
191+
struct Name(String, String); // first, last
192+
193+
let name_1 = Name(String::from("Graydon"), String::from("Hoare"));
194+
let b = bson::to_bson(&name_1).unwrap();
195+
assert_eq!(b, bson!([
196+
name_1.0.clone(),
197+
name_1.1.clone(),
198+
]));
199+
200+
let (first, last) = (String::from("Donald"), String::from("Knuth"));
201+
let de = bson!([first.clone(), last.clone()]);
202+
let name_2 = bson::from_bson::<Name>(de).unwrap();
203+
assert_eq!(name_2, Name(first, last));
204+
}
205+
206+
#[test]
207+
fn test_serde_newtype_variant() {
208+
#[derive(Debug, PartialEq, Serialize, Deserialize)]
209+
#[serde(tag = "type", content = "value")]
210+
enum Number {
211+
Int(i64),
212+
Float(f64),
213+
}
214+
215+
let n = 42;
216+
let num_1 = Number::Int(n);
217+
let b = bson::to_bson(&num_1).unwrap();
218+
assert_eq!(b, bson!({ "type": "Int", "value": n }));
219+
220+
let x = 1337.0;
221+
let de = bson!({ "type": "Float", "value": x });
222+
let num_2 = bson::from_bson::<Number>(de).unwrap();
223+
assert_eq!(num_2, Number::Float(x));
224+
}
225+
226+
#[test]
227+
fn test_serde_tuple_variant() {
228+
#[derive(Debug, PartialEq, Serialize, Deserialize)]
229+
enum Point {
230+
TwoDim(f64, f64),
231+
ThreeDim(f64, f64, f64),
232+
}
233+
234+
let (x1, y1) = (3.14, -2.71);
235+
let p1 = Point::TwoDim(x1, y1);
236+
let b = bson::to_bson(&p1).unwrap();
237+
assert_eq!(b, bson!({ "TwoDim": [x1, y1] }));
238+
239+
let (x2, y2, z2) = (0.0, -13.37, 4.2);
240+
let de = bson!({ "ThreeDim": [x2, y2, z2] });
241+
let p2 = bson::from_bson::<Point>(de).unwrap();
242+
assert_eq!(p2, Point::ThreeDim(x2, y2, z2));
243+
}

0 commit comments

Comments
 (0)