Add support for deserialization streams that have an option type

This commit is contained in:
Erick Tryzelaar 2014-10-03 07:50:30 -07:00
parent 4c43b4fea2
commit e605d70af1
2 changed files with 95 additions and 54 deletions

View File

@ -8,6 +8,7 @@ enum Token {
Null, Null,
Int(int), Int(int),
//String(String), //String(String),
Optiony(bool),
SeqStart(uint), SeqStart(uint),
//MapStart(uint), //MapStart(uint),
End, End,
@ -23,7 +24,7 @@ enum Error {
struct MyDeserializer<Iter> { struct MyDeserializer<Iter> {
tokens: Iter, tokens: Iter,
peeked: Option<Token>, peeked: ::std::option::Option<Token>,
} }
impl<Iter: Iterator<Token>> MyDeserializer<Iter> { impl<Iter: Iterator<Token>> MyDeserializer<Iter> {
@ -70,6 +71,12 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
visitor.visit_string(self, v) visitor.visit_string(self, v)
} }
*/ */
Some(Optiony(is_some)) => {
visitor.visit_option(self, MyOptionVisitor {
is_some: is_some,
finished: false,
})
}
Some(SeqStart(len)) => { Some(SeqStart(len)) => {
visitor.visit_seq(self, MySeqVisitor { len: len }) visitor.visit_seq(self, MySeqVisitor { len: len })
} }
@ -95,6 +102,15 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
self.next(); self.next();
Ok(None) Ok(None)
} }
Some(&Optiony(true)) => {
self.next();
let v = try!(Deserialize::deserialize(self));
Ok(Some(v))
}
Some(&Optiony(false)) => {
self.next();
Ok(None)
}
_ => { _ => {
let v = try!(Deserialize::deserialize(self)); let v = try!(Deserialize::deserialize(self));
Ok(Some(v)) Ok(Some(v))
@ -111,6 +127,32 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
} }
} }
struct MyOptionVisitor {
is_some: bool,
finished: bool,
}
impl<
Iter: Iterator<Token>,
> de2::OptionVisitor<MyDeserializer<Iter>, Error> for MyOptionVisitor {
fn visit<
T: Deserialize<MyDeserializer<Iter>, Error>,
>(&mut self, d: &mut MyDeserializer<Iter>) -> Result<Option<T>, Error> {
if self.finished {
Err(d.syntax_error())
} else {
self.finished = true;
if self.is_some {
let v = try!(Deserialize::deserialize(d));
Ok(Some(v))
} else {
Ok(None)
}
}
}
}
struct MySeqVisitor { struct MySeqVisitor {
len: uint, len: uint,
} }
@ -220,16 +262,14 @@ 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> {
match visitor.next(d) { match try!(visitor.visit(d)) {
Some(v) => v, Some(value) => Ok(value),
None => Ok(Null), None => Ok(Null),
} }
} }
*/
fn visit_seq< fn visit_seq<
Visitor: de2::SeqVisitor<D, E>, Visitor: de2::SeqVisitor<D, E>,
@ -294,7 +334,7 @@ fn main() {
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<Vec<int>, Error> = Deserialize::deserialize(&mut state); let v: Result<Vec<int>, Error> = Deserialize::deserialize(&mut state);
println!("{}", v); println!("vec: {}", v);
//// ////
@ -307,7 +347,7 @@ fn main() {
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<(int, int), Error> = Deserialize::deserialize(&mut state); let v: Result<(int, int), Error> = Deserialize::deserialize(&mut state);
println!("{}", v); println!("tuple: {}", v);
//// ////
@ -320,7 +360,49 @@ fn main() {
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<json::Value, Error> = Deserialize::deserialize(&mut state); let v: Result<json::Value, Error> = Deserialize::deserialize(&mut state);
println!("{}", v); println!("value: {}", v);
////
let tokens = vec!(
Optiony(true),
Int(1),
);
let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<Option<int>, Error> = Deserialize::deserialize(&mut state);
println!("optiony: {}", v);
////
let tokens = vec!(
Optiony(false),
);
let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<Option<int>, Error> = Deserialize::deserialize(&mut state);
println!("optiony: {}", v);
////
let tokens = vec!(
Optiony(true),
Int(1),
);
let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<json::Value, Error> = Deserialize::deserialize(&mut state);
println!("optiony value:{}", v);
////
let tokens = vec!(
Optiony(false),
);
let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<json::Value, Error> = Deserialize::deserialize(&mut state);
println!("optiony value:{}", v);
//// ////
@ -330,7 +412,7 @@ fn main() {
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<Option<int>, Error> = Deserialize::deserialize(&mut state); let v: Result<Option<int>, Error> = Deserialize::deserialize(&mut state);
println!("{}", v); println!("option: {}", v);
//// ////
@ -340,7 +422,7 @@ fn main() {
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<Option<int>, Error> = Deserialize::deserialize(&mut state); let v: Result<Option<int>, Error> = Deserialize::deserialize(&mut state);
println!("{}", v); println!("option: {}", v);
//// ////
@ -350,7 +432,7 @@ fn main() {
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<json::Value, Error> = Deserialize::deserialize(&mut state); let v: Result<json::Value, Error> = Deserialize::deserialize(&mut state);
println!("{}", v); println!("option value: {}", v);
//// ////
@ -360,7 +442,7 @@ fn main() {
let mut state = MyDeserializer::new(tokens.into_iter()); let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<json::Value, Error> = Deserialize::deserialize(&mut state); let v: Result<json::Value, Error> = Deserialize::deserialize(&mut state);
println!("{}", v); println!("option value: {}", v);
/* /*
//// ////

View File

@ -31,13 +31,11 @@ pub trait Visitor<D: Deserializer<E>, R, E> {
Err(d.syntax_error()) Err(d.syntax_error())
} }
/*
fn visit_option< fn visit_option<
V: OptionVisitor<D, E> V: OptionVisitor<D, 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>
@ -46,19 +44,11 @@ pub trait Visitor<D: Deserializer<E>, R, E> {
} }
} }
/* pub trait OptionVisitor<D, E> {
pub trait OptionVisitor<D, R, T: Deserialize<D, E>, E> {
/*
fn next<
T: Deserialize<D, E>,
>(&mut self, d: &mut D) -> Option<Result<T, E>>;
*/
fn visit< fn visit<
T: Deserialize<D, E>, T: Deserialize<D, E>,
>(&mut self, d: &mut D) -> Result<Option<T>, E>; >(&mut self, d: &mut D) -> Result<Option<T>, E>;
} }
*/
pub trait SeqVisitor<D, E> { pub trait SeqVisitor<D, E> {
fn next< fn next<
@ -119,37 +109,6 @@ 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<
R: Deserialize<D, E>,
D: Deserializer<E>,
E,
> self::OptionVisitor<D, E> for Visitor {
/*
fn visit_option<
V: OptionVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: V) -> Result<Option<R>, E> {
match visitor.next(d) {
Some(value) => {
Ok(Some(try!(value)))
}
None => {
Ok(None)
}
}
}
*/
fn visit<
T: Deserialize<D, E>,
>(&mut self, d: &mut D, value: Option<T>) -> Result<Option<T>, E> {
Ok(value)
}
}
*/
d.visit_option() d.visit_option()
} }
} }