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) visitor.visit_string(self, v)
} }
Some(Option(is_some)) => { Some(Option(is_some)) => {
/*
visitor.visit_option(self, MyOptionVisitor { visitor.visit_option(self, MyOptionVisitor {
is_some: is_some, is_some: is_some,
finished: false, finished: false,
}) })
*/
//fail!()
let value = try!(self.visit_option());
visitor.visit_option(self, value)
} }
Some(SeqStart(len)) => { Some(SeqStart(len)) => {
visitor.visit_seq(self, MySeqVisitor { len: len }) visitor.visit_seq(self, MySeqVisitor { len: len })
@ -96,8 +101,27 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
} }
fn visit_option< fn visit_option<
T: Deserialize<MyDeserializer<Iter>, Error>, R: Deserialize<MyDeserializer<Iter>, Error>,
>(&mut self) -> Result<option::Option<T>, 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() { match self.peek() {
Some(&Null) => { Some(&Null) => {
self.next(); self.next();
@ -117,6 +141,7 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
Ok(Some(v)) Ok(Some(v))
} }
} }
*/
} }
fn syntax_error(&mut self) -> Error { fn syntax_error(&mut self) -> Error {
@ -128,6 +153,7 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
} }
} }
/*
struct MyOptionVisitor { struct MyOptionVisitor {
is_some: bool, is_some: bool,
finished: bool, finished: bool,
@ -153,6 +179,7 @@ impl<
} }
} }
} }
*/
struct MySeqVisitor { struct MySeqVisitor {
len: uint, len: uint,
@ -263,6 +290,7 @@ mod json {
} }
*/ */
/*
fn visit_option< fn visit_option<
Visitor: de2::OptionVisitor<D, E>, Visitor: de2::OptionVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: Visitor) -> Result<Value, E> { >(&mut self, d: &mut D, mut visitor: Visitor) -> Result<Value, E> {
@ -271,6 +299,7 @@ mod json {
None => Ok(Null), None => Ok(Null),
} }
} }
*/
fn visit_seq< fn visit_seq<
Visitor: de2::SeqVisitor<D, E>, Visitor: de2::SeqVisitor<D, E>,
@ -341,8 +370,8 @@ fn main() {
let tokens = vec!( let tokens = vec!(
SeqStart(2), SeqStart(2),
Int(1), Int(3),
Int(2), Int(4),
End, End,
); );
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());
@ -354,8 +383,8 @@ fn main() {
let tokens = vec!( let tokens = vec!(
SeqStart(2), SeqStart(2),
Int(1), Int(5),
Int(2), Int(6),
End, End,
); );
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());
@ -367,7 +396,7 @@ fn main() {
let tokens = vec!( let tokens = vec!(
Option(true), Option(true),
Int(1), Int(7),
); );
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());
@ -388,7 +417,7 @@ fn main() {
let tokens = vec!( let tokens = vec!(
Option(true), Option(true),
Int(1), Int(8),
); );
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());
@ -408,7 +437,7 @@ fn main() {
//// ////
let tokens = vec!( let tokens = vec!(
Int(1), Int(9),
); );
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());
@ -428,7 +457,7 @@ fn main() {
//// ////
let tokens = vec!( let tokens = vec!(
Int(1), Int(10),
); );
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());

View File

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