Migrate everything over to a visitor approach
This commit is contained in:
parent
a3ac2ab061
commit
00cba6cdc4
@ -1,4 +1,4 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, TreeMap};
|
||||
use std::hash::Hash;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@ -10,12 +10,16 @@ trait Deserialize<S, E> {
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
trait VisitorState<E> {
|
||||
fn visit<
|
||||
V: Visitor<Value, SeqValue, E>,
|
||||
Value,
|
||||
SeqValue,
|
||||
>(&mut self, visitor: &mut V) -> Result<Value, E>;
|
||||
fn syntax_error(&mut self) -> E;
|
||||
|
||||
fn visit<
|
||||
V: Visitor<T, SeqState, MapState, Self, E>,
|
||||
T,
|
||||
SeqState,
|
||||
MapState,
|
||||
>(&mut self, visitor: &mut V) -> Result<T, E>;
|
||||
|
||||
/*
|
||||
fn visit_null(&mut self) -> Result<(), E>;
|
||||
|
||||
fn visit_int(&mut self) -> Result<int, E>;
|
||||
@ -39,20 +43,51 @@ trait VisitorState<E> {
|
||||
V: Deserialize<Self, E>
|
||||
>(&mut self) -> Result<(K, V), E>;
|
||||
*/
|
||||
*/
|
||||
}
|
||||
|
||||
trait Visitor<Value, SeqValue, E> {
|
||||
fn visit_null(&mut self) -> Result<Value, E>;
|
||||
trait Visitor<
|
||||
T,
|
||||
SeqState,
|
||||
MapState,
|
||||
S: VisitorState<E>,
|
||||
E,
|
||||
> {
|
||||
fn visit_null(&mut self, state: &mut S) -> Result<T, E> {
|
||||
Err(state.syntax_error())
|
||||
}
|
||||
|
||||
fn visit_int(&mut self, v: int) -> Result<Value, E>;
|
||||
fn visit_int(&mut self, state: &mut S, _v: int) -> Result<T, E> {
|
||||
Err(state.syntax_error())
|
||||
}
|
||||
|
||||
fn visit_string(&mut self, v: String) -> Result<Value, E>;
|
||||
fn visit_string(&mut self, state: &mut S, _v: String) -> Result<T, E> {
|
||||
Err(state.syntax_error())
|
||||
}
|
||||
|
||||
fn visit_seq(&mut self, len: uint) -> Result<SeqValue, E>;
|
||||
fn visit_seq(&mut self, state: &mut S, _len: uint) -> Result<SeqState, E> {
|
||||
Err(state.syntax_error())
|
||||
}
|
||||
|
||||
fn visit_seq_elt(&mut self, values: &mut SeqValue, value: Value) -> Result<(), E>;
|
||||
fn visit_seq_elt(&mut self, state: &mut S, _values: &mut SeqState) -> Result<(), E> {
|
||||
Err(state.syntax_error())
|
||||
}
|
||||
|
||||
fn visit_seq_end(&mut self, values: SeqValue) -> Result<Value, E>;
|
||||
fn visit_seq_end(&mut self, state: &mut S, _values: SeqState) -> Result<T, E> {
|
||||
Err(state.syntax_error())
|
||||
}
|
||||
|
||||
fn visit_map(&mut self, state: &mut S, _len: uint) -> Result<MapState, E> {
|
||||
Err(state.syntax_error())
|
||||
}
|
||||
|
||||
fn visit_map_elt(&mut self, state: &mut S, _values: &mut MapState) -> Result<(), E> {
|
||||
Err(state.syntax_error())
|
||||
}
|
||||
|
||||
fn visit_map_end(&mut self, state: &mut S, _values: MapState) -> Result<T, E> {
|
||||
Err(state.syntax_error())
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@ -82,7 +117,20 @@ impl<
|
||||
E,
|
||||
> Deserialize<S, E> for int {
|
||||
fn deserialize(state: &mut S) -> Result<int, E> {
|
||||
state.visit_int()
|
||||
struct Visitor;
|
||||
|
||||
impl<
|
||||
S: VisitorState<E>,
|
||||
E,
|
||||
> ::Visitor<int, (), (), S, E> for Visitor {
|
||||
fn visit_int(&mut self, _state: &mut S, v: int) -> Result<int, E> {
|
||||
Ok(v)
|
||||
}
|
||||
}
|
||||
|
||||
state.visit(&mut Visitor)
|
||||
|
||||
//state.visit_int()
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,7 +139,20 @@ impl<
|
||||
E,
|
||||
> Deserialize<S, E> for String {
|
||||
fn deserialize(state: &mut S) -> Result<String, E> {
|
||||
state.visit_string()
|
||||
struct Visitor;
|
||||
|
||||
impl<
|
||||
S: VisitorState<E>,
|
||||
E,
|
||||
> ::Visitor<String, (), (), S, E> for Visitor {
|
||||
fn visit_string(&mut self, _state: &mut S, v: String) -> Result<String, E> {
|
||||
Ok(v)
|
||||
}
|
||||
}
|
||||
|
||||
state.visit(&mut Visitor)
|
||||
|
||||
//state.visit_string()
|
||||
}
|
||||
}
|
||||
|
||||
@ -103,6 +164,31 @@ impl<
|
||||
E,
|
||||
> Deserialize<S, E> for Vec<T> {
|
||||
fn deserialize(state: &mut S) -> Result<Vec<T>, E> {
|
||||
struct Visitor;
|
||||
|
||||
impl<
|
||||
T: Deserialize<S, E>,
|
||||
S: VisitorState<E>,
|
||||
E,
|
||||
> ::Visitor<Vec<T>, Vec<T>, (), S, E> for Visitor {
|
||||
fn visit_seq(&mut self, _state: &mut S, len: uint) -> Result<Vec<T>, E> {
|
||||
Ok(Vec::with_capacity(len))
|
||||
}
|
||||
|
||||
fn visit_seq_elt(&mut self, state: &mut S, values: &mut Vec<T>) -> Result<(), E> {
|
||||
let value = try!(Deserialize::deserialize(state));
|
||||
values.push(value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_seq_end(&mut self, _state: &mut S, values: Vec<T>) -> Result<Vec<T>, E> {
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
state.visit(&mut Visitor)
|
||||
|
||||
/*
|
||||
let len = try!(state.visit_seq());
|
||||
let mut value = Vec::with_capacity(len);
|
||||
|
||||
@ -115,6 +201,7 @@ impl<
|
||||
}
|
||||
|
||||
Ok(value)
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,7 +213,20 @@ impl<
|
||||
E
|
||||
> Deserialize<S, E> for () {
|
||||
fn deserialize(state: &mut S) -> Result<(), E> {
|
||||
state.visit_null()
|
||||
struct Visitor;
|
||||
|
||||
impl<
|
||||
S: VisitorState<E>,
|
||||
E,
|
||||
> ::Visitor<(), (), (), S, E> for Visitor {
|
||||
fn visit_null(&mut self, _state: &mut S) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
state.visit(&mut Visitor)
|
||||
|
||||
//state.visit_null()
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,6 +239,81 @@ impl<
|
||||
E
|
||||
> Deserialize<S, E> for (T0, T1) {
|
||||
fn deserialize(state: &mut S) -> Result<(T0, T1), E> {
|
||||
struct Visitor {
|
||||
state: uint,
|
||||
}
|
||||
|
||||
impl<
|
||||
T0: Deserialize<S, E>,
|
||||
T1: Deserialize<S, E>,
|
||||
S: VisitorState<E>,
|
||||
E
|
||||
> ::Visitor<(T0, T1), (Option<T0>, Option<T1>), (), S, E> for Visitor {
|
||||
fn visit_seq(&mut self, _state: &mut S, _len: uint) -> Result<(Option<T0>, Option<T1>), E> {
|
||||
Ok((None, None))
|
||||
}
|
||||
|
||||
fn visit_seq_elt(&mut self, state: &mut S, values: &mut (Option<T0>, Option<T1>)) -> Result<(), E> {
|
||||
match self.state {
|
||||
0 => {
|
||||
*values.mut0() = Some(try!(Deserialize::deserialize(state)));
|
||||
self.state += 1;
|
||||
Ok(())
|
||||
}
|
||||
1 => {
|
||||
*values.mut1() = Some(try!(Deserialize::deserialize(state)));
|
||||
self.state += 1;
|
||||
Ok(())
|
||||
}
|
||||
_ => {
|
||||
Err(state.syntax_error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_seq_end(&mut self, state: &mut S, values: (Option<T0>, Option<T1>)) -> Result<(T0, T1), E> {
|
||||
match values {
|
||||
(Some(t0), Some(t1)) => Ok((t0, t1)),
|
||||
_ => Err(state.syntax_error()),
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
match self.state {
|
||||
0 => {
|
||||
self.state += 1;
|
||||
self.t0 = Some(try!(state.visit_seq_elt()));
|
||||
}
|
||||
1 => {
|
||||
self.state += 1;
|
||||
self.t1 = Some(try!(state.visit_seq_elt()));
|
||||
}
|
||||
_ => fail!()
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn unwrap(self) -> Result<(T0, T1), E> {
|
||||
let t0 = match self.t0 {
|
||||
Some(t0) => t0,
|
||||
None => { fail!(); }
|
||||
};
|
||||
|
||||
let t1 = match self.t1 {
|
||||
Some(t1) => t1,
|
||||
None => { fail!(); }
|
||||
};
|
||||
|
||||
Ok((t0, t1))
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
state.visit(&mut Visitor { state: 0 })
|
||||
|
||||
|
||||
/*
|
||||
let _ = try!(state.visit_seq());
|
||||
|
||||
let t0 = match state.visit_seq_elt() {
|
||||
@ -160,6 +335,7 @@ impl<
|
||||
}
|
||||
|
||||
Ok((t0, t1))
|
||||
*/
|
||||
|
||||
/*
|
||||
struct Visitor<T0, T1> {
|
||||
@ -218,7 +394,6 @@ impl<
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
@ -228,6 +403,34 @@ impl<
|
||||
E
|
||||
> Deserialize<S, E> for HashMap<K, V> {
|
||||
fn deserialize(state: &mut S) -> Result<HashMap<K, V>, E> {
|
||||
struct Visitor;
|
||||
|
||||
impl<
|
||||
K: Deserialize<S, E> + Eq + Hash,
|
||||
V: Deserialize<S, E>,
|
||||
S: VisitorState<E>,
|
||||
E,
|
||||
> ::Visitor<HashMap<K, V>, (), HashMap<K, V>, S, E> for Visitor {
|
||||
fn visit_map(&mut self, _state: &mut S, len: uint) -> Result<HashMap<K, V>, E> {
|
||||
Ok(HashMap::with_capacity(len))
|
||||
}
|
||||
|
||||
fn visit_map_elt(&mut self, state: &mut S, values: &mut HashMap<K, V>) -> Result<(), E> {
|
||||
let key = try!(Deserialize::deserialize(state));
|
||||
let value = try!(Deserialize::deserialize(state));
|
||||
values.insert(key, value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_map_end(&mut self, _state: &mut S, values: HashMap<K, V>) -> Result<HashMap<K, V>, E> {
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
state.visit(&mut Visitor)
|
||||
|
||||
|
||||
/*
|
||||
struct Visitor<K, V> {
|
||||
value: HashMap<K, V>,
|
||||
}
|
||||
@ -256,53 +459,143 @@ impl<
|
||||
}
|
||||
|
||||
state.visit_map::<HashMap<K, V>, Visitor<K, V>>()
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
K: Deserialize<S, E> + Eq + Ord,
|
||||
V: Deserialize<S, E>,
|
||||
S: VisitorState<E>,
|
||||
E
|
||||
> Deserialize<S, E> for TreeMap<K, V> {
|
||||
fn deserialize(state: &mut S) -> Result<TreeMap<K, V>, E> {
|
||||
struct Visitor;
|
||||
|
||||
impl<
|
||||
K: Deserialize<S, E> + Eq + Ord,
|
||||
V: Deserialize<S, E>,
|
||||
S: VisitorState<E>,
|
||||
E,
|
||||
> ::Visitor<TreeMap<K, V>, (), TreeMap<K, V>, S, E> for Visitor {
|
||||
fn visit_map(&mut self, _state: &mut S, _len: uint) -> Result<TreeMap<K, V>, E> {
|
||||
Ok(TreeMap::new())
|
||||
}
|
||||
|
||||
fn visit_map_elt(&mut self, state: &mut S, values: &mut TreeMap<K, V>) -> Result<(), E> {
|
||||
let key = try!(Deserialize::deserialize(state));
|
||||
let value = try!(Deserialize::deserialize(state));
|
||||
values.insert(key, value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_map_end(&mut self, _state: &mut S, values: TreeMap<K, V>) -> Result<TreeMap<K, V>, E> {
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
state.visit(&mut Visitor)
|
||||
|
||||
|
||||
/*
|
||||
struct Visitor<K, V> {
|
||||
value: HashMap<K, V>,
|
||||
}
|
||||
|
||||
impl<
|
||||
K: Deserialize<S, E> + Eq + Hash,
|
||||
V: Deserialize<S, E>,
|
||||
S: VisitorState<E>,
|
||||
E
|
||||
> ::Visitor<HashMap<K, V>, S, E> for Visitor<K, V> {
|
||||
fn new(len: uint) -> Visitor<K, V> {
|
||||
Visitor {
|
||||
value: HashMap::with_capacity(len),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit(&mut self, state: &mut S) -> Result<(), E> {
|
||||
let (key, value) = try!(state.visit_map_elt());
|
||||
self.value.insert(key, value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn unwrap(self) -> Result<HashMap<K, V>, E> {
|
||||
Ok(self.value)
|
||||
}
|
||||
}
|
||||
|
||||
state.visit_map::<HashMap<K, V>, Visitor<K, V>>()
|
||||
*/
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
mod json {
|
||||
enum Value {
|
||||
use std::collections::TreeMap;
|
||||
|
||||
#[deriving(Show)]
|
||||
pub enum Value {
|
||||
Null,
|
||||
Bool(bool),
|
||||
Int(int),
|
||||
String(String),
|
||||
Vec(Vec<Value>),
|
||||
Map(TreeMap<String, Value>),
|
||||
}
|
||||
|
||||
impl<
|
||||
S: super::VisitorState<E>,
|
||||
E
|
||||
E,
|
||||
> super::Deserialize<S, E> for Value {
|
||||
fn deserialize(state: &mut S) -> Result<Value, E> {
|
||||
struct Visitor;
|
||||
|
||||
impl<E> super::Visitor<Value, Vec<Value>, E> for Visitor {
|
||||
fn visit_null(&mut self) -> Result<Value, E> {
|
||||
impl<
|
||||
S: super::VisitorState<E>,
|
||||
E,
|
||||
> super::Visitor<Value, Vec<Value>, TreeMap<String, Value>, S, E> for Visitor {
|
||||
fn visit_null(&mut self, _state: &mut S) -> Result<Value, E> {
|
||||
Ok(Null)
|
||||
}
|
||||
|
||||
fn visit_int(&mut self, v: int) -> Result<Value, E> {
|
||||
fn visit_int(&mut self, _state: &mut S, v: int) -> Result<Value, E> {
|
||||
Ok(Int(v))
|
||||
}
|
||||
|
||||
fn visit_string(&mut self, v: String) -> Result<Value, E> {
|
||||
fn visit_string(&mut self, _state: &mut S, v: String) -> Result<Value, E> {
|
||||
Ok(String(v))
|
||||
}
|
||||
|
||||
fn visit_seq(&mut self, len: uint) -> Result<Vec<Value>, E> {
|
||||
fn visit_seq(&mut self, _state: &mut S, len: uint) -> Result<Vec<Value>, E> {
|
||||
Ok(Vec::with_capacity(len))
|
||||
}
|
||||
|
||||
fn visit_seq_elt(&mut self, values: &mut Vec<Value>, value: Value) -> Result<(), E> {
|
||||
fn visit_seq_elt(&mut self, state: &mut S, values: &mut Vec<Value>) -> Result<(), E> {
|
||||
let value = try!(::Deserialize::deserialize(state));
|
||||
values.push(value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_seq_end(&mut self, values: Vec<Value>) -> Result<Value, E> {
|
||||
fn visit_seq_end(&mut self, _state: &mut S, values: Vec<Value>) -> Result<Value, E> {
|
||||
Ok(Vec(values))
|
||||
}
|
||||
|
||||
fn visit_map(&mut self, _state: &mut S, _len: uint) -> Result<TreeMap<String, Value>, E> {
|
||||
Ok(TreeMap::new())
|
||||
}
|
||||
|
||||
fn visit_map_elt(&mut self, state: &mut S, values: &mut TreeMap<String, Value>) -> Result<(), E> {
|
||||
let key = try!(::Deserialize::deserialize(state));
|
||||
let value = try!(::Deserialize::deserialize(state));
|
||||
values.insert(key, value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_map_end(&mut self, _state: &mut S, values: TreeMap<String, Value>) -> Result<Value, E> {
|
||||
Ok(Map(values))
|
||||
}
|
||||
}
|
||||
|
||||
state.visit(&mut Visitor)
|
||||
@ -368,23 +661,28 @@ impl<
|
||||
> VisitorState<
|
||||
(),
|
||||
> for MyDeserializerState<Iter> {
|
||||
fn syntax_error(&mut self) -> () {
|
||||
()
|
||||
}
|
||||
|
||||
fn visit<
|
||||
V: Visitor<Value, SeqValue, ()>,
|
||||
Value,
|
||||
SeqValue,
|
||||
>(&mut self, visitor: &mut V) -> Result<Value, ()> {
|
||||
V: Visitor<T, SeqState, MapState, MyDeserializerState<Iter>, ()>,
|
||||
T,
|
||||
SeqState,
|
||||
MapState,
|
||||
>(&mut self, visitor: &mut V) -> Result<T, ()> {
|
||||
match self.next() {
|
||||
Some(Null) => {
|
||||
visitor.visit_null()
|
||||
visitor.visit_null(self)
|
||||
}
|
||||
Some(Int(v)) => {
|
||||
visitor.visit_int(v)
|
||||
visitor.visit_int(self, v)
|
||||
}
|
||||
Some(String(v)) => {
|
||||
visitor.visit_string(v)
|
||||
visitor.visit_string(self, v)
|
||||
}
|
||||
Some(SeqStart(len)) => {
|
||||
let mut state = try!(visitor.visit_seq(len));
|
||||
let mut state = try!(visitor.visit_seq(self, len));
|
||||
|
||||
loop {
|
||||
match self.peek() {
|
||||
@ -393,8 +691,7 @@ impl<
|
||||
break;
|
||||
}
|
||||
Some(_) => {
|
||||
let value = try!(self.visit(visitor));
|
||||
try!(visitor.visit_seq_elt(&mut state, value));
|
||||
try!(visitor.visit_seq_elt(self, &mut state));
|
||||
}
|
||||
None => {
|
||||
return Err(());
|
||||
@ -402,10 +699,27 @@ impl<
|
||||
}
|
||||
}
|
||||
|
||||
visitor.visit_seq_end(state)
|
||||
visitor.visit_seq_end(self, state)
|
||||
}
|
||||
Some(MapStart(len)) => {
|
||||
Err(())
|
||||
let mut state = try!(visitor.visit_map(self, len));
|
||||
|
||||
loop {
|
||||
match self.peek() {
|
||||
Some(&End) => {
|
||||
self.next();
|
||||
break;
|
||||
}
|
||||
Some(_) => {
|
||||
try!(visitor.visit_map_elt(self, &mut state));
|
||||
}
|
||||
None => {
|
||||
return Err(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
visitor.visit_map_end(self, state)
|
||||
}
|
||||
Some(End) => {
|
||||
Err(())
|
||||
@ -417,6 +731,7 @@ impl<
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
fn visit_null(&mut self) -> Result<(), ()> {
|
||||
match self.next() {
|
||||
Some(Null) => Ok(()),
|
||||
@ -497,6 +812,7 @@ impl<
|
||||
Ok((k, v))
|
||||
}
|
||||
*/
|
||||
*/
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@ -513,6 +829,8 @@ fn main() {
|
||||
let v: Result<Vec<int>, ()> = Deserialize::deserialize(&mut state);
|
||||
println!("{}", v);
|
||||
|
||||
////
|
||||
|
||||
let tokens = vec!(
|
||||
SeqStart(2),
|
||||
Int(1),
|
||||
@ -524,7 +842,21 @@ fn main() {
|
||||
let v: Result<(int, int), ()> = Deserialize::deserialize(&mut state);
|
||||
println!("{}", v);
|
||||
|
||||
/*
|
||||
////
|
||||
|
||||
let tokens = vec!(
|
||||
SeqStart(2),
|
||||
Int(1),
|
||||
Int(2),
|
||||
End
|
||||
);
|
||||
let mut state = MyDeserializerState::new(tokens.move_iter());
|
||||
|
||||
let v: Result<json::Value, ()> = Deserialize::deserialize(&mut state);
|
||||
println!("{}", v);
|
||||
|
||||
////
|
||||
|
||||
let tokens = vec!(
|
||||
MapStart(2),
|
||||
String("a".to_string()),
|
||||
@ -537,5 +869,19 @@ fn main() {
|
||||
|
||||
let v: Result<HashMap<String, int>, ()> = Deserialize::deserialize(&mut state);
|
||||
println!("{}", v);
|
||||
*/
|
||||
|
||||
////
|
||||
|
||||
let tokens = vec!(
|
||||
MapStart(2),
|
||||
String("a".to_string()),
|
||||
Int(1),
|
||||
String("b".to_string()),
|
||||
Int(2),
|
||||
End
|
||||
);
|
||||
let mut state = MyDeserializerState::new(tokens.move_iter());
|
||||
|
||||
let v: Result<json::Value, ()> = Deserialize::deserialize(&mut state);
|
||||
println!("{}", v);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user