Skip to content

Commit

Permalink
feat(status): add StatusCode::try_from(u16).
Browse files Browse the repository at this point in the history
This is a temporary function until the `TryFrom` trait stabilizes.

BREAKING CHANGE: Removes the undocumented `from_u16` function. Use
  `StatusCode::try_from` instead.

  Also makes the `status` module private. All imports of
  `hyper::status::StatusCode` should be `hyper::StatusCode`.
  • Loading branch information
seanmonstar committed Jun 13, 2017
1 parent c166268 commit f953caf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/http/h1/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ impl Http1Transaction for ClientTransaction {
httparse::Status::Complete(len) => {
trace!("Response.try_parse Complete({})", len);
let code = res.code.unwrap();
let reason = match StatusCode::from_u16(code).canonical_reason() {
let status = try!(StatusCode::try_from(code).map_err(|_| ::Error::Status));
let reason = match status.canonical_reason() {
Some(reason) if reason == res.reason.unwrap() => Cow::Borrowed(reason),
_ => Cow::Owned(res.reason.unwrap().to_owned())
};
Expand Down
2 changes: 1 addition & 1 deletion src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl RawStatus {
/// Converts this into a StatusCode.
#[inline]
pub fn status(&self) -> StatusCode {
StatusCode::from_u16(self.0)
StatusCode::try_from(self.0).unwrap()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ mod method;
pub mod header;
mod http;
pub mod server;
pub mod status;
mod status;
mod uri;
mod version;
44 changes: 40 additions & 4 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,35 @@ pub enum StatusCode {
Unregistered(u16),
}

#[derive(Debug)]
pub struct InvalidStatusCode {
_inner: (),
}

impl StatusCode {

#[doc(hidden)]
// Not part of public API or API contract. Could disappear.
pub fn from_u16(n: u16) -> StatusCode {
/// Try to convert a `u16` into a `StatusCode`.
///
/// # Errors
///
/// This will return an error if the provided argument is not within the
/// range `100...599`.
///
/// # Note
///
/// This function is temporary. When the `TryFrom` trait becomes stable,
/// this will be deprecated and replaced by `TryFrom<u16>`.
pub fn try_from(n: u16) -> Result<StatusCode, InvalidStatusCode> {
if n < 100 || n > 599 {
Err(InvalidStatusCode {
_inner: (),
})
} else {
Ok(StatusCode::from_u16(n))
}
}

fn from_u16(n: u16) -> StatusCode {
match n {
100 => StatusCode::Continue,
101 => StatusCode::SwitchingProtocols,
Expand Down Expand Up @@ -490,7 +514,7 @@ impl Copy for StatusCode {}
/// Formats the status code, *including* the canonical reason.
///
/// ```rust
/// # use hyper::status::StatusCode::{ImATeapot, Unregistered};
/// # use hyper::StatusCode::{ImATeapot, Unregistered};
/// assert_eq!(format!("{}", ImATeapot), "418 I'm a teapot");
/// assert_eq!(format!("{}", Unregistered(123)),
/// "123 <unknown status code>");
Expand Down Expand Up @@ -750,4 +774,16 @@ mod tests {
Some("Network Authentication Required"));

}

#[test]
fn try_from() {
StatusCode::try_from(100).unwrap();
StatusCode::try_from(599).unwrap();
StatusCode::try_from(200).unwrap();

StatusCode::try_from(99).unwrap_err();
StatusCode::try_from(600).unwrap_err();
StatusCode::try_from(0).unwrap_err();
StatusCode::try_from(1000).unwrap_err();
}
}

0 comments on commit f953caf

Please sign in to comment.