Merge pull request #65 from alexcrichton/owned-errors

Add owned variants for each error
This commit is contained in:
Alex Crichton
2017-07-28 17:43:16 -05:00
committed by GitHub
5 changed files with 99 additions and 18 deletions
+24
View File
@@ -26,8 +26,11 @@ enum ErrorKind {
StatusCode(status::InvalidStatusCode),
Method(method::InvalidMethod),
Uri(uri::InvalidUri),
UriShared(uri::InvalidUriBytes),
HeaderName(header::InvalidHeaderName),
HeaderNameShared(header::InvalidHeaderNameBytes),
HeaderValue(header::InvalidHeaderValue),
HeaderValueShared(header::InvalidHeaderValueBytes),
}
impl fmt::Display for Error {
@@ -44,8 +47,11 @@ 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(),
HeaderName(ref e) => e.description(),
HeaderNameShared(ref e) => e.description(),
HeaderValue(ref e) => e.description(),
HeaderValueShared(ref e) => e.description(),
}
}
}
@@ -68,14 +74,32 @@ 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<header::InvalidHeaderName> for Error {
fn from(err: header::InvalidHeaderName) -> Error {
Error { inner: ErrorKind::HeaderName(err) }
}
}
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 { inner: ErrorKind::HeaderValue(err) }
}
}
impl From<header::InvalidHeaderValueBytes> for Error {
fn from(err: header::InvalidHeaderValueBytes) -> Error {
Error { inner: ErrorKind::HeaderValueShared(err) }
}
}
+2
View File
@@ -93,10 +93,12 @@ pub use self::map::{
pub use self::name::{
HeaderName,
InvalidHeaderName,
InvalidHeaderNameBytes,
};
pub use self::value::{
HeaderValue,
InvalidHeaderValue,
InvalidHeaderValueBytes,
ToStrError,
};
+18 -2
View File
@@ -61,6 +61,10 @@ pub struct InvalidHeaderName {
_priv: (),
}
/// A possible error when converting a `HeaderName` from another type.
#[derive(Debug)]
pub struct InvalidHeaderNameBytes(InvalidHeaderName) ;
macro_rules! standard_headers {
(
$(
@@ -1558,10 +1562,10 @@ impl<'a> HttpTryFrom<&'a [u8]> for HeaderName {
}
impl HttpTryFrom<Bytes> for HeaderName {
type Error = InvalidHeaderName;
type Error = InvalidHeaderNameBytes;
#[inline]
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
Self::from_bytes(bytes.as_ref())
Self::from_bytes(bytes.as_ref()).map_err(InvalidHeaderNameBytes)
}
}
@@ -1628,6 +1632,18 @@ 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> {
+21 -4
View File
@@ -28,6 +28,11 @@ 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
@@ -137,8 +142,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]
pub fn try_from_shared(src: Bytes) -> Result<HeaderValue, InvalidHeaderValue> {
HeaderValue::try_from(src)
pub fn try_from_shared(src: Bytes) -> Result<HeaderValue, InvalidHeaderValueBytes> {
HeaderValue::try_from(src).map_err(InvalidHeaderValueBytes)
}
fn try_from<T: AsRef<[u8]> + Into<Bytes>>(src: T) -> Result<HeaderValue, InvalidHeaderValue> {
@@ -324,11 +329,11 @@ impl<'a> HttpTryFrom<&'a [u8]> for HeaderValue {
}
impl HttpTryFrom<Bytes> for HeaderValue {
type Error = InvalidHeaderValue;
type Error = InvalidHeaderValueBytes;
#[inline]
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
<HeaderValue>::try_from(bytes)
HeaderValue::try_from_shared(bytes)
}
}
@@ -369,6 +374,18 @@ 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)
+34 -12
View File
@@ -100,6 +100,10 @@ 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);
#[derive(Debug, Eq, PartialEq)]
enum ErrorKind {
InvalidUriChar,
@@ -204,7 +208,7 @@ impl Uri {
/// assert_eq!(uri.path(), "/foo");
/// # }
/// ```
pub fn try_from_shared(s: Bytes) -> Result<Uri, InvalidUri> {
pub fn try_from_shared(s: Bytes) -> Result<Uri, InvalidUriBytes> {
use self::ErrorKind::*;
if s.len() > MAX_LEN {
@@ -553,7 +557,7 @@ impl<'a> HttpTryFrom<&'a str> for Uri {
}
impl HttpTryFrom<Bytes> for Uri {
type Error = InvalidUri;
type Error = InvalidUriBytes;
#[inline]
fn try_from(t: Bytes) -> Result<Self, Self::Error> {
@@ -679,10 +683,10 @@ impl Scheme {
/// assert_eq!(scheme.as_str(), "http");
/// # }
/// ```
pub fn try_from_shared(s: Bytes) -> Result<Self, InvalidUri> {
pub fn try_from_shared(s: Bytes) -> Result<Self, InvalidUriBytes> {
use self::Scheme2::*;
match try!(Scheme2::parse_exact(&s[..])) {
match try!(Scheme2::parse_exact(&s[..]).map_err(InvalidUriBytes)) {
None => Err(ErrorKind::InvalidScheme.into()),
Standard(p) => Ok(Standard(p).into()),
Other(_) => {
@@ -892,8 +896,8 @@ impl Authority {
/// assert_eq!(authority.host(), "example.com");
/// # }
/// ```
pub fn try_from_shared(s: Bytes) -> Result<Self, InvalidUri> {
let authority_end = try!(Authority::parse(&s[..]));
pub fn try_from_shared(s: Bytes) -> Result<Self, InvalidUriBytes> {
let authority_end = try!(Authority::parse(&s[..]).map_err(InvalidUriBytes));
if authority_end != s.len() {
return Err(ErrorKind::InvalidUriChar.into());
@@ -1031,7 +1035,7 @@ impl OriginForm {
/// assert_eq!(origin_form.query(), Some("world"));
/// # }
/// ```
pub fn try_from_shared(mut src: Bytes) -> Result<Self, InvalidUri> {
pub fn try_from_shared(mut src: Bytes) -> Result<Self, InvalidUriBytes> {
let mut query = NONE;
for i in 0..src.len() {
@@ -1174,7 +1178,7 @@ impl FromStr for OriginForm {
type Err = InvalidUri;
fn from_str(s: &str) -> Result<Self, InvalidUri> {
OriginForm::try_from_shared(s.into())
OriginForm::try_from_shared(s.into()).map_err(|e| e.0)
}
}
@@ -1203,9 +1207,9 @@ impl fmt::Display for OriginForm {
}
}
fn parse_full(mut s: Bytes) -> Result<Uri, InvalidUri> {
fn parse_full(mut s: Bytes) -> Result<Uri, InvalidUriBytes> {
// Parse the scheme
let scheme = match try!(Scheme2::parse(&s[..])) {
let scheme = match try!(Scheme2::parse(&s[..]).map_err(InvalidUriBytes)) {
Scheme2::None => Scheme2::None,
Scheme2::Standard(p) => {
// TODO: use truncate
@@ -1228,7 +1232,7 @@ fn parse_full(mut s: Bytes) -> Result<Uri, InvalidUri> {
// Find the end of the authority. The scheme will already have been
// extracted.
let authority_end = try!(Authority::parse(&s[..]));
let authority_end = try!(Authority::parse(&s[..]).map_err(InvalidUriBytes));
if scheme.is_none() {
if authority_end != s.len() {
@@ -1284,7 +1288,7 @@ impl FromStr for Uri {
#[inline]
fn from_str(s: &str) -> Result<Uri, InvalidUri> {
Uri::try_from_shared(s.into())
Uri::try_from_shared(s.into()).map_err(|e| e.0)
}
}
@@ -1441,6 +1445,12 @@ impl From<ErrorKind> for InvalidUri {
}
}
impl From<ErrorKind> for InvalidUriBytes {
fn from(src: ErrorKind) -> InvalidUriBytes {
InvalidUriBytes(src.into())
}
}
impl fmt::Display for InvalidUri {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.description().fmt(f)
@@ -1461,6 +1471,18 @@ impl Error for InvalidUri {
}
}
impl fmt::Display for InvalidUriBytes {
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<T> From<Protocol> for Scheme2<T> {
fn from(src: Protocol) -> Self {
Scheme2::Standard(src)