rocket 0.1.6

Web framework for nightly with a focus on ease-of-use, expressability, and speed.
Documentation
use std::fmt;
use std::str::FromStr;


use error::Error;
use http::hyper;
use http::ascii;

use self::Method::*;

// TODO: Support non-standard methods, here and in codegen.

/// Representation of HTTP methods.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Method {
    Get,
    Put,
    Post,
    Delete,
    Options,
    Head,
    Trace,
    Connect,
    Patch
}

impl Method {
    #[doc(hidden)]
    pub fn from_hyp(method: &hyper::Method) -> Option<Method> {
        match *method {
            hyper::Method::Get => Some(Get),
            hyper::Method::Put => Some(Put),
            hyper::Method::Post => Some(Post),
            hyper::Method::Delete => Some(Delete),
            hyper::Method::Options => Some(Options),
            hyper::Method::Head => Some(Head),
            hyper::Method::Trace => Some(Trace),
            hyper::Method::Connect => Some(Connect),
            hyper::Method::Patch => Some(Patch),
            hyper::Method::Extension(_) => None,
        }
    }

    #[doc(hidden)]
    #[inline(always)]
    pub fn to_hyp(&self) -> hyper::Method {
        self.to_string().as_str().parse().unwrap()
    }

    /// Whether an HTTP request with the given method supports a payload.
    pub fn supports_payload(&self) -> bool {
        match *self {
            Put | Post | Delete | Patch => true,
            Get | Head | Connect | Trace | Options => false,
        }
    }

    #[inline(always)]
    pub fn as_str(&self) -> &'static str {
        match *self {
            Get => "GET",
            Put => "PUT",
            Post => "POST",
            Delete => "DELETE",
            Options => "OPTIONS",
            Head => "HEAD",
            Trace => "TRACE",
            Connect => "CONNECT",
            Patch => "PATCH",
        }
    }
}

impl FromStr for Method {
    type Err = Error;

    // According to the RFC, method names are case-sensitive. But some old
    // clients don't follow this, so we just do a case-insensitive match here.
    fn from_str(s: &str) -> Result<Method, Error> {
        match s {
            x if ascii::uncased_eq(x, Get.as_str()) => Ok(Get),
            x if ascii::uncased_eq(x, Put.as_str()) => Ok(Put),
            x if ascii::uncased_eq(x, Post.as_str()) => Ok(Post),
            x if ascii::uncased_eq(x, Delete.as_str()) => Ok(Delete),
            x if ascii::uncased_eq(x, Options.as_str()) => Ok(Options),
            x if ascii::uncased_eq(x, Head.as_str()) => Ok(Head),
            x if ascii::uncased_eq(x, Trace.as_str()) => Ok(Trace),
            x if ascii::uncased_eq(x, Connect.as_str()) => Ok(Connect),
            x if ascii::uncased_eq(x, Patch.as_str()) => Ok(Patch),
            _ => Err(Error::BadMethod),
        }
    }
}

impl fmt::Display for Method {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.as_str().fmt(f)
    }
}