mirror of
https://github.com/actix/actix-web.git
synced 2024-12-21 23:56:35 +00:00
Export IntoHeaderValue
This commit is contained in:
parent
e738361e09
commit
dfa0abf5a5
3 changed files with 24 additions and 38 deletions
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
## [0.1.0-alpha.4] - 2019-04-xx
|
## [0.1.0-alpha.4] - 2019-04-xx
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Export IntoHeaderValue
|
||||||
|
|
||||||
### Deleted
|
### Deleted
|
||||||
|
|
||||||
* Removed PayloadBuffer
|
* Removed PayloadBuffer
|
||||||
|
|
|
@ -20,7 +20,6 @@ pub use self::common::*;
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use self::shared::*;
|
pub use self::shared::*;
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
/// A trait for any object that will represent a header field and value.
|
/// A trait for any object that will represent a header field and value.
|
||||||
pub trait Header
|
pub trait Header
|
||||||
where
|
where
|
||||||
|
@ -33,7 +32,6 @@ where
|
||||||
fn parse<T: HttpMessage>(msg: &T) -> Result<Self, ParseError>;
|
fn parse<T: HttpMessage>(msg: &T) -> Result<Self, ParseError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
/// A trait for any object that can be Converted to a `HeaderValue`
|
/// A trait for any object that can be Converted to a `HeaderValue`
|
||||||
pub trait IntoHeaderValue: Sized {
|
pub trait IntoHeaderValue: Sized {
|
||||||
/// The type returned in the event of a conversion error.
|
/// 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<HeaderValue, Self::Error> {
|
||||||
|
let s = format!("{}", self);
|
||||||
|
HeaderValue::from_shared(Bytes::from(s))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoHeaderValue for u64 {
|
||||||
|
type Error = InvalidHeaderValueBytes;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn try_into(self) -> Result<HeaderValue, Self::Error> {
|
||||||
|
let s = format!("{}", self);
|
||||||
|
HeaderValue::from_shared(Bytes::from(s))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl IntoHeaderValue for Mime {
|
impl IntoHeaderValue for Mime {
|
||||||
type Error = InvalidHeaderValueBytes;
|
type Error = InvalidHeaderValueBytes;
|
||||||
|
|
||||||
|
|
36
src/error.rs
36
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -180,10 +150,4 @@ mod tests {
|
||||||
let resp: HttpResponse = ReadlinesError::EncodingError.error_response();
|
let resp: HttpResponse = ReadlinesError::EncodingError.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue