Skip to content

Commit 5df6588

Browse files
committed
Merge branch 'map-support-generic' into WIP-verbose-struct
Conflicts: rmp-serde/src/encode.rs rmp-serde/tests/serializer.rs
2 parents 87ca3f3 + 1296037 commit 5df6588

3 files changed

Lines changed: 89 additions & 56 deletions

File tree

rmp-serde/src/encode.rs

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use serde;
33
use std::fmt;
44
use std::io::Write;
55

6+
use rmp::Marker;
67
use rmp::encode::{
78
write_nil,
89
write_bool,
@@ -58,6 +59,52 @@ impl From<ValueWriteError> for Error {
5859
}
5960
}
6061

62+
pub trait VariantWriter {
63+
fn write_struct_len<W>(&self, wr: &mut W, len: u32) -> Result<Marker, ValueWriteError> where W: Write;
64+
fn write_field_name<W>(&self, wr: &mut W, _key: &str) -> Result<(), ValueWriteError> where W: Write;
65+
}
66+
67+
/// Writes struct as MessagePack array with no field names
68+
pub struct StructArrayWriter;
69+
70+
impl VariantWriter for StructArrayWriter {
71+
fn write_struct_len<W>(&self, wr: &mut W, len: u32) -> Result<Marker, ValueWriteError>
72+
where W: Write
73+
{
74+
write_array_len(wr, len)
75+
}
76+
77+
/// This implementation does not write field names
78+
#[allow(unused_variables)]
79+
fn write_field_name<W>(&self, wr: &mut W, _key: &str) -> Result<(), ValueWriteError>
80+
where W: Write
81+
{
82+
Ok(())
83+
}
84+
}
85+
86+
/// Writes struct as MessagePack map including field names
87+
pub struct StructMapWriter;
88+
89+
impl VariantWriter for StructMapWriter {
90+
fn write_struct_len<W>(&self, wr: &mut W, len: u32) -> Result<Marker, ValueWriteError>
91+
where W: Write
92+
{
93+
write_map_len(wr, len)
94+
}
95+
96+
fn write_field_name<W>(&self, wr: &mut W, _key: &str) -> Result<(), ValueWriteError>
97+
where W: Write
98+
{
99+
write_str(wr, _key)
100+
}
101+
}
102+
103+
/// Creates a new MessagePack encoder with default variant options
104+
pub fn new_default_serializer<'a>(wr: &'a mut Write) -> Serializer<'a, StructArrayWriter> {
105+
Serializer::new(wr, StructArrayWriter)
106+
}
107+
61108
/// Represents MessagePack serialization implementation.
62109
///
63110
/// # Note
@@ -70,31 +117,22 @@ impl From<ValueWriteError> for Error {
70117
/// All instances of `ErrorKind::Interrupted` are handled by this function and the underlying
71118
/// operation is retried.
72119
// TODO: Docs. Examples.
73-
pub struct Serializer<'a> {
120+
pub struct Serializer<'a, W: VariantWriter> {
74121
wr: &'a mut Write,
75-
verbose: bool,
122+
vw: W,
76123
}
77124

78-
impl<'a> Serializer<'a> {
79-
/// Creates a new MessagePack encoder whose output will be written to the writer specified.
80-
pub fn new(wr: &'a mut Write) -> Serializer<'a> {
81-
Serializer {
82-
wr: wr,
83-
verbose: false,
84-
}
85-
}
86-
125+
impl<'a, W: VariantWriter> Serializer<'a, W> {
87126
/// Creates a new MessagePack encoder whose output will be written to the writer specified.
88-
/// structs will be written as maps in MessagePack
89-
pub fn new_verbose(wr: &'a mut Write) -> Serializer<'a> {
127+
pub fn new(wr: &'a mut Write, variant_writer: W) -> Serializer<'a, W> {
90128
Serializer {
91129
wr: wr,
92-
verbose: true,
130+
vw: variant_writer,
93131
}
94132
}
95133
}
96134

97-
impl<'a> serde::Serializer for Serializer<'a> {
135+
impl<'a, W: VariantWriter> serde::Serializer for Serializer<'a, W> {
98136
type Error = Error;
99137

100138
fn visit_unit(&mut self) -> Result<(), Error> {
@@ -287,11 +325,7 @@ impl<'a> serde::Serializer for Serializer<'a> {
287325
None => panic!("do not know how to serialize a sequence with no length"),
288326
};
289327

290-
if self.verbose {
291-
try!(write_map_len(&mut self.wr, len as u32));
292-
} else {
293-
try!(write_array_len(&mut self.wr, len as u32));
294-
}
328+
try!(self.vw.write_struct_len(&mut self.wr, len as u32));
295329

296330
while let Some(()) = try!(visitor.visit(self)) { }
297331

@@ -301,11 +335,7 @@ impl<'a> serde::Serializer for Serializer<'a> {
301335
fn visit_struct_elt<V>(&mut self, _key: &str, value: V) -> Result<(), Error>
302336
where V: serde::Serialize,
303337
{
304-
if self.verbose {
305-
if let Err(e) = write_str(&mut self.wr, _key).map_err(From::from) {
306-
return Err(e);
307-
}
308-
}
338+
try!(self.vw.write_field_name(&mut self.wr, _key));
309339
value.serialize(self)
310340
}
311341

rmp-serde/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ pub mod decode;
55
pub mod encode;
66

77
pub use decode::Deserializer;
8-
pub use encode::Serializer;
8+
pub use encode::{Serializer, new_default_serializer, StructArrayWriter, StructMapWriter};

0 commit comments

Comments
 (0)