Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Commit 2824cdb

Browse files
committed
Merge pull request #32 from oli-obk/allow_enum_variant_as_hashmap_key
Allow enum variant as hashmap key
2 parents c9e1df1 + 68acb1c commit 2824cdb

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/json.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,6 @@ impl<'a> ::Encoder for Encoder<'a> {
538538
fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
539539
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
540540
{
541-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
542541
f(self)
543542
}
544543

@@ -552,10 +551,10 @@ impl<'a> ::Encoder for Encoder<'a> {
552551
// enums are encoded as strings or objects
553552
// Bunny => "Bunny"
554553
// Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}
555-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
556554
if cnt == 0 {
557555
escape_str(self.writer, name)
558556
} else {
557+
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
559558
try!(write!(self.writer, "{{\"variant\":"));
560559
try!(escape_str(self.writer, name));
561560
try!(write!(self.writer, ",\"fields\":["));
@@ -787,7 +786,6 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
787786
fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
788787
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
789788
{
790-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
791789
f(self)
792790
}
793791

@@ -799,10 +797,10 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
799797
-> EncodeResult where
800798
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
801799
{
802-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
803800
if cnt == 0 {
804801
escape_str(self.writer, name)
805802
} else {
803+
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
806804
try!(write!(self.writer, "{{\n"));
807805
self.curr_indent += self.indent;
808806
try!(spaces(self.writer, self.curr_indent));
@@ -3556,20 +3554,18 @@ mod tests {
35563554
fn test_hashmap_with_enum_key() {
35573555
use std::collections::HashMap;
35583556
use json;
3559-
#[derive(RustcEncodable, Eq, Hash, PartialEq)]
3557+
#[derive(RustcEncodable, Eq, Hash, PartialEq, RustcDecodable, Show)]
35603558
enum Enum {
35613559
Foo,
35623560
#[allow(dead_code)]
35633561
Bar,
35643562
}
35653563
let mut map = HashMap::new();
35663564
map.insert(Enum::Foo, 0);
3567-
let result = json::encode(&map);
3568-
match result.unwrap_err() {
3569-
EncoderError::BadHashmapKey => (),
3570-
_ => panic!("expected bad hash map key")
3571-
}
3572-
//assert_eq!(&enc[], r#"{"Foo": 0}"#);
3565+
let result = json::encode(&map).unwrap();
3566+
assert_eq!(&result[], r#"{"Foo":0}"#);
3567+
let decoded: HashMap<Enum, _> = json::decode(result.as_slice()).unwrap();
3568+
assert_eq!(map, decoded);
35733569
}
35743570

35753571
#[test]

0 commit comments

Comments
 (0)