re-cleaning up

This commit is contained in:
Erick Tryzelaar 2014-10-06 21:02:58 -07:00
parent 3ec686cab1
commit 4755578c08
2 changed files with 40 additions and 107 deletions

View File

@ -73,15 +73,9 @@ 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 })
@ -101,47 +95,28 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
}
fn visit_option<
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())
}
}
/*
R,
V: de2::Visitor<MyDeserializer<Iter>, R, Error>,
>(&mut self, visitor: &mut V) -> Result<R, Error> {
match self.peek() {
Some(&Null) => {
self.next();
Ok(None)
visitor.visit_option(self, MyOptionVisitor {
is_some: false,
})
}
Some(&Option(true)) => {
Some(&Option(is_some)) => {
self.next();
let v = try!(Deserialize::deserialize(self));
Ok(Some(v))
}
Some(&Option(false)) => {
self.next();
Ok(None)
visitor.visit_option(self, MyOptionVisitor {
is_some: is_some,
})
}
_ => {
let v = try!(Deserialize::deserialize(self));
Ok(Some(v))
visitor.visit_option(self, MyOptionVisitor {
is_some: true,
})
}
}
*/
}
fn syntax_error(&mut self) -> Error {
@ -153,10 +128,8 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
}
}
/*
struct MyOptionVisitor {
is_some: bool,
finished: bool,
}
impl<
@ -165,21 +138,15 @@ impl<
fn visit<
T: Deserialize<MyDeserializer<Iter>, Error>,
>(&mut self, d: &mut MyDeserializer<Iter>) -> Result<option::Option<T>, Error> {
if self.finished {
Err(d.syntax_error())
if self.is_some {
self.is_some = false;
let value = try!(Deserialize::deserialize(d));
Ok(Some(value))
} else {
self.finished = true;
if self.is_some {
let v = try!(Deserialize::deserialize(d));
Ok(Some(v))
} else {
Ok(None)
}
Ok(None)
}
}
}
*/
struct MySeqVisitor {
len: uint,
@ -290,7 +257,6 @@ mod json {
}
*/
/*
fn visit_option<
Visitor: de2::OptionVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: Visitor) -> Result<Value, E> {
@ -299,7 +265,6 @@ mod json {
None => Ok(Null),
}
}
*/
fn visit_seq<
Visitor: de2::SeqVisitor<D, E>,

View File

@ -13,18 +13,12 @@ pub trait Deserializer<E> {
V: Visitor<Self, R, E>,
>(&mut self, visitor: &mut V) -> Result<R, E>;
/*
fn visit_option<
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;
@ -49,31 +43,10 @@ pub trait Visitor<D: Deserializer<E>, R, E> {
}
fn visit_option<
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>,
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>
@ -82,10 +55,10 @@ pub trait Visitor<D: Deserializer<E>, R, 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 OptionVisitor<D, E> {
fn visit<
T: Deserialize<D, E>,
>(&mut self, d: &mut D) -> Result<Option<T>, E>;
}
pub trait SeqVisitor<D, E> {
@ -166,7 +139,6 @@ impl<
E,
> Deserialize<D, E> for Option<T> {
fn deserialize(d: &mut D) -> Result<Option<T>, E> {
/*
struct Visitor;
impl<
@ -174,15 +146,14 @@ impl<
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)
fn visit_option<
V: OptionVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: V) -> Result<Option<T>, E> {
visitor.visit(d)
}
}
d.visit_option(&mut Visitor)
*/
d.visit_option()
}
}
@ -296,7 +267,7 @@ trait Deserialize<S, E> {
///////////////////////////////////////////////////////////////////////////////
trait Deserializer<S, E> {
fn visit<R>(&mut self, state: &mut S) -> Result<R, E>;
fn visit<R>(&mut self, state: &mut S) -> Result<(), E>;
fn syntax_error(&mut self) -> E;
}
@ -314,7 +285,7 @@ trait DeserializerState<E> {
}
trait Visitor<R, E> {
fn visit<S>(&mut self, state: &mut S) -> Result<R, E>;
fn visit<S>(&mut self, state: &mut S) -> Result<(), E>;
fn size_hint(&self) -> (uint, Option<uint>);
}
@ -335,43 +306,43 @@ trait VisitorState<
}
/*
fn visit_i8(&mut self, d: &mut D, v: i8) -> Result<R, E> {
fn visit_i8(&mut self, d: &mut D, v: i8) -> Result<(), E> {
self.visit_i64(d, v as i64)
}
fn visit_i16(&mut self, d: &mut D, v: i16) -> Result<R, E> {
fn visit_i16(&mut self, d: &mut D, v: i16) -> Result<(), E> {
self.visit_i64(d, v as i64)
}
fn visit_i32(&mut self, d: &mut D, v: i32) -> Result<R, E> {
fn visit_i32(&mut self, d: &mut D, v: i32) -> Result<(), E> {
self.visit_i64(d, v as i64)
}
fn visit_i64(&mut self, d: &mut D, v: i64) -> Result<R, E> {
fn visit_i64(&mut self, d: &mut D, v: i64) -> Result<(), E> {
Err(d.syntax_error())
}
fn visit_uint(&mut self, d: &mut D, v: int) -> Result<R, E> {
fn visit_uint(&mut self, d: &mut D, v: int) -> Result<(), E> {
self.visit_u64(d, v as u64)
}
fn visit_u8(&mut self, d: &mut D, v: u8) -> Result<R, E> {
fn visit_u8(&mut self, d: &mut D, v: u8) -> Result<(), E> {
self.visit_u64(d, v as u64)
}
fn visit_u16(&mut self, d: &mut D, v: u16) -> Result<R, E> {
fn visit_u16(&mut self, d: &mut D, v: u16) -> Result<(), E> {
self.visit_u64(d, v as u64)
}
fn visit_u32(&mut self, d: &mut D, v: u32) -> Result<R, E> {
fn visit_u32(&mut self, d: &mut D, v: u32) -> Result<(), E> {
self.visit_u64(d, v as u64)
}
fn visit_u64(&mut self, d: &mut D, _v: u64) -> Result<R, E> {
fn visit_u64(&mut self, d: &mut D, _v: u64) -> Result<(), E> {
Err(d.syntax_error())
}
fn visit_string(&mut self, d: &mut D, _v: String) -> Result<R, E> {
fn visit_string(&mut self, d: &mut D, _v: String) -> Result<(), E> {
Err(d.syntax_error())
}
*/
@ -379,7 +350,7 @@ trait VisitorState<
fn visit_seq<
R,
V: SeqVisitor<D, E>,
>(&mut self, d: &mut D, _visitor: V) -> Result<R, E> {
>(&mut self, d: &mut D, _visitor: V) -> Result<(), E> {
Err(d.syntax_error())
}
@ -746,6 +717,3 @@ impl<
*/
*/
///////////////////////////////////////////////////////////////////////////////