mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-13 05:31:02 +00:00
Add support for enums
This commit is contained in:
@@ -24,12 +24,16 @@ pub enum Token {
|
|||||||
Char(char),
|
Char(char),
|
||||||
Str(&'static str),
|
Str(&'static str),
|
||||||
StrBuf(StrBuf),
|
StrBuf(StrBuf),
|
||||||
|
|
||||||
Option(bool),
|
Option(bool),
|
||||||
|
|
||||||
TupleStart(uint),
|
TupleStart(uint),
|
||||||
|
|
||||||
StructStart(&'static str),
|
StructStart(&'static str),
|
||||||
StructField(&'static str),
|
StructField(&'static str),
|
||||||
|
|
||||||
|
EnumStart(&'static str),
|
||||||
|
EnumVariant(&'static str),
|
||||||
|
|
||||||
SeqStart(uint),
|
SeqStart(uint),
|
||||||
MapStart(uint),
|
MapStart(uint),
|
||||||
|
|
||||||
@@ -190,6 +194,26 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn expect_enum_start<'a>(&mut self, name: &str, variants: &[&str]) -> Result<uint, E> {
|
||||||
|
match_token! {
|
||||||
|
EnumStart(n) => {
|
||||||
|
if name == n {
|
||||||
|
match_token! {
|
||||||
|
EnumVariant(n) => {
|
||||||
|
match variants.iter().position(|variant| *variant == n) {
|
||||||
|
Some(position) => Ok(position),
|
||||||
|
None => Err(self.syntax_error()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(self.syntax_error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_collection<
|
fn expect_collection<
|
||||||
T: Deserializable<E, Self>,
|
T: Deserializable<E, Self>,
|
||||||
@@ -410,7 +434,8 @@ mod tests {
|
|||||||
use self::serialize::{Decoder, Decodable};
|
use self::serialize::{Decoder, Decodable};
|
||||||
|
|
||||||
use super::{Token, Null, Int, Uint, Str, StrBuf, Char, Option};
|
use super::{Token, Null, Int, Uint, Str, StrBuf, Char, Option};
|
||||||
use super::{TupleStart, StructStart, StructField, SeqStart, MapStart, Sep, End};
|
use super::{TupleStart, StructStart, StructField, EnumStart, EnumVariant};
|
||||||
|
use super::{SeqStart, MapStart, Sep, End};
|
||||||
use super::{Deserializer, Deserializable};
|
use super::{Deserializer, Deserializable};
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -453,6 +478,33 @@ mod tests {
|
|||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[deriving(Eq, Show)]
|
||||||
|
enum Animal {
|
||||||
|
Dog,
|
||||||
|
Frog(StrBuf, int)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E, D: Deserializer<E>> Deserializable<E, D> for Animal {
|
||||||
|
#[inline]
|
||||||
|
fn deserialize(d: &mut D) -> Result<Animal, E> {
|
||||||
|
match try!(d.expect_enum_start("Animal", ["Dog", "Frog"])) {
|
||||||
|
0 => {
|
||||||
|
try!(d.expect_end());
|
||||||
|
Ok(Dog)
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
let x0 = try!(Deserializable::deserialize(d));
|
||||||
|
let x1 = try!(Deserializable::deserialize(d));
|
||||||
|
try!(d.expect_end());
|
||||||
|
Ok(Frog(x0, x1))
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[deriving(Show)]
|
#[deriving(Show)]
|
||||||
enum Error {
|
enum Error {
|
||||||
EndOfStream,
|
EndOfStream,
|
||||||
@@ -886,6 +938,33 @@ mod tests {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tokens_enum() {
|
||||||
|
let tokens = vec!(
|
||||||
|
EnumStart("Animal"),
|
||||||
|
EnumVariant("Dog"),
|
||||||
|
End,
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut deserializer = TokenDeserializer::new(tokens);
|
||||||
|
let value: Animal = Deserializable::deserialize(&mut deserializer).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(value, Dog);
|
||||||
|
|
||||||
|
let tokens = vec!(
|
||||||
|
EnumStart("Animal"),
|
||||||
|
EnumVariant("Frog"),
|
||||||
|
StrBuf("Henry".to_strbuf()),
|
||||||
|
Int(349),
|
||||||
|
End,
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut deserializer = TokenDeserializer::new(tokens);
|
||||||
|
let value: Animal = Deserializable::deserialize(&mut deserializer).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(value, Frog("Henry".to_strbuf(), 349));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_tokens_vec_empty() {
|
fn test_tokens_vec_empty() {
|
||||||
let tokens = vec!(
|
let tokens = vec!(
|
||||||
|
|||||||
Reference in New Issue
Block a user