mirror of https://github.com/rwf2/Rocket.git
Implemented FromFormValue for all of the FromStrs.
This commit is contained in:
parent
d0dd49f98d
commit
2dbcfa10e3
|
@ -1,4 +1,6 @@
|
|||
use std::str::FromStr;
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr};
|
||||
|
||||
use error::Error;
|
||||
|
||||
pub trait FromForm<'f>: Sized {
|
||||
|
@ -19,13 +21,20 @@ impl<'v> FromFormValue<'v> for &'v str {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'v> FromFormValue<'v> for isize {
|
||||
macro_rules! impl_with_fromstr {
|
||||
($($T:ident),+) => ($(
|
||||
impl<'v> FromFormValue<'v> for $T {
|
||||
type Error = &'v str;
|
||||
|
||||
fn parse(v: &'v str) -> Result<Self, Self::Error> {
|
||||
isize::from_str(v).map_err(|_| v)
|
||||
$T::from_str(v).map_err(|_| v)
|
||||
}
|
||||
}
|
||||
)+)
|
||||
}
|
||||
|
||||
impl_with_fromstr!(f32, f64, isize, i8, i16, i32, i64, usize, u8, u16, u32, u64,
|
||||
bool, String, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6,
|
||||
SocketAddr);
|
||||
|
||||
impl<'v, T: FromFormValue<'v>> FromFormValue<'v> for Option<T> {
|
||||
type Error = Error;
|
||||
|
|
|
@ -14,9 +14,9 @@ impl<'a> FromParam<'a> for &'a str {
|
|||
}
|
||||
|
||||
macro_rules! impl_with_fromstr {
|
||||
($($T:ident),+) => (
|
||||
$(impl<'a> FromParam<'a> for $T {
|
||||
fn from_param(param: &'a str) -> Result<$T, Error> {
|
||||
($($T:ident),+) => ($(
|
||||
impl<'a> FromParam<'a> for $T {
|
||||
fn from_param(param: &'a str) -> Result<Self, Error> {
|
||||
$T::from_str(param).map_err(|_| Error::BadParse)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue