http_with_url/
convert.rs

1use url::{Url, ParseError};
2
3use Error;
4use header::{HeaderName, HeaderValue};
5use method::Method;
6use sealed::Sealed;
7use status::StatusCode;
8
9/// Private trait for the `http` crate to have generic methods with fallible
10/// conversions.
11///
12/// This trait is similar to the `TryFrom` trait proposed in the standard
13/// library, except this is specialized for the `http` crate and isn't intended
14/// for general consumption.
15///
16/// This trait cannot be implemented types outside of the `http` crate, and is
17/// only intended for use as a generic bound on methods in the `http` crate.
18pub trait HttpTryFrom<T>: Sized + Sealed {
19    /// Associated error with the conversion this implementation represents.
20    type Error: Into<Error>;
21
22    #[doc(hidden)]
23    fn try_from(t: T) -> Result<Self, Self::Error>;
24}
25
26macro_rules! reflexive {
27    ($($t:ty,)*) => ($(
28        impl HttpTryFrom<$t> for $t {
29            type Error = Error;
30
31            fn try_from(t: Self) -> Result<Self, Self::Error> {
32                Ok(t)
33            }
34        }
35
36        impl Sealed for $t {}
37    )*)
38}
39
40reflexive! {
41    Url,
42    Method,
43    StatusCode,
44    HeaderName,
45    HeaderValue,
46}
47
48impl<'a> HttpTryFrom<&'a str> for Url {
49    type Error =  ParseError;
50
51    fn try_from(input: &str) -> Result<Url, ParseError> {
52        Url::parse(input)
53    }
54}