mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-15 21:21:01 +00:00
changes needed for xml parsing
This commit is contained in:
+53
-3
@@ -4,6 +4,7 @@ use std::marker::PhantomData;
|
|||||||
use std::path;
|
use std::path;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::str;
|
||||||
|
|
||||||
use num::FromPrimitive;
|
use num::FromPrimitive;
|
||||||
|
|
||||||
@@ -56,6 +57,16 @@ impl Visitor for BoolVisitor {
|
|||||||
{
|
{
|
||||||
Ok(v)
|
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 {
|
impl Deserialize for bool {
|
||||||
@@ -82,6 +93,10 @@ macro_rules! impl_deserialize_num_method {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct NumericVisitor<T> {
|
||||||
|
marker: PhantomData<T>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct PrimitiveVisitor<T> {
|
pub struct PrimitiveVisitor<T> {
|
||||||
marker: PhantomData<T>,
|
marker: PhantomData<T>,
|
||||||
}
|
}
|
||||||
@@ -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<
|
impl<
|
||||||
T: Deserialize + FromPrimitive
|
T: Deserialize + FromPrimitive
|
||||||
> Visitor for PrimitiveVisitor<T> {
|
> Visitor for PrimitiveVisitor<T> {
|
||||||
@@ -121,7 +171,7 @@ macro_rules! impl_deserialize_num {
|
|||||||
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
{
|
{
|
||||||
deserializer.visit(PrimitiveVisitor::new())
|
deserializer.visit(NumericVisitor::new())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -394,7 +444,7 @@ impl<T> Deserialize for Vec<T>
|
|||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Vec<T>, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<Vec<T>, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
{
|
{
|
||||||
deserializer.visit(VecVisitor::new())
|
deserializer.visit_seq(VecVisitor::new())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -438,7 +488,7 @@ macro_rules! tuple_impls {
|
|||||||
fn deserialize<D>(deserializer: &mut D) -> Result<($($name,)+), D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<($($name,)+), D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
{
|
{
|
||||||
deserializer.visit($visitor { marker: PhantomData })
|
deserializer.visit_seq($visitor { marker: PhantomData })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)+
|
)+
|
||||||
|
|||||||
+1
-1
@@ -118,7 +118,7 @@ pub trait Deserializer {
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
pub trait Visitor {
|
pub trait Visitor {
|
||||||
type Value;
|
type Value: Deserialize;
|
||||||
|
|
||||||
fn visit_bool<E>(&mut self, _v: bool) -> Result<Self::Value, E>
|
fn visit_bool<E>(&mut self, _v: bool) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
|
|||||||
+9
-5
@@ -1,7 +1,8 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
|
use std::iter::Peekable;
|
||||||
|
|
||||||
pub struct LineColIterator<Iter: Iterator<Item=io::Result<u8>>> {
|
pub struct LineColIterator<Iter: Iterator<Item=io::Result<u8>>> {
|
||||||
iter: Iter,
|
iter: Peekable<Iter>,
|
||||||
line: usize,
|
line: usize,
|
||||||
col: usize,
|
col: usize,
|
||||||
}
|
}
|
||||||
@@ -9,7 +10,7 @@ pub struct LineColIterator<Iter: Iterator<Item=io::Result<u8>>> {
|
|||||||
impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Iter> {
|
impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Iter> {
|
||||||
pub fn new(iter: Iter) -> LineColIterator<Iter> {
|
pub fn new(iter: Iter) -> LineColIterator<Iter> {
|
||||||
LineColIterator {
|
LineColIterator {
|
||||||
iter: iter,
|
iter: iter.peekable(),
|
||||||
line: 1,
|
line: 1,
|
||||||
col: 0,
|
col: 0,
|
||||||
}
|
}
|
||||||
@@ -22,13 +23,16 @@ impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Iter> {
|
|||||||
pub fn col(&self) -> usize { self.col }
|
pub fn col(&self) -> usize { self.col }
|
||||||
|
|
||||||
/// Gets a reference to the underlying iterator.
|
/// 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.
|
/// 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.
|
/// 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> {
|
impl<Iter: Iterator<Item=io::Result<u8>>> Iterator for LineColIterator<Iter> {
|
||||||
|
|||||||
Reference in New Issue
Block a user