1834
1834
// Also, macros should be imported before using them
1835
1835
use serde:: serde_if_integer128;
1836
1836
1837
- macro_rules! deserialize_type {
1837
+ macro_rules! deserialize_num {
1838
1838
( $deserialize: ident => $visit: ident, $( $mut: tt) ?) => {
1839
1839
fn $deserialize<V >( $( $mut) ? self , visitor: V ) -> Result <V :: Value , DeError >
1840
1840
where
1841
1841
V : Visitor <' de>,
1842
1842
{
1843
1843
// No need to unescape because valid integer representations cannot be escaped
1844
1844
let text = self . read_string( ) ?;
1845
- visitor. $visit( text. parse( ) ?)
1845
+ match text. parse( ) {
1846
+ Ok ( number) => visitor. $visit( number) ,
1847
+ Err ( _) => match text {
1848
+ Cow :: Borrowed ( t) => visitor. visit_str( t) ,
1849
+ Cow :: Owned ( t) => visitor. visit_string( t) ,
1850
+ }
1851
+ }
1846
1852
}
1847
1853
} ;
1848
1854
}
@@ -1851,31 +1857,33 @@ macro_rules! deserialize_type {
1851
1857
/// byte arrays, booleans and identifiers.
1852
1858
macro_rules! deserialize_primitives {
1853
1859
( $( $mut: tt) ?) => {
1854
- deserialize_type !( deserialize_i8 => visit_i8, $( $mut) ?) ;
1855
- deserialize_type !( deserialize_i16 => visit_i16, $( $mut) ?) ;
1856
- deserialize_type !( deserialize_i32 => visit_i32, $( $mut) ?) ;
1857
- deserialize_type !( deserialize_i64 => visit_i64, $( $mut) ?) ;
1860
+ deserialize_num !( deserialize_i8 => visit_i8, $( $mut) ?) ;
1861
+ deserialize_num !( deserialize_i16 => visit_i16, $( $mut) ?) ;
1862
+ deserialize_num !( deserialize_i32 => visit_i32, $( $mut) ?) ;
1863
+ deserialize_num !( deserialize_i64 => visit_i64, $( $mut) ?) ;
1858
1864
1859
- deserialize_type !( deserialize_u8 => visit_u8, $( $mut) ?) ;
1860
- deserialize_type !( deserialize_u16 => visit_u16, $( $mut) ?) ;
1861
- deserialize_type !( deserialize_u32 => visit_u32, $( $mut) ?) ;
1862
- deserialize_type !( deserialize_u64 => visit_u64, $( $mut) ?) ;
1865
+ deserialize_num !( deserialize_u8 => visit_u8, $( $mut) ?) ;
1866
+ deserialize_num !( deserialize_u16 => visit_u16, $( $mut) ?) ;
1867
+ deserialize_num !( deserialize_u32 => visit_u32, $( $mut) ?) ;
1868
+ deserialize_num !( deserialize_u64 => visit_u64, $( $mut) ?) ;
1863
1869
1864
1870
serde_if_integer128! {
1865
- deserialize_type !( deserialize_i128 => visit_i128, $( $mut) ?) ;
1866
- deserialize_type !( deserialize_u128 => visit_u128, $( $mut) ?) ;
1871
+ deserialize_num !( deserialize_i128 => visit_i128, $( $mut) ?) ;
1872
+ deserialize_num !( deserialize_u128 => visit_u128, $( $mut) ?) ;
1867
1873
}
1868
1874
1869
- deserialize_type !( deserialize_f32 => visit_f32, $( $mut) ?) ;
1870
- deserialize_type !( deserialize_f64 => visit_f64, $( $mut) ?) ;
1875
+ deserialize_num !( deserialize_f32 => visit_f32, $( $mut) ?) ;
1876
+ deserialize_num !( deserialize_f64 => visit_f64, $( $mut) ?) ;
1871
1877
1872
1878
fn deserialize_bool<V >( $( $mut) ? self , visitor: V ) -> Result <V :: Value , DeError >
1873
1879
where
1874
1880
V : Visitor <' de>,
1875
1881
{
1876
- let text = self . read_string( ) ?;
1877
-
1878
- str2bool( & text, visitor)
1882
+ let text = match self . read_string( ) ? {
1883
+ Cow :: Borrowed ( s) => CowRef :: Input ( s) ,
1884
+ Cow :: Owned ( s) => CowRef :: Owned ( s) ,
1885
+ } ;
1886
+ text. deserialize_bool( visitor)
1879
1887
}
1880
1888
1881
1889
/// Character represented as [strings](#method.deserialize_str).
@@ -1998,8 +2006,9 @@ mod simple_type;
1998
2006
mod text;
1999
2007
mod var;
2000
2008
2009
+ pub use self :: resolver:: { EntityResolver , PredefinedEntityResolver } ;
2010
+ pub use self :: simple_type:: SimpleTypeDeserializer ;
2001
2011
pub use crate :: errors:: serialize:: DeError ;
2002
- pub use resolver:: { EntityResolver , PredefinedEntityResolver } ;
2003
2012
2004
2013
use crate :: {
2005
2014
de:: map:: ElementMapAccess ,
@@ -2008,8 +2017,11 @@ use crate::{
2008
2017
events:: { BytesCData , BytesEnd , BytesStart , BytesText , Event } ,
2009
2018
name:: QName ,
2010
2019
reader:: Reader ,
2020
+ utils:: CowRef ,
2021
+ } ;
2022
+ use serde:: de:: {
2023
+ self , Deserialize , DeserializeOwned , DeserializeSeed , IntoDeserializer , SeqAccess , Visitor ,
2011
2024
} ;
2012
- use serde:: de:: { self , Deserialize , DeserializeOwned , DeserializeSeed , SeqAccess , Visitor } ;
2013
2025
use std:: borrow:: Cow ;
2014
2026
#[ cfg( feature = "overlapped-lists" ) ]
2015
2027
use std:: collections:: VecDeque ;
@@ -2058,6 +2070,22 @@ impl<'a> From<&'a str> for Text<'a> {
2058
2070
}
2059
2071
}
2060
2072
2073
+ impl < ' a > From < String > for Text < ' a > {
2074
+ #[ inline]
2075
+ fn from ( text : String ) -> Self {
2076
+ Self {
2077
+ text : Cow :: Owned ( text) ,
2078
+ }
2079
+ }
2080
+ }
2081
+
2082
+ impl < ' a > From < Cow < ' a , str > > for Text < ' a > {
2083
+ #[ inline]
2084
+ fn from ( text : Cow < ' a , str > ) -> Self {
2085
+ Self { text }
2086
+ }
2087
+ }
2088
+
2061
2089
////////////////////////////////////////////////////////////////////////////////////////////////////
2062
2090
2063
2091
/// Simplified event which contains only these variants that used by deserializer
@@ -2287,7 +2315,7 @@ where
2287
2315
}
2288
2316
2289
2317
/// Deserialize from a reader. This method will do internal copies of data
2290
- /// readed from `reader`. If you want have a `&str` input and want to borrow
2318
+ /// read from `reader`. If you want have a `&str` input and want to borrow
2291
2319
/// as much as possible, use [`from_str`].
2292
2320
pub fn from_reader < R , T > ( reader : R ) -> Result < T , DeError >
2293
2321
where
@@ -2298,49 +2326,6 @@ where
2298
2326
T :: deserialize ( & mut de)
2299
2327
}
2300
2328
2301
- // TODO: According to the https://www.w3.org/TR/xmlschema11-2/#boolean,
2302
- // valid boolean representations are only "true", "false", "1", and "0"
2303
- fn str2bool < ' de , V > ( value : & str , visitor : V ) -> Result < V :: Value , DeError >
2304
- where
2305
- V : de:: Visitor < ' de > ,
2306
- {
2307
- match value {
2308
- "true" | "1" | "True" | "TRUE" | "t" | "Yes" | "YES" | "yes" | "y" => {
2309
- visitor. visit_bool ( true )
2310
- }
2311
- "false" | "0" | "False" | "FALSE" | "f" | "No" | "NO" | "no" | "n" => {
2312
- visitor. visit_bool ( false )
2313
- }
2314
- _ => Err ( DeError :: InvalidBoolean ( value. into ( ) ) ) ,
2315
- }
2316
- }
2317
-
2318
- fn deserialize_bool < ' de , V > ( value : & [ u8 ] , decoder : Decoder , visitor : V ) -> Result < V :: Value , DeError >
2319
- where
2320
- V : Visitor < ' de > ,
2321
- {
2322
- #[ cfg( feature = "encoding" ) ]
2323
- {
2324
- let value = decoder. decode ( value) ?;
2325
- // No need to unescape because valid boolean representations cannot be escaped
2326
- str2bool ( value. as_ref ( ) , visitor)
2327
- }
2328
-
2329
- #[ cfg( not( feature = "encoding" ) ) ]
2330
- {
2331
- // No need to unescape because valid boolean representations cannot be escaped
2332
- match value {
2333
- b"true" | b"1" | b"True" | b"TRUE" | b"t" | b"Yes" | b"YES" | b"yes" | b"y" => {
2334
- visitor. visit_bool ( true )
2335
- }
2336
- b"false" | b"0" | b"False" | b"FALSE" | b"f" | b"No" | b"NO" | b"no" | b"n" => {
2337
- visitor. visit_bool ( false )
2338
- }
2339
- e => Err ( DeError :: InvalidBoolean ( decoder. decode ( e) ?. into ( ) ) ) ,
2340
- }
2341
- }
2342
- }
2343
-
2344
2329
////////////////////////////////////////////////////////////////////////////////////////////////////
2345
2330
2346
2331
/// A structure that deserializes XML into Rust values.
@@ -3007,6 +2992,19 @@ where
3007
2992
}
3008
2993
}
3009
2994
2995
+ impl < ' de , ' a , R , E > IntoDeserializer < ' de , DeError > for & ' a mut Deserializer < ' de , R , E >
2996
+ where
2997
+ R : XmlRead < ' de > ,
2998
+ E : EntityResolver ,
2999
+ {
3000
+ type Deserializer = Self ;
3001
+
3002
+ #[ inline]
3003
+ fn into_deserializer ( self ) -> Self {
3004
+ self
3005
+ }
3006
+ }
3007
+
3010
3008
////////////////////////////////////////////////////////////////////////////////////////////////////
3011
3009
3012
3010
/// Helper struct that contains a state for an algorithm of converting events
0 commit comments