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,
Int(int),
//String(String),
Optiony(bool),
SeqStart(uint),
//MapStart(uint),
End,
@ -23,7 +24,7 @@ enum Error {
struct MyDeserializer<Iter> {
tokens: Iter,
peeked: Option<Token>,
peeked: ::std::option::Option<Token>,
}
impl<Iter: Iterator<Token>> MyDeserializer<Iter> {
@ -70,6 +71,12 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
visitor.visit_string(self, v)
}
*/
Some(Optiony(is_some)) => {
visitor.visit_option(self, MyOptionVisitor {
is_some: is_some,
finished: false,
})
}
Some(SeqStart(len)) => {
visitor.visit_seq(self, MySeqVisitor { len: len })
}
@ -95,6 +102,15 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
self.next();
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));
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 {
len: uint,
}
@ -220,16 +262,14 @@ mod json {
}
*/
/*
fn visit_option<
Visitor: de2::OptionVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: Visitor) -> Result<Value, E> {
match visitor.next(d) {
Some(v) => v,
match try!(visitor.visit(d)) {
Some(value) => Ok(value),
None => Ok(Null),
}
}
*/
fn visit_seq<
Visitor: de2::SeqVisitor<D, E>,
@ -294,7 +334,7 @@ fn main() {
let mut state = MyDeserializer::new(tokens.into_iter());
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 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 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 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 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 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 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())
}
/*
fn visit_option<
V: OptionVisitor<D, E>
>(&mut self, d: &mut D, _visitor: V) -> Result<R, E> {
Err(d.syntax_error())
}
*/
fn visit_seq<
V: SeqVisitor<D, E>
@ -46,19 +44,11 @@ pub trait Visitor<D: Deserializer<E>, R, 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>>;
*/
pub trait OptionVisitor<D, E> {
fn visit<
T: Deserialize<D, E>,
>(&mut self, d: &mut D) -> Result<Option<T>, E>;
}
*/
pub trait SeqVisitor<D, E> {
fn next<
@ -119,37 +109,6 @@ impl<
E,
> Deserialize<D, E> for Option<T> {
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()
}
}