mirror of
https://github.com/openharmony/third_party_rust_http.git
synced 2026-07-18 20:54:29 -04:00
Switch HttpTryFrom to TryFrom.
This commit is contained in:
committed by
Sean McArthur
parent
23fb5401fa
commit
a23d8edcfb
@@ -1,75 +0,0 @@
|
||||
use crate::header::{HeaderMap, HeaderName, HeaderValue};
|
||||
use crate::method::Method;
|
||||
use crate::sealed::Sealed;
|
||||
use crate::status::StatusCode;
|
||||
use crate::uri::{Authority, PathAndQuery, Scheme, Uri};
|
||||
use crate::Error;
|
||||
|
||||
/// Private trait for the `http` crate to have generic methods with fallible
|
||||
/// conversions.
|
||||
///
|
||||
/// This trait is similar to the `TryFrom` trait proposed in the standard
|
||||
/// library, except this is specialized for the `http` crate and isn't intended
|
||||
/// for general consumption.
|
||||
///
|
||||
/// This trait cannot be implemented types outside of the `http` crate, and is
|
||||
/// only intended for use as a generic bound on methods in the `http` crate.
|
||||
pub trait HttpTryFrom<T>: Sized + Sealed {
|
||||
/// Associated error with the conversion this implementation represents.
|
||||
type Error: Into<Error>;
|
||||
|
||||
#[doc(hidden)]
|
||||
fn try_from(t: T) -> Result<Self, Self::Error>;
|
||||
}
|
||||
|
||||
pub(crate) trait HttpTryInto<T>: Sized {
|
||||
fn http_try_into(self) -> Result<T, Error>;
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
impl<T, U> HttpTryInto<U> for T
|
||||
where
|
||||
U: HttpTryFrom<T>,
|
||||
T: Sized,
|
||||
{
|
||||
fn http_try_into(self) -> Result<U, Error> {
|
||||
HttpTryFrom::try_from(self).map_err(|e: U::Error| e.into())
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! reflexive {
|
||||
($($t:ty,)*) => ($(
|
||||
impl HttpTryFrom<$t> for $t {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(t: Self) -> Result<Self, Self::Error> {
|
||||
Ok(t)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sealed for $t {}
|
||||
)*)
|
||||
}
|
||||
|
||||
reflexive! {
|
||||
Uri,
|
||||
Method,
|
||||
StatusCode,
|
||||
HeaderName,
|
||||
HeaderValue,
|
||||
Scheme,
|
||||
Authority,
|
||||
PathAndQuery,
|
||||
}
|
||||
|
||||
// HeaderMap<T> can't use reflexive easily due to the generic T
|
||||
|
||||
impl<T> HttpTryFrom<HeaderMap<T>> for HeaderMap<T> {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(t: Self) -> Result<Self, Self::Error> {
|
||||
Ok(t)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Sealed for HeaderMap<T> {}
|
||||
+3
-27
@@ -167,33 +167,9 @@ impl From<header::InvalidHeaderValueBytes> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
// A crate-private type until we can use !.
|
||||
//
|
||||
// Being crate-private, we should be able to swap the type out in a
|
||||
// backwards compatible way.
|
||||
pub enum Never {}
|
||||
|
||||
impl From<Never> for Error {
|
||||
fn from(never: Never) -> Error {
|
||||
match never {}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Never {
|
||||
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Never {
|
||||
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for Never {
|
||||
fn description(&self) -> &str {
|
||||
match *self {}
|
||||
impl From<std::convert::Infallible> for Error {
|
||||
fn from(err: std::convert::Infallible) -> Error {
|
||||
match err {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-9
@@ -1,11 +1,11 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hash_map::RandomState;
|
||||
use std::convert::TryFrom;
|
||||
use std::hash::{BuildHasher, Hash, Hasher};
|
||||
use std::iter::{FromIterator, FusedIterator};
|
||||
use std::marker::PhantomData;
|
||||
use std::{fmt, mem, ops, ptr, vec};
|
||||
|
||||
use crate::convert::{HttpTryFrom, HttpTryInto};
|
||||
use crate::Error;
|
||||
|
||||
use super::HeaderValue;
|
||||
@@ -1815,27 +1815,30 @@ impl<T> FromIterator<(HeaderName, T)> for HeaderMap<T> {
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashMap;
|
||||
/// use http::{HttpTryFrom, header::HeaderMap};
|
||||
/// use std::convert::TryInto;
|
||||
/// use http::HeaderMap;
|
||||
///
|
||||
/// let mut map = HashMap::new();
|
||||
/// map.insert("X-Custom-Header".to_string(), "my value".to_string());
|
||||
///
|
||||
/// let headers: HeaderMap = HttpTryFrom::try_from(&map).expect("valid headers");
|
||||
/// let headers: HeaderMap = (&map).try_into().expect("valid headers");
|
||||
/// assert_eq!(headers["X-Custom-Header"], "my value");
|
||||
/// ```
|
||||
impl<'a, K, V, T> HttpTryFrom<&'a HashMap<K, V>> for HeaderMap<T>
|
||||
impl<'a, K, V, T> TryFrom<&'a HashMap<K, V>> for HeaderMap<T>
|
||||
where
|
||||
K: Eq + Hash,
|
||||
HeaderName: HttpTryFrom<&'a K>,
|
||||
T: HttpTryFrom<&'a V>
|
||||
HeaderName: TryFrom<&'a K>,
|
||||
<HeaderName as TryFrom<&'a K>>::Error: Into<crate::Error>,
|
||||
T: TryFrom<&'a V>,
|
||||
T::Error: Into<crate::Error>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(c: &'a HashMap<K, V>) -> Result<Self, Self::Error> {
|
||||
c.into_iter()
|
||||
.map(|(k, v)| {
|
||||
let name = k.http_try_into()?;
|
||||
let value = v.http_try_into()?;
|
||||
.map(|(k, v)| -> crate::Result<(HeaderName, T)> {
|
||||
let name = TryFrom::try_from(k).map_err(Into::into)?;
|
||||
let value = TryFrom::try_from(v).map_err(Into::into)?;
|
||||
Ok((name, value))
|
||||
})
|
||||
.collect()
|
||||
|
||||
+5
-14
@@ -1,9 +1,9 @@
|
||||
use crate::byte_str::ByteStr;
|
||||
use crate::HttpTryFrom;
|
||||
use bytes::{Bytes, BytesMut};
|
||||
|
||||
use std::borrow::Borrow;
|
||||
use std::error::Error;
|
||||
use std::convert::{TryFrom};
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::str::FromStr;
|
||||
use std::{fmt, mem};
|
||||
@@ -1888,16 +1888,7 @@ impl From<HeaderName> for Bytes {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a HeaderName> for HeaderName {
|
||||
type Error = crate::error::Never;
|
||||
|
||||
#[inline]
|
||||
fn try_from(t: &'a HeaderName) -> Result<Self, Self::Error> {
|
||||
Ok(t.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a str> for HeaderName {
|
||||
impl<'a> TryFrom<&'a str> for HeaderName {
|
||||
type Error = InvalidHeaderName;
|
||||
#[inline]
|
||||
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
|
||||
@@ -1905,7 +1896,7 @@ impl<'a> HttpTryFrom<&'a str> for HeaderName {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a String> for HeaderName {
|
||||
impl<'a> TryFrom<&'a String> for HeaderName {
|
||||
type Error = InvalidHeaderName;
|
||||
#[inline]
|
||||
fn try_from(s: &'a String) -> Result<Self, Self::Error> {
|
||||
@@ -1913,7 +1904,7 @@ impl<'a> HttpTryFrom<&'a String> for HeaderName {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a [u8]> for HeaderName {
|
||||
impl<'a> TryFrom<&'a [u8]> for HeaderName {
|
||||
type Error = InvalidHeaderName;
|
||||
#[inline]
|
||||
fn try_from(s: &'a [u8]) -> Result<Self, Self::Error> {
|
||||
@@ -1921,7 +1912,7 @@ impl<'a> HttpTryFrom<&'a [u8]> for HeaderName {
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpTryFrom<Bytes> for HeaderName {
|
||||
impl TryFrom<Bytes> for HeaderName {
|
||||
type Error = InvalidHeaderNameBytes;
|
||||
#[inline]
|
||||
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
|
||||
|
||||
+6
-35
@@ -1,11 +1,10 @@
|
||||
use bytes::{Bytes, BytesMut};
|
||||
|
||||
use std::convert::TryFrom;
|
||||
use std::error::Error;
|
||||
use std::str::FromStr;
|
||||
use std::{cmp, fmt, mem, str};
|
||||
|
||||
use crate::convert::HttpTryFrom;
|
||||
use crate::error::Never;
|
||||
use crate::header::name::HeaderName;
|
||||
|
||||
/// Represents an HTTP header field value.
|
||||
@@ -397,15 +396,6 @@ macro_rules! from_integers {
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpTryFrom<$t> for HeaderValue {
|
||||
type Error = Never;
|
||||
|
||||
#[inline]
|
||||
fn try_from(num: $t) -> Result<Self, Self::Error> {
|
||||
Ok(num.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn $name() {
|
||||
let n: $t = 55;
|
||||
@@ -499,16 +489,7 @@ impl<'a> From<&'a HeaderValue> for HeaderValue {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a HeaderValue> for HeaderValue {
|
||||
type Error = crate::error::Never;
|
||||
|
||||
#[inline]
|
||||
fn try_from(t: &'a HeaderValue) -> Result<Self, Self::Error> {
|
||||
Ok(t.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a str> for HeaderValue {
|
||||
impl<'a> TryFrom<&'a str> for HeaderValue {
|
||||
type Error = InvalidHeaderValue;
|
||||
|
||||
#[inline]
|
||||
@@ -517,7 +498,7 @@ impl<'a> HttpTryFrom<&'a str> for HeaderValue {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a String> for HeaderValue {
|
||||
impl<'a> TryFrom<&'a String> for HeaderValue {
|
||||
type Error = InvalidHeaderValue;
|
||||
#[inline]
|
||||
fn try_from(s: &'a String) -> Result<Self, Self::Error> {
|
||||
@@ -525,7 +506,7 @@ impl<'a> HttpTryFrom<&'a String> for HeaderValue {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a [u8]> for HeaderValue {
|
||||
impl<'a> TryFrom<&'a [u8]> for HeaderValue {
|
||||
type Error = InvalidHeaderValue;
|
||||
|
||||
#[inline]
|
||||
@@ -534,7 +515,7 @@ impl<'a> HttpTryFrom<&'a [u8]> for HeaderValue {
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpTryFrom<String> for HeaderValue {
|
||||
impl TryFrom<String> for HeaderValue {
|
||||
type Error = InvalidHeaderValueBytes;
|
||||
|
||||
#[inline]
|
||||
@@ -543,7 +524,7 @@ impl HttpTryFrom<String> for HeaderValue {
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpTryFrom<Bytes> for HeaderValue {
|
||||
impl TryFrom<Bytes> for HeaderValue {
|
||||
type Error = InvalidHeaderValueBytes;
|
||||
|
||||
#[inline]
|
||||
@@ -552,16 +533,6 @@ impl HttpTryFrom<Bytes> for HeaderValue {
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpTryFrom<HeaderName> for HeaderValue {
|
||||
type Error = InvalidHeaderValue;
|
||||
|
||||
#[inline]
|
||||
fn try_from(name: HeaderName) -> Result<Self, Self::Error> {
|
||||
// Infallable as header names have the same validations
|
||||
Ok(name.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod try_from_header_name_tests {
|
||||
use super::*;
|
||||
|
||||
@@ -176,11 +176,9 @@ pub mod uri;
|
||||
pub mod version;
|
||||
|
||||
mod byte_str;
|
||||
mod convert;
|
||||
mod error;
|
||||
mod extensions;
|
||||
|
||||
pub use crate::convert::HttpTryFrom;
|
||||
pub use crate::error::{Error, Result};
|
||||
pub use crate::extensions::Extensions;
|
||||
#[doc(no_inline)]
|
||||
|
||||
+5
-14
@@ -16,11 +16,11 @@
|
||||
//! ```
|
||||
|
||||
use self::Inner::*;
|
||||
use crate::HttpTryFrom;
|
||||
|
||||
use std::convert::AsRef;
|
||||
use std::error::Error;
|
||||
use std::str::FromStr;
|
||||
use std::convert::TryFrom;
|
||||
use std::{fmt, str};
|
||||
|
||||
/// The Request Method (VERB)
|
||||
@@ -322,16 +322,7 @@ impl<'a> From<&'a Method> for Method {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a Method> for Method {
|
||||
type Error = crate::error::Never;
|
||||
|
||||
#[inline]
|
||||
fn try_from(t: &'a Method) -> Result<Self, Self::Error> {
|
||||
Ok(t.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a [u8]> for Method {
|
||||
impl<'a> TryFrom<&'a [u8]> for Method {
|
||||
type Error = InvalidMethod;
|
||||
|
||||
#[inline]
|
||||
@@ -340,12 +331,12 @@ impl<'a> HttpTryFrom<&'a [u8]> for Method {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a str> for Method {
|
||||
impl<'a> TryFrom<&'a str> for Method {
|
||||
type Error = InvalidMethod;
|
||||
|
||||
#[inline]
|
||||
fn try_from(t: &'a str) -> Result<Self, Self::Error> {
|
||||
HttpTryFrom::try_from(t.as_bytes())
|
||||
TryFrom::try_from(t.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,7 +345,7 @@ impl FromStr for Method {
|
||||
|
||||
#[inline]
|
||||
fn from_str(t: &str) -> Result<Self, Self::Err> {
|
||||
HttpTryFrom::try_from(t)
|
||||
TryFrom::try_from(t)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+40
-18
@@ -53,12 +53,13 @@
|
||||
//! ```
|
||||
|
||||
use std::any::Any;
|
||||
use std::convert::{TryFrom};
|
||||
use std::fmt;
|
||||
|
||||
use crate::header::{HeaderMap, HeaderName, HeaderValue};
|
||||
use crate::method::Method;
|
||||
use crate::version::Version;
|
||||
use crate::{Extensions, HttpTryFrom, Result, Uri};
|
||||
use crate::{Extensions, Result, Uri};
|
||||
|
||||
/// Represents an HTTP request.
|
||||
///
|
||||
@@ -228,7 +229,9 @@ impl Request<()> {
|
||||
/// ```
|
||||
pub fn get<T>(uri: T) -> Builder
|
||||
where
|
||||
Uri: HttpTryFrom<T>,
|
||||
Uri: TryFrom<T>,
|
||||
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
|
||||
{
|
||||
Builder::new().method(Method::GET).uri(uri)
|
||||
}
|
||||
@@ -249,7 +252,9 @@ impl Request<()> {
|
||||
/// ```
|
||||
pub fn put<T>(uri: T) -> Builder
|
||||
where
|
||||
Uri: HttpTryFrom<T>,
|
||||
Uri: TryFrom<T>,
|
||||
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
|
||||
{
|
||||
Builder::new().method(Method::PUT).uri(uri)
|
||||
}
|
||||
@@ -270,7 +275,9 @@ impl Request<()> {
|
||||
/// ```
|
||||
pub fn post<T>(uri: T) -> Builder
|
||||
where
|
||||
Uri: HttpTryFrom<T>,
|
||||
Uri: TryFrom<T>,
|
||||
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
|
||||
{
|
||||
Builder::new().method(Method::POST).uri(uri)
|
||||
}
|
||||
@@ -291,7 +298,9 @@ impl Request<()> {
|
||||
/// ```
|
||||
pub fn delete<T>(uri: T) -> Builder
|
||||
where
|
||||
Uri: HttpTryFrom<T>,
|
||||
Uri: TryFrom<T>,
|
||||
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
|
||||
{
|
||||
Builder::new().method(Method::DELETE).uri(uri)
|
||||
}
|
||||
@@ -313,7 +322,9 @@ impl Request<()> {
|
||||
/// ```
|
||||
pub fn options<T>(uri: T) -> Builder
|
||||
where
|
||||
Uri: HttpTryFrom<T>,
|
||||
Uri: TryFrom<T>,
|
||||
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
|
||||
{
|
||||
Builder::new().method(Method::OPTIONS).uri(uri)
|
||||
}
|
||||
@@ -334,7 +345,9 @@ impl Request<()> {
|
||||
/// ```
|
||||
pub fn head<T>(uri: T) -> Builder
|
||||
where
|
||||
Uri: HttpTryFrom<T>,
|
||||
Uri: TryFrom<T>,
|
||||
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
|
||||
{
|
||||
Builder::new().method(Method::HEAD).uri(uri)
|
||||
}
|
||||
@@ -355,7 +368,9 @@ impl Request<()> {
|
||||
/// ```
|
||||
pub fn connect<T>(uri: T) -> Builder
|
||||
where
|
||||
Uri: HttpTryFrom<T>,
|
||||
Uri: TryFrom<T>,
|
||||
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
|
||||
{
|
||||
Builder::new().method(Method::CONNECT).uri(uri)
|
||||
}
|
||||
@@ -376,7 +391,8 @@ impl Request<()> {
|
||||
/// ```
|
||||
pub fn patch<T>(uri: T) -> Builder
|
||||
where
|
||||
Uri: HttpTryFrom<T>,
|
||||
Uri: TryFrom<T>,
|
||||
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
{
|
||||
Builder::new().method(Method::PATCH).uri(uri)
|
||||
}
|
||||
@@ -397,7 +413,8 @@ impl Request<()> {
|
||||
/// ```
|
||||
pub fn trace<T>(uri: T) -> Builder
|
||||
where
|
||||
Uri: HttpTryFrom<T>,
|
||||
Uri: TryFrom<T>,
|
||||
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
{
|
||||
Builder::new().method(Method::TRACE).uri(uri)
|
||||
}
|
||||
@@ -761,10 +778,12 @@ impl Builder {
|
||||
/// ```
|
||||
pub fn method<T>(self, method: T) -> Builder
|
||||
where
|
||||
Method: HttpTryFrom<T>,
|
||||
Method: TryFrom<T>,
|
||||
<Method as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
{
|
||||
self.and_then(move |mut head| {
|
||||
head.method = HttpTryFrom::try_from(method).map_err(Into::into)?;
|
||||
let method = TryFrom::try_from(method).map_err(Into::into)?;
|
||||
head.method = method;
|
||||
Ok(head)
|
||||
})
|
||||
}
|
||||
@@ -807,10 +826,11 @@ impl Builder {
|
||||
/// ```
|
||||
pub fn uri<T>(self, uri: T) -> Builder
|
||||
where
|
||||
Uri: HttpTryFrom<T>,
|
||||
Uri: TryFrom<T>,
|
||||
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
{
|
||||
self.and_then(move |mut head| {
|
||||
head.uri = HttpTryFrom::try_from(uri).map_err(Into::into)?;
|
||||
head.uri = TryFrom::try_from(uri).map_err(Into::into)?;
|
||||
Ok(head)
|
||||
})
|
||||
}
|
||||
@@ -878,12 +898,14 @@ impl Builder {
|
||||
/// ```
|
||||
pub fn header<K, V>(self, key: K, value: V) -> Builder
|
||||
where
|
||||
HeaderName: HttpTryFrom<K>,
|
||||
HeaderValue: HttpTryFrom<V>,
|
||||
HeaderName: TryFrom<K>,
|
||||
<HeaderName as TryFrom<K>>::Error: Into<crate::Error>,
|
||||
HeaderValue: TryFrom<V>,
|
||||
<HeaderValue as TryFrom<V>>::Error: Into<crate::Error>,
|
||||
{
|
||||
self.and_then(move |mut head| {
|
||||
let name = <HeaderName as HttpTryFrom<K>>::try_from(key).map_err(Into::into)?;
|
||||
let value = <HeaderValue as HttpTryFrom<V>>::try_from(value).map_err(Into::into)?;
|
||||
let name = <HeaderName as TryFrom<K>>::try_from(key).map_err(Into::into)?;
|
||||
let value = <HeaderValue as TryFrom<V>>::try_from(value).map_err(Into::into)?;
|
||||
head.headers.append(name, value);
|
||||
Ok(head)
|
||||
})
|
||||
|
||||
+11
-7
@@ -62,12 +62,13 @@
|
||||
//! ```
|
||||
|
||||
use std::any::Any;
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
|
||||
use crate::header::{HeaderMap, HeaderName, HeaderValue};
|
||||
use crate::status::StatusCode;
|
||||
use crate::version::Version;
|
||||
use crate::{Extensions, HttpTryFrom, Result};
|
||||
use crate::{Extensions, Result};
|
||||
|
||||
/// Represents an HTTP response
|
||||
///
|
||||
@@ -562,10 +563,11 @@ impl Builder {
|
||||
/// ```
|
||||
pub fn status<T>(self, status: T) -> Builder
|
||||
where
|
||||
StatusCode: HttpTryFrom<T>,
|
||||
StatusCode: TryFrom<T>,
|
||||
<StatusCode as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
{
|
||||
self.and_then(move |mut head| {
|
||||
head.status = HttpTryFrom::try_from(status).map_err(Into::into)?;
|
||||
head.status = TryFrom::try_from(status).map_err(Into::into)?;
|
||||
Ok(head)
|
||||
})
|
||||
}
|
||||
@@ -615,12 +617,14 @@ impl Builder {
|
||||
/// ```
|
||||
pub fn header<K, V>(self, key: K, value: V) -> Builder
|
||||
where
|
||||
HeaderName: HttpTryFrom<K>,
|
||||
HeaderValue: HttpTryFrom<V>,
|
||||
HeaderName: TryFrom<K>,
|
||||
<HeaderName as TryFrom<K>>::Error: Into<crate::Error>,
|
||||
HeaderValue: TryFrom<V>,
|
||||
<HeaderValue as TryFrom<V>>::Error: Into<crate::Error>,
|
||||
{
|
||||
self.and_then(move |mut head| {
|
||||
let name = <HeaderName as HttpTryFrom<K>>::try_from(key).map_err(Into::into)?;
|
||||
let value = <HeaderValue as HttpTryFrom<V>>::try_from(value).map_err(Into::into)?;
|
||||
let name = <HeaderName as TryFrom<K>>::try_from(key).map_err(Into::into)?;
|
||||
let value = <HeaderValue as TryFrom<V>>::try_from(value).map_err(Into::into)?;
|
||||
head.headers.append(name, value);
|
||||
Ok(head)
|
||||
})
|
||||
|
||||
+4
-14
@@ -14,12 +14,11 @@
|
||||
//! assert!(StatusCode::OK.is_success());
|
||||
//! ```
|
||||
|
||||
use std::convert::TryFrom;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::HttpTryFrom;
|
||||
|
||||
/// An HTTP status code (`status-code` in RFC 7230 et al.).
|
||||
///
|
||||
/// This type contains constants for all common status codes.
|
||||
@@ -251,16 +250,7 @@ impl<'a> From<&'a StatusCode> for StatusCode {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a StatusCode> for StatusCode {
|
||||
type Error = crate::error::Never;
|
||||
|
||||
#[inline]
|
||||
fn try_from(t: &'a StatusCode) -> Result<Self, Self::Error> {
|
||||
Ok(t.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a [u8]> for StatusCode {
|
||||
impl<'a> TryFrom<&'a [u8]> for StatusCode {
|
||||
type Error = InvalidStatusCode;
|
||||
|
||||
#[inline]
|
||||
@@ -269,7 +259,7 @@ impl<'a> HttpTryFrom<&'a [u8]> for StatusCode {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a str> for StatusCode {
|
||||
impl<'a> TryFrom<&'a str> for StatusCode {
|
||||
type Error = InvalidStatusCode;
|
||||
|
||||
#[inline]
|
||||
@@ -278,7 +268,7 @@ impl<'a> HttpTryFrom<&'a str> for StatusCode {
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpTryFrom<u16> for StatusCode {
|
||||
impl TryFrom<u16> for StatusCode {
|
||||
type Error = InvalidStatusCode;
|
||||
|
||||
#[inline]
|
||||
|
||||
+4
-13
@@ -10,7 +10,6 @@ use bytes::Bytes;
|
||||
|
||||
use super::{ErrorKind, InvalidUri, InvalidUriBytes, Port, URI_CHARS};
|
||||
use crate::byte_str::ByteStr;
|
||||
use crate::convert::HttpTryFrom;
|
||||
|
||||
/// Represents the authority component of a URI.
|
||||
#[derive(Clone)]
|
||||
@@ -463,15 +462,7 @@ impl Hash for Authority {
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpTryFrom<Bytes> for Authority {
|
||||
type Error = InvalidUriBytes;
|
||||
#[inline]
|
||||
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
|
||||
Authority::from_shared(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a [u8]> for Authority {
|
||||
impl<'a> TryFrom<&'a [u8]> for Authority {
|
||||
type Error = InvalidUri;
|
||||
#[inline]
|
||||
fn try_from(s: &'a [u8]) -> Result<Self, Self::Error> {
|
||||
@@ -488,11 +479,11 @@ impl<'a> HttpTryFrom<&'a [u8]> for Authority {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a str> for Authority {
|
||||
impl<'a> TryFrom<&'a str> for Authority {
|
||||
type Error = InvalidUri;
|
||||
#[inline]
|
||||
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
|
||||
HttpTryFrom::try_from(s.as_bytes())
|
||||
TryFrom::try_from(s.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -500,7 +491,7 @@ impl FromStr for Authority {
|
||||
type Err = InvalidUri;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, InvalidUri> {
|
||||
HttpTryFrom::try_from(s)
|
||||
TryFrom::try_from(s)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+20
-14
@@ -1,6 +1,7 @@
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
|
||||
use super::{Authority, Parts, PathAndQuery, Scheme};
|
||||
use crate::convert::{HttpTryFrom, HttpTryInto};
|
||||
use crate::{Result, Uri};
|
||||
use crate::Uri;
|
||||
|
||||
/// A builder for `Uri`s.
|
||||
///
|
||||
@@ -8,7 +9,7 @@ use crate::{Result, Uri};
|
||||
/// through a builder pattern.
|
||||
#[derive(Debug)]
|
||||
pub struct Builder {
|
||||
parts: Result<Parts>,
|
||||
parts: Result<Parts, crate::Error>,
|
||||
}
|
||||
|
||||
impl Builder {
|
||||
@@ -43,10 +44,12 @@ impl Builder {
|
||||
/// ```
|
||||
pub fn scheme<T>(self, scheme: T) -> Self
|
||||
where
|
||||
Scheme: HttpTryFrom<T>,
|
||||
Scheme: TryFrom<T>,
|
||||
<Scheme as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
{
|
||||
self.map(move |mut parts| {
|
||||
parts.scheme = Some(scheme.http_try_into()?);
|
||||
let scheme = scheme.try_into().map_err(Into::into)?;
|
||||
parts.scheme = Some(scheme);
|
||||
Ok(parts)
|
||||
})
|
||||
}
|
||||
@@ -65,10 +68,12 @@ impl Builder {
|
||||
/// ```
|
||||
pub fn authority<T>(self, auth: T) -> Self
|
||||
where
|
||||
Authority: HttpTryFrom<T>,
|
||||
Authority: TryFrom<T>,
|
||||
<Authority as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
{
|
||||
self.map(move |mut parts| {
|
||||
parts.authority = Some(auth.http_try_into()?);
|
||||
let auth = auth.try_into().map_err(Into::into)?;
|
||||
parts.authority = Some(auth);
|
||||
Ok(parts)
|
||||
})
|
||||
}
|
||||
@@ -87,10 +92,12 @@ impl Builder {
|
||||
/// ```
|
||||
pub fn path_and_query<T>(self, p_and_q: T) -> Self
|
||||
where
|
||||
PathAndQuery: HttpTryFrom<T>,
|
||||
PathAndQuery: TryFrom<T>,
|
||||
<PathAndQuery as TryFrom<T>>::Error: Into<crate::Error>,
|
||||
{
|
||||
self.map(move |mut parts| {
|
||||
parts.path_and_query = Some(p_and_q.http_try_into()?);
|
||||
let p_and_q = p_and_q.try_into().map_err(Into::into)?;
|
||||
parts.path_and_query = Some(p_and_q);
|
||||
Ok(parts)
|
||||
})
|
||||
}
|
||||
@@ -119,17 +126,16 @@ impl Builder {
|
||||
/// .build()
|
||||
/// .unwrap();
|
||||
/// ```
|
||||
pub fn build(self) -> Result<Uri> {
|
||||
self
|
||||
.parts
|
||||
.and_then(|parts| parts.http_try_into())
|
||||
pub fn build(self) -> Result<Uri, crate::Error> {
|
||||
let parts = self.parts?;
|
||||
Uri::from_parts(parts).map_err(Into::into)
|
||||
}
|
||||
|
||||
// private
|
||||
|
||||
fn map<F>(self, func: F) -> Self
|
||||
where
|
||||
F: FnOnce(Parts) -> Result<Parts>,
|
||||
F: FnOnce(Parts) -> Result<Parts, crate::Error>,
|
||||
{
|
||||
|
||||
Builder {
|
||||
|
||||
+5
-15
@@ -23,7 +23,6 @@
|
||||
//! ```
|
||||
|
||||
use crate::byte_str::ByteStr;
|
||||
use crate::HttpTryFrom;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use bytes::Bytes;
|
||||
@@ -707,7 +706,7 @@ impl TryFrom<Bytes> for Uri {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a str> for Uri {
|
||||
impl<'a> TryFrom<&'a str> for Uri {
|
||||
type Error = InvalidUri;
|
||||
|
||||
#[inline]
|
||||
@@ -716,7 +715,7 @@ impl<'a> HttpTryFrom<&'a str> for Uri {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a String> for Uri {
|
||||
impl<'a> TryFrom<&'a String> for Uri {
|
||||
type Error = InvalidUri;
|
||||
|
||||
#[inline]
|
||||
@@ -725,7 +724,7 @@ impl<'a> HttpTryFrom<&'a String> for Uri {
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpTryFrom<String> for Uri {
|
||||
impl TryFrom<String> for Uri {
|
||||
type Error = InvalidUriBytes;
|
||||
|
||||
#[inline]
|
||||
@@ -734,16 +733,7 @@ impl HttpTryFrom<String> for Uri {
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpTryFrom<Bytes> for Uri {
|
||||
type Error = InvalidUriBytes;
|
||||
|
||||
#[inline]
|
||||
fn try_from(t: Bytes) -> Result<Self, Self::Error> {
|
||||
Uri::from_shared(t)
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpTryFrom<Parts> for Uri {
|
||||
impl TryFrom<Parts> for Uri {
|
||||
type Error = InvalidUriParts;
|
||||
|
||||
#[inline]
|
||||
@@ -752,7 +742,7 @@ impl HttpTryFrom<Parts> for Uri {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a Uri> for Uri {
|
||||
impl<'a> TryFrom<&'a Uri> for Uri {
|
||||
type Error = crate::Error;
|
||||
|
||||
#[inline]
|
||||
|
||||
+6
-6
@@ -1,3 +1,4 @@
|
||||
use std::convert::TryFrom;
|
||||
use std::str::FromStr;
|
||||
use std::{cmp, fmt, str};
|
||||
|
||||
@@ -5,7 +6,6 @@ use bytes::Bytes;
|
||||
|
||||
use super::{ErrorKind, InvalidUri, InvalidUriBytes};
|
||||
use crate::byte_str::ByteStr;
|
||||
use crate::convert::HttpTryFrom;
|
||||
|
||||
/// Represents the path component of a URI
|
||||
#[derive(Clone)]
|
||||
@@ -283,7 +283,7 @@ impl PathAndQuery {
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpTryFrom<Bytes> for PathAndQuery {
|
||||
impl TryFrom<Bytes> for PathAndQuery {
|
||||
type Error = InvalidUriBytes;
|
||||
#[inline]
|
||||
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
|
||||
@@ -291,7 +291,7 @@ impl HttpTryFrom<Bytes> for PathAndQuery {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a [u8]> for PathAndQuery {
|
||||
impl<'a> TryFrom<&'a [u8]> for PathAndQuery {
|
||||
type Error = InvalidUri;
|
||||
#[inline]
|
||||
fn try_from(s: &'a [u8]) -> Result<Self, Self::Error> {
|
||||
@@ -299,11 +299,11 @@ impl<'a> HttpTryFrom<&'a [u8]> for PathAndQuery {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a str> for PathAndQuery {
|
||||
impl<'a> TryFrom<&'a str> for PathAndQuery {
|
||||
type Error = InvalidUri;
|
||||
#[inline]
|
||||
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
|
||||
HttpTryFrom::try_from(s.as_bytes())
|
||||
TryFrom::try_from(s.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,7 +311,7 @@ impl FromStr for PathAndQuery {
|
||||
type Err = InvalidUri;
|
||||
#[inline]
|
||||
fn from_str(s: &str) -> Result<Self, InvalidUri> {
|
||||
HttpTryFrom::try_from(s)
|
||||
TryFrom::try_from(s)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-13
@@ -10,7 +10,6 @@ use bytes::Bytes;
|
||||
|
||||
use super::{ErrorKind, InvalidUri, InvalidUriBytes};
|
||||
use crate::byte_str::ByteStr;
|
||||
use crate::convert::HttpTryFrom;
|
||||
|
||||
/// Represents the scheme component of a URI
|
||||
#[derive(Clone)]
|
||||
@@ -101,14 +100,6 @@ impl Scheme {
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpTryFrom<Bytes> for Scheme {
|
||||
type Error = InvalidUriBytes;
|
||||
#[inline]
|
||||
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
|
||||
Scheme::from_shared(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Bytes> for Scheme {
|
||||
type Error = InvalidUriBytes;
|
||||
|
||||
@@ -145,7 +136,7 @@ impl TryFrom<Bytes> for Scheme {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a [u8]> for Scheme {
|
||||
impl<'a> TryFrom<&'a [u8]> for Scheme {
|
||||
type Error = InvalidUri;
|
||||
#[inline]
|
||||
fn try_from(s: &'a [u8]) -> Result<Self, Self::Error> {
|
||||
@@ -162,11 +153,11 @@ impl<'a> HttpTryFrom<&'a [u8]> for Scheme {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HttpTryFrom<&'a str> for Scheme {
|
||||
impl<'a> TryFrom<&'a str> for Scheme {
|
||||
type Error = InvalidUri;
|
||||
#[inline]
|
||||
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
|
||||
HttpTryFrom::try_from(s.as_bytes())
|
||||
TryFrom::try_from(s.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,7 +165,7 @@ impl FromStr for Scheme {
|
||||
type Err = InvalidUri;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
HttpTryFrom::try_from(s)
|
||||
TryFrom::try_from(s)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user