simplify deserializing a value from a primitive
This commit is contained in:
parent
dbc1cbcbfb
commit
499638eccd
@ -51,11 +51,7 @@ impl de::Deserializable for HttpProtocol {
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<HttpProtocol, E> {
|
||||
let x: uint = try!(de::Deserializable::deserialize_token(d, token));
|
||||
match FromPrimitive::from_uint(x) {
|
||||
Some(x) => Ok(x),
|
||||
None => d.syntax_error(),
|
||||
}
|
||||
d.expect_from_primitive(token)
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,11 +86,7 @@ impl de::Deserializable for HttpMethod {
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<HttpMethod, E> {
|
||||
let x: uint = try!(de::Deserializable::deserialize_token(d, token));
|
||||
match FromPrimitive::from_uint(x) {
|
||||
Some(x) => Ok(x),
|
||||
None => d.syntax_error(),
|
||||
}
|
||||
d.expect_from_primitive(token)
|
||||
}
|
||||
}
|
||||
|
||||
@ -122,11 +114,7 @@ impl de::Deserializable for CacheStatus {
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<CacheStatus, E> {
|
||||
let x: uint = try!(de::Deserializable::deserialize_token(d, token));
|
||||
match FromPrimitive::from_uint(x) {
|
||||
Some(x) => Ok(x),
|
||||
None => d.syntax_error(),
|
||||
}
|
||||
d.expect_from_primitive(token)
|
||||
}
|
||||
}
|
||||
|
||||
@ -163,11 +151,7 @@ impl de::Deserializable for OriginProtocol {
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<OriginProtocol, E> {
|
||||
let x: uint = try!(de::Deserializable::deserialize_token(d, token));
|
||||
match FromPrimitive::from_uint(x) {
|
||||
Some(x) => Ok(x),
|
||||
None => d.syntax_error(),
|
||||
}
|
||||
d.expect_from_primitive(token)
|
||||
}
|
||||
}
|
||||
|
||||
@ -196,11 +180,7 @@ impl de::Deserializable for ZonePlan {
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<ZonePlan, E> {
|
||||
let x: uint = try!(de::Deserializable::deserialize_token(d, token));
|
||||
match FromPrimitive::from_uint(x) {
|
||||
Some(x) => Ok(x),
|
||||
None => d.syntax_error(),
|
||||
}
|
||||
d.expect_from_primitive(token)
|
||||
}
|
||||
}
|
||||
|
||||
@ -480,11 +460,7 @@ impl de::Deserializable for Country {
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<Country, E> {
|
||||
let x: uint = try!(de::Deserializable::deserialize_token(d, token));
|
||||
match FromPrimitive::from_uint(x) {
|
||||
Some(x) => Ok(x),
|
||||
None => d.syntax_error(),
|
||||
}
|
||||
d.expect_from_primitive(token)
|
||||
}
|
||||
}
|
||||
|
||||
|
16
src/de.rs
16
src/de.rs
@ -102,6 +102,22 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
U64(x) => to_result!(num::cast(x), self.syntax_error()),
|
||||
F32(x) => to_result!(num::cast(x), self.syntax_error()),
|
||||
F64(x) => to_result!(num::cast(x), self.syntax_error()),
|
||||
|
||||
#[inline]
|
||||
fn expect_from_primitive<T: FromPrimitive>(&mut self, token: Token) -> Result<T, E> {
|
||||
match token {
|
||||
Int(x) => to_result!(num::from_int(x), self.syntax_error()),
|
||||
I8(x) => to_result!(num::from_i8(x), self.syntax_error()),
|
||||
I16(x) => to_result!(num::from_i16(x), self.syntax_error()),
|
||||
I32(x) => to_result!(num::from_i32(x), self.syntax_error()),
|
||||
I64(x) => to_result!(num::from_i64(x), self.syntax_error()),
|
||||
Uint(x) => to_result!(num::from_uint(x), self.syntax_error()),
|
||||
U8(x) => to_result!(num::from_u8(x), self.syntax_error()),
|
||||
U16(x) => to_result!(num::from_u16(x), self.syntax_error()),
|
||||
U32(x) => to_result!(num::from_u32(x), self.syntax_error()),
|
||||
U64(x) => to_result!(num::from_u64(x), self.syntax_error()),
|
||||
F32(x) => to_result!(num::from_f32(x), self.syntax_error()),
|
||||
F64(x) => to_result!(num::from_f64(x), self.syntax_error()),
|
||||
_ => self.syntax_error(),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user