Cleaned up to get rid of warnings.

This commit is contained in:
Sergio Benitez 2016-03-17 20:37:34 -07:00
parent e4ceabb0e0
commit 40559736fc
5 changed files with 39 additions and 45 deletions

View File

@ -3,15 +3,9 @@
extern crate rocket; extern crate rocket;
use rocket::Rocket; use rocket::Rocket;
use rocket::response::{HypResponse, HypFresh, Responder};
use std::fs::File; use std::fs::File;
// #[route(GET, path = "/")]
// fn simple() -> &'static str {
// "Hello, simple example world! How is thou?"
// }
#[route(GET, path = "/")] #[route(GET, path = "/")]
fn simple() -> File { fn simple() -> File {
File::open("/tmp/index.html").unwrap() File::open("/tmp/index.html").unwrap()
@ -28,8 +22,8 @@ fn simple3() -> String {
} }
#[route(GET, path = "/<name>/<age>")] #[route(GET, path = "/<name>/<age>")]
fn simple4(name: &str, age: i8) -> &str { fn simple4(name: &str, age: i8) -> String {
name format!("Hello, {} year old named {}!", age, name)
} }
#[route(GET, path = "/something")] #[route(GET, path = "/something")]

View File

@ -7,8 +7,6 @@ pub mod request;
pub mod param; pub mod param;
pub mod router; pub mod router;
use std::io::Write;
pub use method::Method; pub use method::Method;
pub use error::Error; pub use error::Error;
pub use response::{Response, HypResponse, HypFresh, Responder}; pub use response::{Response, HypResponse, HypFresh, Responder};
@ -41,7 +39,7 @@ pub struct Rocket {
impl HypHandler for Rocket { impl HypHandler for Rocket {
fn handle<'a, 'k>(&'a self, req: HypRequest<'a, 'k>, fn handle<'a, 'k>(&'a self, req: HypRequest<'a, 'k>,
mut res: HypResponse<'a, HypFresh>) { res: HypResponse<'a, HypFresh>) {
println!("Request: {:?}", req.uri); println!("Request: {:?}", req.uri);
if self.handler.is_some() { if self.handler.is_some() {
let handler = self.handler.as_ref(); let handler = self.handler.as_ref();

View File

@ -8,7 +8,7 @@ impl Request {
Request Request
} }
pub fn get_param_str<'a>(&self, name: &'a str) -> Result<&'a str, Error> { pub fn get_param_str<'a>(&self, _name: &'a str) -> Result<&'a str, Error> {
Err(Error::NoKey) Err(Error::NoKey)
} }

View File

@ -2,8 +2,8 @@ pub use hyper::server::Response as HypResponse;
pub use hyper::net::Fresh as HypFresh; pub use hyper::net::Fresh as HypFresh;
use hyper::status::StatusCode; use hyper::status::StatusCode;
use hyper::header::{self, Headers, Encoding}; use hyper::header;
use std::io::{self, Read, Write}; use std::io::{Read, Write};
use std::fs::File; use std::fs::File;
pub struct Response<'a> { pub struct Response<'a> {
@ -16,9 +16,31 @@ impl<'a> Response<'a> {
body: Box::new(body) body: Box::new(body)
} }
} }
pub fn empty() -> Response<'a> {
Response {
body: Box::new(Empty::new(StatusCode::Ok))
}
}
pub fn not_found() -> Response<'a> {
Response {
body: Box::new(Empty::new(StatusCode::NotFound))
}
}
pub fn server_error() -> Response<'a> {
Response {
body: Box::new(Empty::new(StatusCode::InternalServerError))
}
}
} }
struct Empty { pub trait Responder {
fn respond<'a>(&mut self, mut res: HypResponse<'a, HypFresh>);
}
pub struct Empty {
status: StatusCode status: StatusCode
} }
@ -30,24 +52,24 @@ impl Empty {
} }
} }
pub trait Responder {
fn respond<'a>(&mut self, mut res: HypResponse<'a, HypFresh>);
}
impl Responder for Empty { impl Responder for Empty {
fn respond<'a>(&mut self, mut res: HypResponse<'a, HypFresh>) { fn respond<'a>(&mut self, mut res: HypResponse<'a, HypFresh>) {
res.send(b"").unwrap(); res.headers_mut().set(header::ContentLength(0));
*(res.status_mut()) = self.status;
let mut stream = res.start().unwrap();
stream.write_all(b"").unwrap();
} }
} }
impl<'a> Responder for &'a str { impl<'a> Responder for &'a str {
fn respond<'b>(&mut self, mut res: HypResponse<'b, HypFresh>) { fn respond<'b>(&mut self, res: HypResponse<'b, HypFresh>) {
res.send(self.as_bytes()).unwrap(); res.send(self.as_bytes()).unwrap();
} }
} }
impl Responder for String { impl Responder for String {
fn respond<'b>(&mut self, mut res: HypResponse<'b, HypFresh>) { fn respond<'b>(&mut self, res: HypResponse<'b, HypFresh>) {
res.send(self.as_bytes()).unwrap(); res.send(self.as_bytes()).unwrap();
} }
} }
@ -86,27 +108,6 @@ impl Responder for File {
// } // }
// } // }
impl<'a> Response<'a> {
pub fn empty() -> Response<'a> {
Response {
body: Box::new(Empty::new(StatusCode::Ok))
}
}
pub fn not_found() -> Response<'a> {
Response {
body: Box::new(Empty::new(StatusCode::NotFound))
}
}
pub fn server_error() -> Response<'a> {
Response {
body: Box::new(Empty::new(StatusCode::InternalServerError))
}
}
}
// macro_rules! impl_from_lengthed { // macro_rules! impl_from_lengthed {
// ($name:ident, $T:ty) => ( // ($name:ident, $T:ty) => (
// impl<'a> From<$T> for Response<'a> { // impl<'a> From<$T> for Response<'a> {

View File

@ -43,7 +43,8 @@ impl Router {
} }
} }
pub fn add_route(&mut self, method: Method, base: &str, route: &str) { // TODO: Use `method` argument
pub fn add_route(&mut self, _method: Method, base: &str, route: &str) {
// Allocate enough space for the worst case. // Allocate enough space for the worst case.
let mut deduped = String::with_capacity(base.len() + route.len() + 1); let mut deduped = String::with_capacity(base.len() + route.len() + 1);
@ -56,7 +57,7 @@ impl Router {
last = c; last = c;
} }
let mut path = PathBuf::from(deduped); let path = PathBuf::from(deduped);
println!("Mounted: {:?}", path); println!("Mounted: {:?}", path);
self.paths.push(path); self.paths.push(path);
} }