Variation to support option and a json-ish value

This commit is contained in:
Erick Tryzelaar 2014-10-02 19:24:38 -07:00
parent ed8dca4372
commit 4c43b4fea2
2 changed files with 172 additions and 16 deletions

View File

@ -3,8 +3,9 @@ extern crate serde2;
use serde2::de2;
use serde2::de2::{Deserialize, Deserializer};
#[deriving(Show)]
enum Token {
//Null,
Null,
Int(int),
//String(String),
SeqStart(uint),
@ -58,11 +59,9 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
V: de2::Visitor<MyDeserializer<Iter>, R, Error>,
>(&mut self, visitor: &mut V) -> Result<R, Error> {
match self.next() {
/*
Some(Null) => {
visitor.visit_null(self)
}
*/
Some(Int(v)) => {
visitor.visit_int(self, v)
}
@ -88,6 +87,21 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
}
}
fn visit_option<
T: Deserialize<MyDeserializer<Iter>, Error>,
>(&mut self) -> Result<Option<T>, Error> {
match self.peek() {
Some(&Null) => {
self.next();
Ok(None)
}
_ => {
let v = try!(Deserialize::deserialize(self));
Ok(Some(v))
}
}
}
fn syntax_error(&mut self) -> Error {
SyntaxError
}
@ -173,7 +187,7 @@ mod json {
#[deriving(Show)]
pub enum Value {
//Null,
Null,
//Bool(bool),
Int(int),
//String(String),
@ -192,11 +206,9 @@ mod json {
D: de2::Deserializer<E>,
E,
> de2::Visitor<D, Value, E> for Visitor {
/*
fn visit_null(&mut self, _d: &mut D) -> Result<Value, E> {
Ok(Null)
}
*/
fn visit_int(&mut self, _d: &mut D, v: int) -> Result<Value, E> {
Ok(Int(v))
@ -208,6 +220,17 @@ 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,
None => Ok(Null),
}
}
*/
fn visit_seq<
Visitor: de2::SeqVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: Visitor) -> Result<Value, E> {
@ -266,7 +289,7 @@ fn main() {
SeqStart(2),
Int(1),
Int(2),
End
End,
);
let mut state = MyDeserializer::new(tokens.into_iter());
@ -279,7 +302,7 @@ fn main() {
SeqStart(2),
Int(1),
Int(2),
End
End,
);
let mut state = MyDeserializer::new(tokens.into_iter());
@ -292,7 +315,47 @@ fn main() {
SeqStart(2),
Int(1),
Int(2),
End
End,
);
let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<json::Value, Error> = Deserialize::deserialize(&mut state);
println!("{}", v);
////
let tokens = vec!(
Int(1),
);
let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<Option<int>, Error> = Deserialize::deserialize(&mut state);
println!("{}", v);
////
let tokens = vec!(
Null,
);
let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<Option<int>, Error> = Deserialize::deserialize(&mut state);
println!("{}", v);
////
let tokens = vec!(
Int(1),
);
let mut state = MyDeserializer::new(tokens.into_iter());
let v: Result<json::Value, Error> = Deserialize::deserialize(&mut state);
println!("{}", v);
////
let tokens = vec!(
Null,
);
let mut state = MyDeserializer::new(tokens.into_iter());

View File

@ -10,26 +10,56 @@ pub trait Deserialize<D, E> {
pub trait Deserializer<E> {
fn visit<
R,
V: Visitor<Self, R, 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>;
fn syntax_error(&mut self) -> E;
fn end_of_stream_error(&mut self) -> E;
}
pub trait Visitor<D: Deserializer<E>, R, E> {
fn visit_null(&mut self, d: &mut D) -> Result<R, E> {
Err(d.syntax_error())
}
fn visit_int(&mut self, d: &mut D, _v: int) -> Result<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>
>(&mut self, d: &mut D, _v: V) -> Result<R, E> {
>(&mut self, d: &mut D, _visitor: V) -> Result<R, E> {
Err(d.syntax_error())
}
}
/*
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<
T: Deserialize<D, E>,
>(&mut self, d: &mut D) -> Result<Option<T>, E>;
}
*/
pub trait SeqVisitor<D, E> {
fn next<
T: Deserialize<D, E>,
@ -43,6 +73,27 @@ pub trait SeqVisitor<D, E> {
}
}
///////////////////////////////////////////////////////////////////////////////
impl<
D: Deserializer<E>,
E,
> Deserialize<D, E> for () {
fn deserialize(d: &mut D) -> Result<(), E> {
struct Visitor;
impl<D: Deserializer<E>, E> self::Visitor<D, (), E> for Visitor {
fn visit_null(&mut self, _d: &mut D) -> Result<(), E> {
Ok(())
}
}
d.visit(&mut Visitor)
}
}
///////////////////////////////////////////////////////////////////////////////
impl<
D: Deserializer<E>,
E,
@ -60,6 +111,51 @@ impl<
}
}
///////////////////////////////////////////////////////////////////////////////
impl<
T: Deserialize<D, E>,
D: Deserializer<E>,
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()
}
}
///////////////////////////////////////////////////////////////////////////////
impl<
T: Deserialize<D, E>,
D: Deserializer<E>,
@ -81,11 +177,8 @@ impl<
loop {
match visitor.next(d) {
Some(Ok(value)) => {
values.push(value);
}
Some(Err(err)) => {
return Err(err);
Some(value) => {
values.push(try!(value));
}
None => {
break;