Skip to content

Commit

Permalink
remove error type variants suffixed with Bytes
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
seanmonstar committed Dec 2, 2019
1 parent 4ce5e6a commit db9b1b9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 110 deletions.
33 changes: 0 additions & 33 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}
}
Expand All @@ -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(),
}
}

Expand Down Expand Up @@ -119,14 +110,6 @@ impl From<uri::InvalidUri> for Error {
}
}

impl From<uri::InvalidUriBytes> for Error {
fn from(err: uri::InvalidUriBytes) -> Error {
Error {
inner: ErrorKind::UriShared(err),
}
}
}

impl From<uri::InvalidUriParts> for Error {
fn from(err: uri::InvalidUriParts) -> Error {
Error {
Expand All @@ -143,14 +126,6 @@ impl From<header::InvalidHeaderName> for Error {
}
}

impl From<header::InvalidHeaderNameBytes> for Error {
fn from(err: header::InvalidHeaderNameBytes) -> Error {
Error {
inner: ErrorKind::HeaderNameShared(err),
}
}
}

impl From<header::InvalidHeaderValue> for Error {
fn from(err: header::InvalidHeaderValue) -> Error {
Error {
Expand All @@ -159,14 +134,6 @@ impl From<header::InvalidHeaderValue> for Error {
}
}

impl From<header::InvalidHeaderValueBytes> for Error {
fn from(err: header::InvalidHeaderValueBytes) -> Error {
Error {
inner: ErrorKind::HeaderValueShared(err),
}
}
}

impl From<std::convert::Infallible> for Error {
fn from(err: std::convert::Infallible) -> Error {
match err {}
Expand Down
4 changes: 2 additions & 2 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
16 changes: 0 additions & 16 deletions src/header/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
(
$(
Expand Down Expand Up @@ -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> {
Expand Down
27 changes: 4 additions & 23 deletions src/header/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, InvalidHeaderValueBytes> {
HeaderValue::try_from_generic(src, std::convert::identity).map_err(InvalidHeaderValueBytes)
fn from_shared(src: Bytes) -> Result<HeaderValue, InvalidHeaderValue> {
HeaderValue::try_from_generic(src, std::convert::identity)
}

/*
Expand All @@ -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");
}
}
Expand Down Expand Up @@ -511,7 +504,7 @@ impl<'a> TryFrom<&'a [u8]> for HeaderValue {
}

impl TryFrom<String> for HeaderValue {
type Error = InvalidHeaderValueBytes;
type Error = InvalidHeaderValue;

#[inline]
fn try_from(t: String) -> Result<Self, Self::Error> {
Expand All @@ -520,7 +513,7 @@ impl TryFrom<String> for HeaderValue {
}

impl TryFrom<Vec<u8>> for HeaderValue {
type Error = InvalidHeaderValueBytes;
type Error = InvalidHeaderValue;

#[inline]
fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/uri/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -41,8 +41,8 @@ impl Authority {
/// assert_eq!(authority.host(), "example.com");
/// # }
/// ```
pub(super) fn from_shared(s: Bytes) -> Result<Self, InvalidUriBytes> {
let authority_end = Authority::parse_non_empty(&s[..]).map_err(InvalidUriBytes)?;
pub(super) fn from_shared(s: Bytes) -> Result<Self, InvalidUri> {
let authority_end = Authority::parse_non_empty(&s[..])?;

if authority_end != s.len() {
return Err(ErrorKind::InvalidUriChar.into());
Expand Down Expand Up @@ -269,7 +269,7 @@ impl Authority {

/*
impl TryFrom<Bytes> for Authority {
type Error = InvalidUriBytes;
type Error = InvalidUri;
/// Attempt to convert an `Authority` from `Bytes`.
///
/// # Examples
Expand All @@ -290,7 +290,7 @@ impl TryFrom<Bytes> for Authority {
/// # }
/// ```
fn try_from(s: Bytes) -> Result<Self, Self::Error> {
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());
Expand Down
34 changes: 6 additions & 28 deletions src/uri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -260,7 +256,7 @@ impl Uri {
/// assert_eq!(uri.path(), "/foo");
/// # }
/// ```
fn from_shared(s: Bytes) -> Result<Uri, InvalidUriBytes> {
fn from_shared(s: Bytes) -> Result<Uri, InvalidUri> {
use self::ErrorKind::*;

if s.len() > MAX_LEN {
Expand Down Expand Up @@ -697,7 +693,7 @@ impl<'a> TryFrom<&'a String> for Uri {
}

impl TryFrom<String> for Uri {
type Error = InvalidUriBytes;
type Error = InvalidUri;

#[inline]
fn try_from(t: String) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -785,9 +781,9 @@ impl From<Uri> for Parts {
}
}

fn parse_full(mut s: Bytes) -> Result<Uri, InvalidUriBytes> {
fn parse_full(mut s: Bytes) -> Result<Uri, InvalidUri> {
// 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
Expand All @@ -810,7 +806,7 @@ fn parse_full(mut s: Bytes) -> Result<Uri, InvalidUriBytes> {

// 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() {
Expand Down Expand Up @@ -850,7 +846,7 @@ impl FromStr for Uri {

#[inline]
fn from_str(s: &str) -> Result<Uri, InvalidUri> {
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()))
}
}

Expand Down Expand Up @@ -1019,12 +1015,6 @@ impl From<ErrorKind> for InvalidUri {
}
}

impl From<ErrorKind> for InvalidUriBytes {
fn from(src: ErrorKind) -> InvalidUriBytes {
InvalidUriBytes(src.into())
}
}

impl From<ErrorKind> for InvalidUriParts {
fn from(src: ErrorKind) -> InvalidUriParts {
InvalidUriParts(src.into())
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions src/uri/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,7 +39,7 @@ impl PathAndQuery {
/// assert_eq!(path_and_query.query(), Some("world"));
/// # }
/// ```
pub(super) fn from_shared(mut src: Bytes) -> Result<Self, InvalidUriBytes> {
pub(super) fn from_shared(mut src: Bytes) -> Result<Self, InvalidUri> {
let mut query = NONE;
let mut fragment = None;

Expand Down Expand Up @@ -281,7 +281,7 @@ impl<'a> TryFrom<&'a [u8]> for PathAndQuery {
type Error = InvalidUri;
#[inline]
fn try_from(s: &'a [u8]) -> Result<Self, Self::Error> {
PathAndQuery::from_shared(Bytes::copy_from_slice(s)).map_err(|e| e.0)
PathAndQuery::from_shared(Bytes::copy_from_slice(s))
}
}

Expand Down

0 comments on commit db9b1b9

Please sign in to comment.