@@ -121,57 +121,26 @@ pub impl Encoder: serialize::Encoder {
121
121
fn emit_owned ( & self , f : fn ( ) ) { f ( ) }
122
122
fn emit_managed ( & self , f : fn ( ) ) { f ( ) }
123
123
124
- fn emit_enum ( & self , _name : & str , f : fn ( ) ) {
124
+ fn emit_enum ( & self , name : & str , f : fn ( ) ) {
125
+ if name != "option" { die ! ( ~"only supports option enum ") }
125
126
f()
126
127
}
127
-
128
- fn emit_enum_variant ( & self , name : & str , _id : uint , _cnt : uint , f : fn ( ) ) {
129
- // encoding of enums is special-cased for Option. Specifically:
130
- // Some(34) => 34
131
- // None => null
132
-
133
- // other enums are encoded as vectors:
134
- // Kangaroo(34,"William") => ["Kangaroo",[34,"William"]]
135
-
136
- // the default expansion for enums is more verbose than I'd like;
137
- // specifically, the inner pair of brackets seems superfluous,
138
- // BUT the design of the enumeration framework and the requirements
139
- // of the special-case for Option mean that a first argument must
140
- // be encoded "naked"--with no commas--and that the option name
141
- // can't be followed by just a comma, because there might not
142
- // be any elements in the tuple.
143
-
144
- // this would be more precise and less frightening
145
- // with fully-qualified option names. To get that information,
146
- // we'd have to change the expansion of auto-encode to pass
147
- // those along.
148
-
149
- if ( name == ~"Some ") {
150
- f ( ) ;
151
- } else if ( name == ~"None ") {
152
- self . wr . write_str ( ~"null") ;
128
+ fn emit_enum_variant(&self, _name: &str, id: uint, _cnt: uint, f: fn()) {
129
+ if id == 0 {
130
+ self.emit_nil();
153
131
} else {
154
- self . wr . write_char ( '[' ) ;
155
- self . wr . write_str ( escape_str ( name) ) ;
156
- self . wr . write_char ( ',' ) ;
157
- self . wr . write_char ( '[' ) ;
158
- f ( ) ;
159
- self . wr . write_char ( ']' ) ;
160
- self . wr . write_char ( ']' ) ;
132
+ f()
161
133
}
162
134
}
163
-
164
- fn emit_enum_variant_arg ( & self , idx : uint , f : fn ( ) ) {
165
- if ( idx != 0 ) { self . wr . write_char ( ',' ) ; }
166
- f ( ) ;
135
+ fn emit_enum_variant_arg(&self, _idx: uint, f: fn()) {
136
+ f()
167
137
}
168
138
169
139
fn emit_borrowed_vec(&self, _len: uint, f: fn()) {
170
140
self.wr.write_char('[');
171
141
f();
172
142
self.wr.write_char(']');
173
143
}
174
-
175
144
fn emit_owned_vec(&self, len: uint, f: fn()) {
176
145
self.emit_borrowed_vec(len, f)
177
146
}
@@ -1211,8 +1180,6 @@ mod tests {
1211
1180
1212
1181
use core::result;
1213
1182
use core::hashmap::linear::LinearMap;
1214
- use core::cmp;
1215
-
1216
1183
1217
1184
fn mk_object(items: &[(~str, Json)]) -> Json {
1218
1185
let mut d = ~LinearMap::new();
@@ -1280,72 +1247,6 @@ mod tests {
1280
1247
assert a == b;
1281
1248
}
1282
1249
1283
- // two fns copied from libsyntax/util/testing.rs.
1284
- // Should they be in their own crate?
1285
- pub pure fn check_equal_ptr < T : cmp:: Eq > ( given : & T , expected: & T ) {
1286
- if !( ( given == expected) && ( expected == given ) ) {
1287
- die!( fmt!( "given %?, expected %?" , given, expected) ) ;
1288
- }
1289
- }
1290
-
1291
- pub pure fn check_equal<T : cmp:: Eq > ( given : T , expected: T ) {
1292
- if !( ( given == expected) && ( expected == given ) ) {
1293
- die!( fmt!( "given %?, expected %?" , given, expected) ) ;
1294
- }
1295
- }
1296
-
1297
- // testing both auto_encode's calling patterns
1298
- // and json... not sure where to put these tests.
1299
- #[ test]
1300
- fn test_write_enum ( ) {
1301
- let bw = @io:: BytesWriter { bytes: dvec:: DVec ( ) , pos: 0 } ;
1302
- let bww : @io:: Writer = ( bw as @io:: Writer ) ;
1303
- let encoder = ( @Encoder ( bww) as @serialize:: Encoder ) ;
1304
- do encoder. emit_enum( ~"animal") {
1305
- do encoder.emit_enum_variant (~" frog",37,1242) {
1306
- // name of frog:
1307
- do encoder.emit_enum_variant_arg (0) {
1308
- encoder.emit_owned_str(~" Henry ")
1309
- }
1310
- // mass of frog in grams:
1311
- do encoder.emit_enum_variant_arg (1) {
1312
- encoder.emit_int(349);
1313
- }
1314
- }
1315
- }
1316
- check_equal(str::from_bytes(bw.bytes.data),
1317
- ~" [ \" frog\" , [ \" Henry \" , 349 ] ] ");
1318
- }
1319
-
1320
- #[test]
1321
- fn test_write_some () {
1322
- let bw = @io::BytesWriter {bytes: dvec::DVec(), pos: 0};
1323
- let bww : @io::Writer = (bw as @io::Writer);
1324
- let encoder = (@Encoder(bww) as @serialize::Encoder);
1325
- do encoder.emit_enum(~" Option ") {
1326
- do encoder.emit_enum_variant (~" Some ",37,1242) {
1327
- do encoder.emit_enum_variant_arg (0) {
1328
- encoder.emit_owned_str(~" jodhpurs")
1329
- }
1330
- }
1331
- }
1332
- check_equal(str::from_bytes(bw.bytes.data),
1333
- ~"\" jodhpurs\" " ) ;
1334
- }
1335
-
1336
- #[ test]
1337
- fn test_write_none ( ) {
1338
- let bw = @io:: BytesWriter { bytes: dvec:: DVec ( ) , pos: 0 } ;
1339
- let bww : @io:: Writer = ( bw as @io:: Writer ) ;
1340
- let encoder = ( @Encoder ( bww) as @serialize:: Encoder ) ;
1341
- do encoder. emit_enum( ~"Option ") {
1342
- do encoder.emit_enum_variant (~" None ",37,1242) {
1343
- }
1344
- }
1345
- check_equal(str::from_bytes(bw.bytes.data),
1346
- ~" null");
1347
- }
1348
-
1349
1250
#[ test]
1350
1251
fn test_trailing_characters( ) {
1351
1252
assert from_str( ~"nulla") ==
0 commit comments