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

Commit 9c23568

Browse files
author
Oliver Schneider
committed
allow simple enum variants as hash map keys
1 parent c35ef51 commit 9c23568

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

src/json.rs

Lines changed: 4 additions & 10 deletions
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));
@@ -3564,12 +3562,8 @@ mod tests {
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}"#);
35733567
}
35743568

35753569
#[test]

0 commit comments

Comments
 (0)