Skip to content

Commit

Permalink
changes needed for xml parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Schneider authored and hugoduncan committed May 5, 2015
1 parent 9393cb9 commit 0d68d66
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
56 changes: 53 additions & 3 deletions src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::marker::PhantomData;
use std::path;
use std::rc::Rc;
use std::sync::Arc;
use std::str;

use num::FromPrimitive;

Expand Down Expand Up @@ -56,6 +57,16 @@ impl Visitor for BoolVisitor {
{
Ok(v)
}

fn visit_str<E>(&mut self, s: &str) -> Result<bool, E>
where E: Error,
{
match s.trim() {
"true" => Ok(true),
"false" => Ok(false),
_ => Err(Error::syntax_error()),
}
}
}

impl Deserialize for bool {
Expand All @@ -82,6 +93,10 @@ macro_rules! impl_deserialize_num_method {
}
}

pub struct NumericVisitor<T> {
marker: PhantomData<T>,
}

pub struct PrimitiveVisitor<T> {
marker: PhantomData<T>,
}
Expand All @@ -95,6 +110,41 @@ impl<T> PrimitiveVisitor<T> {
}
}

impl<T> NumericVisitor<T> {
#[inline]
pub fn new() -> Self {
NumericVisitor {
marker: PhantomData,
}
}
}

impl<
T: Deserialize + FromPrimitive + str::FromStr
> Visitor for NumericVisitor<T> {
type Value = T;

impl_deserialize_num_method!(isize, visit_isize, from_isize);
impl_deserialize_num_method!(i8, visit_i8, from_i8);
impl_deserialize_num_method!(i16, visit_i16, from_i16);
impl_deserialize_num_method!(i32, visit_i32, from_i32);
impl_deserialize_num_method!(i64, visit_i64, from_i64);
impl_deserialize_num_method!(usize, visit_usize, from_usize);
impl_deserialize_num_method!(u8, visit_u8, from_u8);
impl_deserialize_num_method!(u16, visit_u16, from_u16);
impl_deserialize_num_method!(u32, visit_u32, from_u32);
impl_deserialize_num_method!(u64, visit_u64, from_u64);
impl_deserialize_num_method!(f32, visit_f32, from_f32);
impl_deserialize_num_method!(f64, visit_f64, from_f64);

#[inline]
fn visit_str<E>(&mut self, v: &str) -> Result<T, E>
where E: Error,
{
str::FromStr::from_str(v.trim()).or(Err(Error::syntax_error()))
}
}

impl<
T: Deserialize + FromPrimitive
> Visitor for PrimitiveVisitor<T> {
Expand All @@ -121,7 +171,7 @@ macro_rules! impl_deserialize_num {
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
where D: Deserializer,
{
deserializer.visit(PrimitiveVisitor::new())
deserializer.visit(NumericVisitor::new())
}
}
}
Expand Down Expand Up @@ -394,7 +444,7 @@ impl<T> Deserialize for Vec<T>
fn deserialize<D>(deserializer: &mut D) -> Result<Vec<T>, D::Error>
where D: Deserializer,
{
deserializer.visit(VecVisitor::new())
deserializer.visit_seq(VecVisitor::new())
}
}

Expand Down Expand Up @@ -438,7 +488,7 @@ macro_rules! tuple_impls {
fn deserialize<D>(deserializer: &mut D) -> Result<($($name,)+), D::Error>
where D: Deserializer,
{
deserializer.visit($visitor { marker: PhantomData })
deserializer.visit_seq($visitor { marker: PhantomData })
}
}
)+
Expand Down
2 changes: 1 addition & 1 deletion src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub trait Deserializer {
///////////////////////////////////////////////////////////////////////////////

pub trait Visitor {
type Value;
type Value: Deserialize;

fn visit_bool<E>(&mut self, _v: bool) -> Result<Self::Value, E>
where E: Error,
Expand Down
14 changes: 9 additions & 5 deletions src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::io;
use std::iter::Peekable;

pub struct LineColIterator<Iter: Iterator<Item=io::Result<u8>>> {
iter: Iter,
iter: Peekable<Iter>,
line: usize,
col: usize,
}

impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Iter> {
pub fn new(iter: Iter) -> LineColIterator<Iter> {
LineColIterator {
iter: iter,
iter: iter.peekable(),
line: 1,
col: 0,
}
Expand All @@ -22,13 +23,16 @@ impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Iter> {
pub fn col(&self) -> usize { self.col }

/// Gets a reference to the underlying iterator.
pub fn get_ref(&self) -> &Iter { &self.iter }
pub fn get_ref(&self) -> &Peekable<Iter> { &self.iter }

/// Gets a mutable reference to the underlying iterator.
pub fn get_mut(&self) -> &Iter { &self.iter }
pub fn get_mut(&mut self) -> &mut Peekable<Iter> { &mut self.iter }

/// Unwraps this `LineColIterator`, returning the underlying iterator.
pub fn into_inner(self) -> Iter { self.iter }
pub fn into_inner(self) -> Peekable<Iter> { self.iter }

/// peeks at the next value
pub fn peek(&mut self) -> Option<&io::Result<u8>> { self.iter.peek() }
}

impl<Iter: Iterator<Item=io::Result<u8>>> Iterator for LineColIterator<Iter> {
Expand Down

0 comments on commit 0d68d66

Please sign in to comment.