From dfa0abf5a5e158f44bea043b765cdf7625bfa236 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 3 Apr 2019 12:44:47 -0700 Subject: [PATCH] Export IntoHeaderValue --- actix-http/CHANGES.md | 4 ++++ actix-http/src/header/mod.rs | 22 ++++++++++++++++++++-- src/error.rs | 36 ------------------------------------ 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 3ae481db4..742ff8d3e 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,10 @@ ## [0.1.0-alpha.4] - 2019-04-xx +### Changed + +* Export IntoHeaderValue + ### Deleted * Removed PayloadBuffer diff --git a/actix-http/src/header/mod.rs b/actix-http/src/header/mod.rs index 1ef1bd198..deedd693f 100644 --- a/actix-http/src/header/mod.rs +++ b/actix-http/src/header/mod.rs @@ -20,7 +20,6 @@ pub use self::common::*; #[doc(hidden)] pub use self::shared::*; -#[doc(hidden)] /// A trait for any object that will represent a header field and value. pub trait Header where @@ -33,7 +32,6 @@ where fn parse(msg: &T) -> Result; } -#[doc(hidden)] /// A trait for any object that can be Converted to a `HeaderValue` pub trait IntoHeaderValue: Sized { /// The type returned in the event of a conversion error. @@ -97,6 +95,26 @@ impl IntoHeaderValue for String { } } +impl IntoHeaderValue for usize { + type Error = InvalidHeaderValueBytes; + + #[inline] + fn try_into(self) -> Result { + let s = format!("{}", self); + HeaderValue::from_shared(Bytes::from(s)) + } +} + +impl IntoHeaderValue for u64 { + type Error = InvalidHeaderValueBytes; + + #[inline] + fn try_into(self) -> Result { + let s = format!("{}", self); + HeaderValue::from_shared(Bytes::from(s)) + } +} + impl IntoHeaderValue for Mime { type Error = InvalidHeaderValueBytes; diff --git a/src/error.rs b/src/error.rs index 78dc2fb6a..74b890f0a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -121,36 +121,6 @@ impl ResponseError for ReadlinesError { } } -/// A set of errors that can occur during parsing multipart streams -#[derive(Debug, Display, From)] -pub enum MultipartError { - /// Content-Type header is not found - #[display(fmt = "No Content-type header found")] - NoContentType, - /// Can not parse Content-Type header - #[display(fmt = "Can not parse Content-Type header")] - ParseContentType, - /// Multipart boundary is not found - #[display(fmt = "Multipart boundary is not found")] - Boundary, - /// Multipart stream is incomplete - #[display(fmt = "Multipart stream is incomplete")] - Incomplete, - /// Error during field parsing - #[display(fmt = "{}", _0)] - Parse(ParseError), - /// Payload error - #[display(fmt = "{}", _0)] - Payload(PayloadError), -} - -/// Return `BadRequest` for `MultipartError` -impl ResponseError for MultipartError { - fn error_response(&self) -> HttpResponse { - HttpResponse::new(StatusCode::BAD_REQUEST) - } -} - #[cfg(test)] mod tests { use super::*; @@ -180,10 +150,4 @@ mod tests { let resp: HttpResponse = ReadlinesError::EncodingError.error_response(); assert_eq!(resp.status(), StatusCode::BAD_REQUEST); } - - #[test] - fn test_multipart_error() { - let resp: HttpResponse = MultipartError::Boundary.error_response(); - assert_eq!(resp.status(), StatusCode::BAD_REQUEST); - } }