2016-03-17 08:57:04 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr};
|
2016-09-08 07:02:17 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use router::Segments;
|
|
|
|
|
2016-08-27 01:37:28 +00:00
|
|
|
use url;
|
2016-03-17 08:57:04 +00:00
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
|
|
|
|
pub trait FromParam<'a>: Sized {
|
|
|
|
fn from_param(param: &'a str) -> Result<Self, Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromParam<'a> for &'a str {
|
|
|
|
fn from_param(param: &'a str) -> Result<&'a str, Error> {
|
|
|
|
Ok(param)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-27 01:37:28 +00:00
|
|
|
impl<'a> FromParam<'a> for String {
|
|
|
|
fn from_param(p: &'a str) -> Result<String, Error> {
|
|
|
|
let decoder = url::percent_encoding::percent_decode(p.as_bytes());
|
|
|
|
decoder.decode_utf8().map_err(|_| Error::BadParse).map(|s| s.into_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 08:57:04 +00:00
|
|
|
macro_rules! impl_with_fromstr {
|
2016-04-04 11:19:07 +00:00
|
|
|
($($T:ident),+) => ($(
|
|
|
|
impl<'a> FromParam<'a> for $T {
|
|
|
|
fn from_param(param: &'a str) -> Result<Self, Error> {
|
2016-03-17 08:57:04 +00:00
|
|
|
$T::from_str(param).map_err(|_| Error::BadParse)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)+)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_with_fromstr!(f32, f64, isize, i8, i16, i32, i64, usize, u8, u16, u32, u64,
|
2016-08-27 01:37:28 +00:00
|
|
|
bool, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6,
|
2016-03-17 08:57:04 +00:00
|
|
|
SocketAddr);
|
2016-09-08 07:02:17 +00:00
|
|
|
|
|
|
|
pub trait FromSegments<'a>: Sized {
|
|
|
|
fn from_segments(segments: Segments<'a>) -> Result<Self, Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromSegments<'a> for Segments<'a> {
|
|
|
|
fn from_segments(segments: Segments<'a>) -> Result<Segments<'a>, Error> {
|
|
|
|
Ok(segments)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromSegments<'a> for PathBuf {
|
|
|
|
fn from_segments(segments: Segments<'a>) -> Result<PathBuf, Error> {
|
|
|
|
Ok(segments.collect())
|
|
|
|
}
|
|
|
|
}
|