From db9b1b9a76ce9c0b366fc6fff433ca4ca00571d1 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 2 Dec 2019 10:54:22 -0800 Subject: [PATCH] remove error type variants suffixed with `Bytes` We reserved these extra error types with the goal of possibly returning the source `Bytes` in the error type, but no one has since need this. Since the dependency on `bytes` is being made private, it made sense to remove these otherwise unused error types. Removes: - `InvalidUriBytes` - `InvalidHeaderNameBytes` - `InvalidHeaderValueBytes` --- src/error.rs | 33 --------------------------------- src/header/mod.rs | 4 ++-- src/header/name.rs | 16 ---------------- src/header/value.rs | 27 ++++----------------------- src/uri/authority.rs | 10 +++++----- src/uri/mod.rs | 34 ++++++---------------------------- src/uri/path.rs | 6 +++--- 7 files changed, 20 insertions(+), 110 deletions(-) diff --git a/src/error.rs b/src/error.rs index 3d06fbc0..3b31a103 100644 --- a/src/error.rs +++ b/src/error.rs @@ -24,12 +24,9 @@ enum ErrorKind { StatusCode(status::InvalidStatusCode), Method(method::InvalidMethod), Uri(uri::InvalidUri), - UriShared(uri::InvalidUriBytes), UriParts(uri::InvalidUriParts), HeaderName(header::InvalidHeaderName), - HeaderNameShared(header::InvalidHeaderNameBytes), HeaderValue(header::InvalidHeaderValue), - HeaderValueShared(header::InvalidHeaderValueBytes), } impl fmt::Debug for Error { @@ -61,12 +58,9 @@ impl Error { StatusCode(ref e) => e, Method(ref e) => e, Uri(ref e) => e, - UriShared(ref e) => e, UriParts(ref e) => e, HeaderName(ref e) => e, - HeaderNameShared(ref e) => e, HeaderValue(ref e) => e, - HeaderValueShared(ref e) => e, } } } @@ -79,12 +73,9 @@ impl error::Error for Error { StatusCode(ref e) => e.description(), Method(ref e) => e.description(), Uri(ref e) => e.description(), - UriShared(ref e) => e.description(), UriParts(ref e) => e.description(), HeaderName(ref e) => e.description(), - HeaderNameShared(ref e) => e.description(), HeaderValue(ref e) => e.description(), - HeaderValueShared(ref e) => e.description(), } } @@ -119,14 +110,6 @@ impl From for Error { } } -impl From for Error { - fn from(err: uri::InvalidUriBytes) -> Error { - Error { - inner: ErrorKind::UriShared(err), - } - } -} - impl From for Error { fn from(err: uri::InvalidUriParts) -> Error { Error { @@ -143,14 +126,6 @@ impl From for Error { } } -impl From for Error { - fn from(err: header::InvalidHeaderNameBytes) -> Error { - Error { - inner: ErrorKind::HeaderNameShared(err), - } - } -} - impl From for Error { fn from(err: header::InvalidHeaderValue) -> Error { Error { @@ -159,14 +134,6 @@ impl From for Error { } } -impl From for Error { - fn from(err: header::InvalidHeaderValueBytes) -> Error { - Error { - inner: ErrorKind::HeaderValueShared(err), - } - } -} - impl From for Error { fn from(err: std::convert::Infallible) -> Error { match err {} diff --git a/src/header/mod.rs b/src/header/mod.rs index dedc95d7..2517eb06 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -78,8 +78,8 @@ pub use self::map::{ AsHeaderName, Drain, Entry, GetAll, HeaderMap, IntoHeaderName, IntoIter, Iter, IterMut, Keys, OccupiedEntry, VacantEntry, ValueDrain, ValueIter, ValueIterMut, Values, ValuesMut, }; -pub use self::name::{HeaderName, InvalidHeaderName, InvalidHeaderNameBytes}; -pub use self::value::{HeaderValue, InvalidHeaderValue, InvalidHeaderValueBytes, ToStrError}; +pub use self::name::{HeaderName, InvalidHeaderName}; +pub use self::value::{HeaderValue, InvalidHeaderValue, ToStrError}; // Use header name constants pub use self::name::{ diff --git a/src/header/name.rs b/src/header/name.rs index b9cfb106..8e871ecf 100644 --- a/src/header/name.rs +++ b/src/header/name.rs @@ -60,10 +60,6 @@ pub struct InvalidHeaderName { _priv: (), } -/// A possible error when converting a `HeaderName` from another type. -#[derive(Debug)] -pub struct InvalidHeaderNameBytes(InvalidHeaderName); - macro_rules! standard_headers { ( $( @@ -2017,18 +2013,6 @@ impl Error for InvalidHeaderName { } } -impl fmt::Display for InvalidHeaderNameBytes { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0.fmt(f) - } -} - -impl Error for InvalidHeaderNameBytes { - fn description(&self) -> &str { - self.0.description() - } -} - // ===== HdrName ===== impl<'a> HdrName<'a> { diff --git a/src/header/value.rs b/src/header/value.rs index 1a0276ed..4532525a 100644 --- a/src/header/value.rs +++ b/src/header/value.rs @@ -28,11 +28,6 @@ pub struct InvalidHeaderValue { _priv: (), } -/// A possible error when converting a `HeaderValue` from a string or byte -/// slice. -#[derive(Debug)] -pub struct InvalidHeaderValueBytes(InvalidHeaderValue); - /// A possible error when converting a `HeaderValue` to a string representation. /// /// Header field values may contain opaque bytes, in which case it is not @@ -161,8 +156,8 @@ impl HeaderValue { /// This function is intended to be replaced in the future by a `TryFrom` /// implementation once the trait is stabilized in std. #[inline] - fn from_shared(src: Bytes) -> Result { - HeaderValue::try_from_generic(src, std::convert::identity).map_err(InvalidHeaderValueBytes) + fn from_shared(src: Bytes) -> Result { + HeaderValue::try_from_generic(src, std::convert::identity) } /* @@ -176,8 +171,6 @@ impl HeaderValue { match HeaderValue::from_shared(src) { Ok(val) => val, Err(_err) => { - //TODO: if the Bytes were part of the InvalidHeaderValueBytes, - //this message could include the invalid bytes. panic!("HeaderValue::from_shared_unchecked() with invalid bytes"); } } @@ -511,7 +504,7 @@ impl<'a> TryFrom<&'a [u8]> for HeaderValue { } impl TryFrom for HeaderValue { - type Error = InvalidHeaderValueBytes; + type Error = InvalidHeaderValue; #[inline] fn try_from(t: String) -> Result { @@ -520,7 +513,7 @@ impl TryFrom for HeaderValue { } impl TryFrom> for HeaderValue { - type Error = InvalidHeaderValueBytes; + type Error = InvalidHeaderValue; #[inline] fn try_from(vec: Vec) -> Result { @@ -571,18 +564,6 @@ impl Error for InvalidHeaderValue { } } -impl fmt::Display for InvalidHeaderValueBytes { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0.fmt(f) - } -} - -impl Error for InvalidHeaderValueBytes { - fn description(&self) -> &str { - self.0.description() - } -} - impl fmt::Display for ToStrError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.description().fmt(f) diff --git a/src/uri/authority.rs b/src/uri/authority.rs index 9d33d626..b46527f6 100644 --- a/src/uri/authority.rs +++ b/src/uri/authority.rs @@ -5,7 +5,7 @@ use std::{cmp, fmt, str}; use bytes::Bytes; -use super::{ErrorKind, InvalidUri, InvalidUriBytes, Port, URI_CHARS}; +use super::{ErrorKind, InvalidUri, Port, URI_CHARS}; use crate::byte_str::ByteStr; /// Represents the authority component of a URI. @@ -41,8 +41,8 @@ impl Authority { /// assert_eq!(authority.host(), "example.com"); /// # } /// ``` - pub(super) fn from_shared(s: Bytes) -> Result { - let authority_end = Authority::parse_non_empty(&s[..]).map_err(InvalidUriBytes)?; + pub(super) fn from_shared(s: Bytes) -> Result { + let authority_end = Authority::parse_non_empty(&s[..])?; if authority_end != s.len() { return Err(ErrorKind::InvalidUriChar.into()); @@ -269,7 +269,7 @@ impl Authority { /* impl TryFrom for Authority { - type Error = InvalidUriBytes; + type Error = InvalidUri; /// Attempt to convert an `Authority` from `Bytes`. /// /// # Examples @@ -290,7 +290,7 @@ impl TryFrom for Authority { /// # } /// ``` fn try_from(s: Bytes) -> Result { - let authority_end = Authority::parse_non_empty(&s[..]).map_err(InvalidUriBytes)?; + let authority_end = Authority::parse_non_empty(&s[..])?; if authority_end != s.len() { return Err(ErrorKind::InvalidUriChar.into()); diff --git a/src/uri/mod.rs b/src/uri/mod.rs index 6d281d19..b47d6ff2 100644 --- a/src/uri/mod.rs +++ b/src/uri/mod.rs @@ -121,10 +121,6 @@ pub struct Parts { #[derive(Debug)] pub struct InvalidUri(ErrorKind); -/// An error resulting from a failed attempt to construct a URI. -#[derive(Debug)] -pub struct InvalidUriBytes(InvalidUri); - /// An error resulting from a failed attempt to construct a URI. #[derive(Debug)] pub struct InvalidUriParts(InvalidUri); @@ -260,7 +256,7 @@ impl Uri { /// assert_eq!(uri.path(), "/foo"); /// # } /// ``` - fn from_shared(s: Bytes) -> Result { + fn from_shared(s: Bytes) -> Result { use self::ErrorKind::*; if s.len() > MAX_LEN { @@ -697,7 +693,7 @@ impl<'a> TryFrom<&'a String> for Uri { } impl TryFrom for Uri { - type Error = InvalidUriBytes; + type Error = InvalidUri; #[inline] fn try_from(t: String) -> Result { @@ -785,9 +781,9 @@ impl From for Parts { } } -fn parse_full(mut s: Bytes) -> Result { +fn parse_full(mut s: Bytes) -> Result { // Parse the scheme - let scheme = match Scheme2::parse(&s[..]).map_err(InvalidUriBytes)? { + let scheme = match Scheme2::parse(&s[..])? { Scheme2::None => Scheme2::None, Scheme2::Standard(p) => { // TODO: use truncate @@ -810,7 +806,7 @@ fn parse_full(mut s: Bytes) -> Result { // Find the end of the authority. The scheme will already have been // extracted. - let authority_end = Authority::parse(&s[..]).map_err(InvalidUriBytes)?; + let authority_end = Authority::parse(&s[..])?; if scheme.is_none() { if authority_end != s.len() { @@ -850,7 +846,7 @@ impl FromStr for Uri { #[inline] fn from_str(s: &str) -> Result { - Uri::from_shared(Bytes::copy_from_slice(s.as_bytes())).map_err(|e| e.0) + Uri::from_shared(Bytes::copy_from_slice(s.as_bytes())) } } @@ -1019,12 +1015,6 @@ impl From for InvalidUri { } } -impl From for InvalidUriBytes { - fn from(src: ErrorKind) -> InvalidUriBytes { - InvalidUriBytes(src.into()) - } -} - impl From for InvalidUriParts { fn from(src: ErrorKind) -> InvalidUriParts { InvalidUriParts(src.into()) @@ -1055,24 +1045,12 @@ impl Error for InvalidUri { } } -impl fmt::Display for InvalidUriBytes { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0.fmt(f) - } -} - impl fmt::Display for InvalidUriParts { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } -impl Error for InvalidUriBytes { - fn description(&self) -> &str { - self.0.description() - } -} - impl Error for InvalidUriParts { fn description(&self) -> &str { self.0.description() diff --git a/src/uri/path.rs b/src/uri/path.rs index dfefc113..973c7dca 100644 --- a/src/uri/path.rs +++ b/src/uri/path.rs @@ -4,7 +4,7 @@ use std::{cmp, fmt, str}; use bytes::Bytes; -use super::{ErrorKind, InvalidUri, InvalidUriBytes}; +use super::{ErrorKind, InvalidUri}; use crate::byte_str::ByteStr; /// Represents the path component of a URI @@ -39,7 +39,7 @@ impl PathAndQuery { /// assert_eq!(path_and_query.query(), Some("world")); /// # } /// ``` - pub(super) fn from_shared(mut src: Bytes) -> Result { + pub(super) fn from_shared(mut src: Bytes) -> Result { let mut query = NONE; let mut fragment = None; @@ -281,7 +281,7 @@ impl<'a> TryFrom<&'a [u8]> for PathAndQuery { type Error = InvalidUri; #[inline] fn try_from(s: &'a [u8]) -> Result { - PathAndQuery::from_shared(Bytes::copy_from_slice(s)).map_err(|e| e.0) + PathAndQuery::from_shared(Bytes::copy_from_slice(s)) } }