@@ -3,6 +3,7 @@ use serde;
33use std:: fmt;
44use std:: io:: Write ;
55
6+ use rmp:: Marker ;
67use 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,20 +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 ,
122+ vw : W ,
75123}
76124
77- impl < ' a > Serializer < ' a > {
125+ impl < ' a , W : VariantWriter > Serializer < ' a , W > {
78126 /// Creates a new MessagePack encoder whose output will be written to the writer specified.
79- pub fn new ( wr : & ' a mut Write ) -> Serializer < ' a > {
127+ pub fn new ( wr : & ' a mut Write , variant_writer : W ) -> Serializer < ' a , W > {
80128 Serializer {
81129 wr : wr,
130+ vw : variant_writer,
82131 }
83132 }
84133}
85134
86- impl < ' a > serde:: Serializer for Serializer < ' a > {
135+ impl < ' a , W : VariantWriter > serde:: Serializer for Serializer < ' a , W > {
87136 type Error = Error ;
88137
89138 fn visit_unit ( & mut self ) -> Result < ( ) , Error > {
@@ -276,7 +325,7 @@ impl<'a> serde::Serializer for Serializer<'a> {
276325 None => panic ! ( "do not know how to serialize a sequence with no length" ) ,
277326 } ;
278327
279- try!( write_array_len ( & mut self . wr , len as u32 ) ) ;
328+ try!( self . vw . write_struct_len ( & mut self . wr , len as u32 ) ) ;
280329
281330 while let Some ( ( ) ) = try!( visitor. visit ( self ) ) { }
282331
@@ -286,6 +335,7 @@ impl<'a> serde::Serializer for Serializer<'a> {
286335 fn visit_struct_elt < V > ( & mut self , _key : & str , value : V ) -> Result < ( ) , Error >
287336 where V : serde:: Serialize ,
288337 {
338+ try!( self . vw . write_field_name ( & mut self . wr , _key) ) ;
289339 value. serialize ( self )
290340 }
291341
0 commit comments