feat(status): add StatusCode::try_from(u16).

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`.
This commit is contained in:
Sean McArthur
2017-06-13 09:49:27 -07:00
parent c166268c07
commit f953cafe27
4 changed files with 44 additions and 7 deletions

View File

@@ -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,
@@ -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>");
@@ -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();
}
}