aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-06-23 09:37:13 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-06-23 09:37:13 -0700
commit21e91fe753d004d4f24318da99d312027a5a19c5 (patch)
tree7a79ff91e736a3d0f87be82669e05be24a952716
parentcd4fede07287fe7c153a59e01c8425dea893d6ab (diff)
downloadmilf-rs-21e91fe753d004d4f24318da99d312027a5a19c5.tar.gz
milf-rs-21e91fe753d004d4f24318da99d312027a5a19c5.zip
Add Encodable/Decodable support for enums
-rw-r--r--src/serialization.rs79
1 files changed, 66 insertions, 13 deletions
diff --git a/src/serialization.rs b/src/serialization.rs
index 028de64..7183eff 100644
--- a/src/serialization.rs
+++ b/src/serialization.rs
@@ -173,20 +173,20 @@ impl serialize::Encoder<Error> for Encoder {
self.emit_value(String(v.to_str()))
}
fn emit_enum(&mut self, _name: &str,
- _f: |&mut Encoder| -> Result<(), Error>) -> Result<(), Error> {
- fail!()
+ f: |&mut Encoder| -> Result<(), Error>) -> Result<(), Error> {
+ f(self)
}
fn emit_enum_variant(&mut self, _v_name: &str, _v_id: uint, _len: uint,
- _f: |&mut Encoder| -> Result<(), Error>)
+ f: |&mut Encoder| -> Result<(), Error>)
-> Result<(), Error>
{
- fail!()
+ f(self)
}
fn emit_enum_variant_arg(&mut self, _a_idx: uint,
- _f: |&mut Encoder| -> Result<(), Error>)
+ f: |&mut Encoder| -> Result<(), Error>)
-> Result<(), Error>
{
- fail!()
+ f(self)
}
fn emit_enum_struct_variant(&mut self, _v_name: &str, _v_id: uint,
_len: uint,
@@ -460,23 +460,39 @@ impl serialize::Decoder<DecodeError> for Decoder {
// Compound types:
fn read_enum<T>(&mut self, _name: &str,
- _f: |&mut Decoder| -> Result<T, DecodeError>)
+ f: |&mut Decoder| -> Result<T, DecodeError>)
-> Result<T, DecodeError>
{
- fail!()
+ f(self)
}
fn read_enum_variant<T>(&mut self,
- _names: &[&str],
- _f: |&mut Decoder, uint| -> Result<T, DecodeError>)
+ names: &[&str],
+ f: |&mut Decoder, uint| -> Result<T, DecodeError>)
-> Result<T, DecodeError> {
- fail!()
+ let mut first_error = None;
+ for i in range(0, names.len()) {
+ let mut d = self.sub_decoder(self.toml.clone(), "");
+ match f(&mut d, i) {
+ Ok(t) => { self.toml = d.toml; return Ok(t) }
+ Err(e) => {
+ if first_error.is_none() {
+ first_error = Some(e);
+ }
+ }
+ }
+ }
+ Err(first_error.unwrap_or_else(|| {
+ DecodeError {
+ desc: format!("no enum variants to decode to"),
+ }
+ }))
}
fn read_enum_variant_arg<T>(&mut self,
_a_idx: uint,
- _f: |&mut Decoder| -> Result<T, DecodeError>)
+ f: |&mut Decoder| -> Result<T, DecodeError>)
-> Result<T, DecodeError> {
- fail!()
+ f(self)
}
fn read_enum_struct_variant<T>(&mut self,
@@ -857,4 +873,41 @@ mod tests {
}
}
}
+
+ #[test]
+ fn parse_enum() {
+ #[deriving(Encodable, Decodable, PartialEq, Show)]
+ struct Foo { a: E }
+ #[deriving(Encodable, Decodable, PartialEq, Show)]
+ enum E {
+ Bar(int),
+ Baz(f64),
+ Last(Foo2),
+ }
+ #[deriving(Encodable, Decodable, PartialEq, Show)]
+ struct Foo2 {
+ test: String,
+ }
+
+ let v = Foo { a: Bar(10) };
+ assert_eq!(
+ encode!(v),
+ map! { a: Integer(10) }
+ );
+ assert_eq!(v, decode!(Table(encode!(v))));
+
+ let v = Foo { a: Baz(10.2) };
+ assert_eq!(
+ encode!(v),
+ map! { a: Float(10.2) }
+ );
+ assert_eq!(v, decode!(Table(encode!(v))));
+
+ let v = Foo { a: Last(Foo2 { test: "test".to_string() }) };
+ assert_eq!(
+ encode!(v),
+ map! { a: Table(map! { test: String("test".to_string()) }) }
+ );
+ assert_eq!(v, decode!(Table(encode!(v))));
+ }
}