@@ -7,6 +7,10 @@ Support code for serialization.
7
7
import list:: list;
8
8
import ebml:: writer;
9
9
10
+ // Set to true to generate more debugging in EBML serialization.
11
+ // Totally lame approach.
12
+ const debug: bool = true ;
13
+
10
14
iface serializer {
11
15
// Primitive types:
12
16
fn emit_nil( ) ;
@@ -86,7 +90,9 @@ enum ebml_serializer_tag {
86
90
es_str,
87
91
es_f64, es_f32, es_float,
88
92
es_enum, es_enum_vid, es_enum_body,
89
- es_vec, es_vec_len, es_vec_elt
93
+ es_vec, es_vec_len, es_vec_elt,
94
+
95
+ es_label // Used only when debugging
90
96
}
91
97
92
98
impl of serializer for ebml:: writer {
@@ -98,6 +104,16 @@ impl of serializer for ebml::writer {
98
104
self . wr_tagged_u32 ( t as uint , v as u32 ) ;
99
105
}
100
106
107
+ fn _emit_label ( label : str ) {
108
+ // There are various strings that we have access to, such as
109
+ // the name of a record field, which do not actually appear in
110
+ // the serialized EBML (normally). This is just for
111
+ // efficiency. When debugging, though, we can emit such
112
+ // labels and then they will be checked by deserializer to
113
+ // try and check failures more quickly.
114
+ if debug { self . wr_tagged_str ( es_label as uint , label) }
115
+ }
116
+
101
117
fn emit_uint ( v : uint ) { self . wr_tagged_u64 ( es_uint as uint , v as u64 ) ; }
102
118
fn emit_u64 ( v : u64 ) { self . wr_tagged_u64 ( es_u64 as uint , v) ; }
103
119
fn emit_u32 ( v : u32 ) { self . wr_tagged_u32 ( es_u32 as uint , v) ; }
@@ -118,7 +134,8 @@ impl of serializer for ebml::writer {
118
134
119
135
fn emit_str ( v : str ) { self . wr_tagged_str ( es_str as uint , v) }
120
136
121
- fn emit_enum ( _name : str , f : fn ( ) ) {
137
+ fn emit_enum ( name : str , f : fn ( ) ) {
138
+ self . _emit_label ( name) ;
122
139
self . wr_tag ( es_enum as uint , f)
123
140
}
124
141
fn emit_enum_variant ( _v_name : str , v_id : uint , _cnt : uint , f : fn ( ) ) {
@@ -138,14 +155,13 @@ impl of serializer for ebml::writer {
138
155
self . wr_tag ( es_vec_elt as uint , f)
139
156
}
140
157
141
- fn emit_vec_elt ( _idx : uint , f : fn ( ) ) {
142
- self . wr_tag ( es_vec_elt as uint , f)
143
- }
144
-
145
158
fn emit_box ( f : fn ( ) ) { f ( ) }
146
159
fn emit_uniq ( f : fn ( ) ) { f ( ) }
147
160
fn emit_rec ( f : fn ( ) ) { f ( ) }
148
- fn emit_rec_field ( _f_name : str , _f_idx : uint , f : fn ( ) ) { f ( ) }
161
+ fn emit_rec_field ( f_name : str , _f_idx : uint , f : fn ( ) ) {
162
+ self . _emit_label ( f_name) ;
163
+ f ( )
164
+ }
149
165
fn emit_tup ( _sz : uint , f : fn ( ) ) { f ( ) }
150
166
fn emit_tup_elt ( _idx : uint , f : fn ( ) ) { f ( ) }
151
167
}
@@ -158,7 +174,22 @@ fn mk_ebml_deserializer(d: ebml::doc) -> ebml_deserializer {
158
174
}
159
175
160
176
impl of deserializer for ebml_deserializer {
177
+ fn _check_label ( lbl : str ) {
178
+ if self . pos < self . parent . end {
179
+ let { tag: r_tag , doc: r_doc } =
180
+ ebml:: doc_at ( self . parent . data , self . pos ) ;
181
+ if r_tag == ( es_label as uint ) {
182
+ self . pos = r_doc. end ;
183
+ let str = ebml:: doc_as_str ( r_doc) ;
184
+ if lbl != str {
185
+ fail #fmt[ "Expected label %s but found %s" , lbl, str ] ;
186
+ }
187
+ }
188
+ }
189
+ }
190
+
161
191
fn next_doc ( exp_tag : ebml_serializer_tag ) -> ebml:: doc {
192
+ #debug[ ". next_doc(exp_tag=%?)" , exp_tag] ;
162
193
if self . pos >= self . parent . end {
163
194
fail "no more documents in current node!" ;
164
195
}
@@ -231,53 +262,68 @@ impl of deserializer for ebml_deserializer {
231
262
fn read_str ( ) -> str { ebml:: doc_as_str ( self . next_doc ( es_str) ) }
232
263
233
264
// Compound types:
234
- fn read_enum < T : copy > ( _name : str , f : fn ( ) -> T ) -> T {
265
+ fn read_enum < T : copy > ( name : str , f : fn ( ) -> T ) -> T {
266
+ #debug[ "read_enum(%s)" , name] ;
267
+ self . _check_label ( name) ;
235
268
self . push_doc ( self . next_doc ( es_enum) , f)
236
269
}
237
270
238
271
fn read_enum_variant < T : copy > ( f : fn ( uint ) -> T ) -> T {
272
+ #debug[ "read_enum_variant()" ] ;
239
273
let idx = self . _next_uint ( es_enum_vid) ;
274
+ #debug[ " idx=%u" , idx] ;
240
275
self . push_doc ( self . next_doc ( es_enum_body) ) { ||
241
276
f ( idx)
242
277
}
243
278
}
244
279
245
- fn read_enum_variant_arg < T : copy > ( _idx : uint , f : fn ( ) -> T ) -> T {
280
+ fn read_enum_variant_arg < T : copy > ( idx : uint , f : fn ( ) -> T ) -> T {
281
+ #debug[ "read_enum_variant_arg(idx=%u)" , idx] ;
246
282
f ( )
247
283
}
248
284
249
285
fn read_vec < T : copy > ( f : fn ( uint ) -> T ) -> T {
286
+ #debug[ "read_vec()" ] ;
250
287
self . push_doc ( self . next_doc ( es_vec) ) { ||
251
288
let len = self . _next_uint ( es_vec_len) ;
289
+ #debug[ " len=%u" , len] ;
252
290
f ( len)
253
291
}
254
292
}
255
293
256
- fn read_vec_elt < T : copy > ( _idx : uint , f : fn ( ) -> T ) -> T {
294
+ fn read_vec_elt < T : copy > ( idx : uint , f : fn ( ) -> T ) -> T {
295
+ #debug[ "read_vec_elt(idx=%u)" , idx] ;
257
296
self . push_doc ( self . next_doc ( es_vec_elt) , f)
258
297
}
259
298
260
299
fn read_box < T : copy > ( f : fn ( ) -> T ) -> T {
300
+ #debug[ "read_box()" ] ;
261
301
f ( )
262
302
}
263
303
264
304
fn read_uniq < T : copy > ( f : fn ( ) -> T ) -> T {
305
+ #debug[ "read_uniq()" ] ;
265
306
f ( )
266
307
}
267
308
268
309
fn read_rec < T : copy > ( f : fn ( ) -> T ) -> T {
310
+ #debug[ "read_rec()" ] ;
269
311
f ( )
270
312
}
271
313
272
- fn read_rec_field < T : copy > ( _f_name : str , _f_idx : uint , f : fn ( ) -> T ) -> T {
314
+ fn read_rec_field < T : copy > ( f_name : str , f_idx : uint , f : fn ( ) -> T ) -> T {
315
+ #debug[ "read_rec_field(%s, idx=%u)" , f_name, f_idx] ;
316
+ self . _check_label ( f_name) ;
273
317
f ( )
274
318
}
275
319
276
- fn read_tup < T : copy > ( _sz : uint , f : fn ( ) -> T ) -> T {
320
+ fn read_tup < T : copy > ( sz : uint , f : fn ( ) -> T ) -> T {
321
+ #debug[ "read_tup(sz=%u)" , sz] ;
277
322
f ( )
278
323
}
279
324
280
- fn read_tup_elt < T : copy > ( _idx : uint , f : fn ( ) -> T ) -> T {
325
+ fn read_tup_elt < T : copy > ( idx : uint , f : fn ( ) -> T ) -> T {
326
+ #debug[ "read_tup_elt(idx=%u)" , idx] ;
281
327
f ( )
282
328
}
283
329
}
0 commit comments