This commit is contained in:
Erick Tryzelaar 2014-10-06 08:12:36 -07:00
parent 79817f0603
commit 3ec686cab1
2 changed files with 92 additions and 17 deletions

View File

@ -73,10 +73,15 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
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<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
}
fn visit_option<
T: Deserialize<MyDeserializer<Iter>, Error>,
>(&mut self) -> Result<option::Option<T>, Error> {
R: Deserialize<MyDeserializer<Iter>, Error>,
//V: de2::Visitor<MyDeserializer<Iter>, R, Error>,
//>(&mut self, visitor: &mut V) -> Result<R, Error> {
>(&mut self) -> Result<option::Option<R>, 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<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
Ok(Some(v))
}
}
*/
}
fn syntax_error(&mut self) -> Error {
@ -128,6 +153,7 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
}
}
/*
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<D, E>,
>(&mut self, d: &mut D, mut visitor: Visitor) -> Result<Value, E> {
@ -271,6 +299,7 @@ mod json {
None => Ok(Null),
}
}
*/
fn visit_seq<
Visitor: de2::SeqVisitor<D, E>,
@ -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());

View File

@ -13,9 +13,18 @@ pub trait Deserializer<E> {
V: Visitor<Self, R, E>,
>(&mut self, visitor: &mut V) -> Result<R, E>;
/*
fn visit_option<
T: Deserialize<Self, E>,
>(&mut self) -> Result<Option<T>, E>;
R,
V: Visitor<Self, R, E>,
>(&mut self, visitor: &mut V) -> Result<R, E> {
self.visit(visitor)
}
*/
fn visit_option<
R: Deserialize<Self, E>,
>(&mut self) -> Result<Option<R>, E>;
fn syntax_error(&mut self) -> E;
@ -40,10 +49,31 @@ pub trait Visitor<D: Deserializer<E>, R, E> {
}
fn visit_option<
V: OptionVisitor<D, E>
T: Deserialize<D, E>,
>(&mut self, d: &mut D, _v: Option<T>) -> Result<R, E> {
Err(d.syntax_error())
}
/*
fn visit_option_some<
T: Deserialize<Self, E>,
>(&mut self, d: &mut D, _v: T) -> Result<R, E> {
Err(d.syntax_error())
}
fn visit_option_none(&mut self, d: &mut D) -> Result<R, E> {
Err(d.syntax_error())
}
*/
/*
fn visit_option<
T: Deserialize<D, E>,
V: OptionVisitor<T, D, R, E>,
>(&mut self, d: &mut D, _visitor: V) -> Result<R, E> {
Err(d.syntax_error())
}
*/
fn visit_seq<
V: SeqVisitor<D, E>
@ -52,10 +82,10 @@ pub trait Visitor<D: Deserializer<E>, R, E> {
}
}
pub trait OptionVisitor<D, E> {
fn visit<
T: Deserialize<D, E>,
>(&mut self, d: &mut D) -> Result<Option<T>, E>;
pub trait OptionVisitor<T: Deserialize<D, E>, D, R, E> {
fn visit_some(&mut self, d: &mut D, _v: T) -> Result<R, E>;
fn visit_none(&mut self, d: &mut D) -> Result<R, E>;
}
pub trait SeqVisitor<D, E> {
@ -136,6 +166,22 @@ impl<
E,
> Deserialize<D, E> for Option<T> {
fn deserialize(d: &mut D) -> Result<Option<T>, E> {
/*
struct Visitor;
impl<
T: Deserialize<D, E>,
D: Deserializer<E>,
E,
> self::Visitor<D, Option<T>, E> for Visitor {
fn visit_option(&mut self, _d: &mut D, v: Option<T>) -> Result<Option<T>, E> {
Ok(v)
}
}
d.visit_option(&mut Visitor)
*/
d.visit_option()
}
}