diff --git a/serde2/src/bin.rs b/serde2/src/bin.rs index fc564cce..7b46a179 100644 --- a/serde2/src/bin.rs +++ b/serde2/src/bin.rs @@ -73,10 +73,15 @@ impl> Deserializer for MyDeserializer { visitor.visit_string(self, v) } Some(Option(is_some)) => { + /* visitor.visit_option(self, MyOptionVisitor { is_some: is_some, finished: false, }) + */ + //fail!() + let value = try!(self.visit_option()); + visitor.visit_option(self, value) } Some(SeqStart(len)) => { visitor.visit_seq(self, MySeqVisitor { len: len }) @@ -96,8 +101,27 @@ impl> Deserializer for MyDeserializer { } fn visit_option< - T: Deserialize, Error>, - >(&mut self) -> Result, Error> { + R: Deserialize, Error>, + //V: de2::Visitor, R, Error>, + //>(&mut self, visitor: &mut V) -> Result { + >(&mut self) -> Result, Error> { + match self.next() { + Some(Option(true)) => { + let v = try!(Deserialize::deserialize(self)); + Ok(Some(v)) + } + Some(Option(false)) => { + Ok(None) + } + Some(_) => { + Err(self.syntax_error()) + } + None => { + Err(self.end_of_stream_error()) + } + } + + /* match self.peek() { Some(&Null) => { self.next(); @@ -117,6 +141,7 @@ impl> Deserializer for MyDeserializer { Ok(Some(v)) } } + */ } fn syntax_error(&mut self) -> Error { @@ -128,6 +153,7 @@ impl> Deserializer for MyDeserializer { } } +/* struct MyOptionVisitor { is_some: bool, finished: bool, @@ -153,6 +179,7 @@ impl< } } } +*/ struct MySeqVisitor { len: uint, @@ -263,6 +290,7 @@ mod json { } */ + /* fn visit_option< Visitor: de2::OptionVisitor, >(&mut self, d: &mut D, mut visitor: Visitor) -> Result { @@ -271,6 +299,7 @@ mod json { None => Ok(Null), } } + */ fn visit_seq< Visitor: de2::SeqVisitor, @@ -341,8 +370,8 @@ fn main() { let tokens = vec!( SeqStart(2), - Int(1), - Int(2), + Int(3), + Int(4), End, ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -354,8 +383,8 @@ fn main() { let tokens = vec!( SeqStart(2), - Int(1), - Int(2), + Int(5), + Int(6), End, ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -367,7 +396,7 @@ fn main() { let tokens = vec!( Option(true), - Int(1), + Int(7), ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -388,7 +417,7 @@ fn main() { let tokens = vec!( Option(true), - Int(1), + Int(8), ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -408,7 +437,7 @@ fn main() { //// let tokens = vec!( - Int(1), + Int(9), ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -428,7 +457,7 @@ fn main() { //// let tokens = vec!( - Int(1), + Int(10), ); let mut state = MyDeserializer::new(tokens.into_iter()); diff --git a/serde2/src/de2.rs b/serde2/src/de2.rs index 4455b619..201c14d6 100644 --- a/serde2/src/de2.rs +++ b/serde2/src/de2.rs @@ -13,9 +13,18 @@ pub trait Deserializer { V: Visitor, >(&mut self, visitor: &mut V) -> Result; + /* fn visit_option< - T: Deserialize, - >(&mut self) -> Result, E>; + R, + V: Visitor, + >(&mut self, visitor: &mut V) -> Result { + self.visit(visitor) + } + */ + + fn visit_option< + R: Deserialize, + >(&mut self) -> Result, E>; fn syntax_error(&mut self) -> E; @@ -40,10 +49,31 @@ pub trait Visitor, R, E> { } fn visit_option< - V: OptionVisitor + T: Deserialize, + >(&mut self, d: &mut D, _v: Option) -> Result { + Err(d.syntax_error()) + } + + /* + fn visit_option_some< + T: Deserialize, + >(&mut self, d: &mut D, _v: T) -> Result { + Err(d.syntax_error()) + } + + fn visit_option_none(&mut self, d: &mut D) -> Result { + Err(d.syntax_error()) + } + */ + + /* + fn visit_option< + T: Deserialize, + V: OptionVisitor, >(&mut self, d: &mut D, _visitor: V) -> Result { Err(d.syntax_error()) } + */ fn visit_seq< V: SeqVisitor @@ -52,10 +82,10 @@ pub trait Visitor, R, E> { } } -pub trait OptionVisitor { - fn visit< - T: Deserialize, - >(&mut self, d: &mut D) -> Result, E>; +pub trait OptionVisitor, D, R, E> { + fn visit_some(&mut self, d: &mut D, _v: T) -> Result; + + fn visit_none(&mut self, d: &mut D) -> Result; } pub trait SeqVisitor { @@ -136,6 +166,22 @@ impl< E, > Deserialize for Option { fn deserialize(d: &mut D) -> Result, E> { + /* + struct Visitor; + + impl< + T: Deserialize, + D: Deserializer, + E, + > self::Visitor, E> for Visitor { + fn visit_option(&mut self, _d: &mut D, v: Option) -> Result, E> { + Ok(v) + } + } + + d.visit_option(&mut Visitor) + */ + d.visit_option() } }