From 4c43b4fea2cdd270f26e71cc783d1c02a106b31f Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 2 Oct 2014 19:24:38 -0700 Subject: [PATCH] Variation to support option and a json-ish value --- serde2/src/bin.rs | 81 +++++++++++++++++++++++++++++++---- serde2/src/de2.rs | 107 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 172 insertions(+), 16 deletions(-) diff --git a/serde2/src/bin.rs b/serde2/src/bin.rs index b5d95d1f..55662d4e 100644 --- a/serde2/src/bin.rs +++ b/serde2/src/bin.rs @@ -3,8 +3,9 @@ extern crate serde2; use serde2::de2; use serde2::de2::{Deserialize, Deserializer}; +#[deriving(Show)] enum Token { - //Null, + Null, Int(int), //String(String), SeqStart(uint), @@ -58,11 +59,9 @@ impl> Deserializer for MyDeserializer { V: de2::Visitor, R, Error>, >(&mut self, visitor: &mut V) -> Result { match self.next() { - /* Some(Null) => { visitor.visit_null(self) } - */ Some(Int(v)) => { visitor.visit_int(self, v) } @@ -88,6 +87,21 @@ impl> Deserializer for MyDeserializer { } } + fn visit_option< + T: Deserialize, Error>, + >(&mut self) -> Result, Error> { + match self.peek() { + Some(&Null) => { + self.next(); + Ok(None) + } + _ => { + let v = try!(Deserialize::deserialize(self)); + Ok(Some(v)) + } + } + } + fn syntax_error(&mut self) -> Error { SyntaxError } @@ -173,7 +187,7 @@ mod json { #[deriving(Show)] pub enum Value { - //Null, + Null, //Bool(bool), Int(int), //String(String), @@ -192,11 +206,9 @@ mod json { D: de2::Deserializer, E, > de2::Visitor for Visitor { - /* fn visit_null(&mut self, _d: &mut D) -> Result { Ok(Null) } - */ fn visit_int(&mut self, _d: &mut D, v: int) -> Result { Ok(Int(v)) @@ -208,6 +220,17 @@ mod json { } */ + /* + fn visit_option< + Visitor: de2::OptionVisitor, + >(&mut self, d: &mut D, mut visitor: Visitor) -> Result { + match visitor.next(d) { + Some(v) => v, + None => Ok(Null), + } + } + */ + fn visit_seq< Visitor: de2::SeqVisitor, >(&mut self, d: &mut D, mut visitor: Visitor) -> Result { @@ -266,7 +289,7 @@ fn main() { SeqStart(2), Int(1), Int(2), - End + End, ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -279,7 +302,7 @@ fn main() { SeqStart(2), Int(1), Int(2), - End + End, ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -292,7 +315,47 @@ fn main() { SeqStart(2), Int(1), Int(2), - End + End, + ); + let mut state = MyDeserializer::new(tokens.into_iter()); + + let v: Result = Deserialize::deserialize(&mut state); + println!("{}", v); + + //// + + let tokens = vec!( + Int(1), + ); + let mut state = MyDeserializer::new(tokens.into_iter()); + + let v: Result, Error> = Deserialize::deserialize(&mut state); + println!("{}", v); + + //// + + let tokens = vec!( + Null, + ); + let mut state = MyDeserializer::new(tokens.into_iter()); + + let v: Result, Error> = Deserialize::deserialize(&mut state); + println!("{}", v); + + //// + + let tokens = vec!( + Int(1), + ); + let mut state = MyDeserializer::new(tokens.into_iter()); + + let v: Result = Deserialize::deserialize(&mut state); + println!("{}", v); + + //// + + let tokens = vec!( + Null, ); let mut state = MyDeserializer::new(tokens.into_iter()); diff --git a/serde2/src/de2.rs b/serde2/src/de2.rs index 6a535ec5..b8c155e5 100644 --- a/serde2/src/de2.rs +++ b/serde2/src/de2.rs @@ -10,26 +10,56 @@ pub trait Deserialize { pub trait Deserializer { fn visit< R, - V: Visitor + V: Visitor, >(&mut self, visitor: &mut V) -> Result; + fn visit_option< + T: Deserialize, + >(&mut self) -> Result, E>; + fn syntax_error(&mut self) -> E; fn end_of_stream_error(&mut self) -> E; } pub trait Visitor, R, E> { + fn visit_null(&mut self, d: &mut D) -> Result { + Err(d.syntax_error()) + } + fn visit_int(&mut self, d: &mut D, _v: int) -> Result { Err(d.syntax_error()) } + /* + fn visit_option< + V: OptionVisitor + >(&mut self, d: &mut D, _visitor: V) -> Result { + Err(d.syntax_error()) + } + */ + fn visit_seq< V: SeqVisitor - >(&mut self, d: &mut D, _v: V) -> Result { + >(&mut self, d: &mut D, _visitor: V) -> Result { Err(d.syntax_error()) } } +/* +pub trait OptionVisitor, E> { + /* + fn next< + T: Deserialize, + >(&mut self, d: &mut D) -> Option>; + */ + + fn visit< + T: Deserialize, + >(&mut self, d: &mut D) -> Result, E>; +} +*/ + pub trait SeqVisitor { fn next< T: Deserialize, @@ -43,6 +73,27 @@ pub trait SeqVisitor { } } +/////////////////////////////////////////////////////////////////////////////// + +impl< + D: Deserializer, + E, +> Deserialize for () { + fn deserialize(d: &mut D) -> Result<(), E> { + struct Visitor; + + impl, E> self::Visitor for Visitor { + fn visit_null(&mut self, _d: &mut D) -> Result<(), E> { + Ok(()) + } + } + + d.visit(&mut Visitor) + } +} + +/////////////////////////////////////////////////////////////////////////////// + impl< D: Deserializer, E, @@ -60,6 +111,51 @@ impl< } } +/////////////////////////////////////////////////////////////////////////////// + +impl< + T: Deserialize, + D: Deserializer, + E, +> Deserialize for Option { + fn deserialize(d: &mut D) -> Result, E> { + /* + struct Visitor; + + impl< + R: Deserialize, + D: Deserializer, + E, + > self::OptionVisitor for Visitor { + /* + fn visit_option< + V: OptionVisitor, + >(&mut self, d: &mut D, mut visitor: V) -> Result, E> { + match visitor.next(d) { + Some(value) => { + Ok(Some(try!(value))) + } + None => { + Ok(None) + } + } + } + */ + + fn visit< + T: Deserialize, + >(&mut self, d: &mut D, value: Option) -> Result, E> { + Ok(value) + } + } + */ + + d.visit_option() + } +} + +/////////////////////////////////////////////////////////////////////////////// + impl< T: Deserialize, D: Deserializer, @@ -81,11 +177,8 @@ impl< loop { match visitor.next(d) { - Some(Ok(value)) => { - values.push(value); - } - Some(Err(err)) => { - return Err(err); + Some(value) => { + values.push(try!(value)); } None => { break;