diff --git a/contrib/src/json/mod.rs b/contrib/src/json/mod.rs index b52e69ed..2b23c06c 100644 --- a/contrib/src/json/mod.rs +++ b/contrib/src/json/mod.rs @@ -4,7 +4,7 @@ extern crate serde_json; use std::ops::{Deref, DerefMut}; use rocket::request::{Request, FromRequest}; -use rocket::response::{Responder, Outcome, data}; +use rocket::response::{Responder, Outcome, ResponseOutcome, data}; use rocket::http::hyper::FreshHyperResponse; use self::serde::{Serialize, Deserialize}; @@ -70,7 +70,7 @@ impl<'r, 'c, T: Deserialize> FromRequest<'r, 'c> for JSON { } impl Responder for JSON { - fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> Outcome<'a> { + fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> ResponseOutcome<'a> { match serde_json::to_string(&self.0) { Ok(json_string) => data::JSON(json_string).respond(res), Err(e) => { diff --git a/contrib/src/templates/mod.rs b/contrib/src/templates/mod.rs index 0dc4f239..73625ff4 100644 --- a/contrib/src/templates/mod.rs +++ b/contrib/src/templates/mod.rs @@ -17,7 +17,7 @@ use std::path::{Path, PathBuf}; use std::collections::HashMap; use rocket::Rocket; -use rocket::response::{Content, Outcome, Responder}; +use rocket::response::{Content, Outcome, ResponseOutcome, Responder}; use rocket::http::hyper::FreshHyperResponse; use rocket::http::ContentType; @@ -133,7 +133,7 @@ impl Template { } impl Responder for Template { - fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> Outcome<'a> { + fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> ResponseOutcome<'a> { let content_type = match self.1 { Some(ref ext) => ContentType::from_extension(ext), None => ContentType::html() diff --git a/lib/src/request/data.rs b/lib/src/request/data.rs new file mode 100644 index 00000000..536ca2bf --- /dev/null +++ b/lib/src/request/data.rs @@ -0,0 +1,9 @@ +use std::io::Read; + +pub struct Data { + stream: Box +} + +pub trait FromData { + fn from_data(data: Data) -> Outcome { } +} diff --git a/lib/src/response/data.rs b/lib/src/response/data.rs index 97d95c52..c0e8986e 100644 --- a/lib/src/response/data.rs +++ b/lib/src/response/data.rs @@ -1,4 +1,4 @@ -use response::{Responder, Outcome}; +use response::{Responder, ResponseOutcome}; use http::hyper::{header, FreshHyperResponse}; use http::mime::{Mime, TopLevel, SubLevel}; use http::ContentType; @@ -6,7 +6,7 @@ use http::ContentType; pub struct Content(pub ContentType, pub T); impl Responder for Content { - fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> Outcome<'b> { + fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> ResponseOutcome<'b> { res.headers_mut().set(header::ContentType(self.0.clone().into())); self.1.respond(res) } @@ -17,7 +17,7 @@ macro_rules! impl_data_type_responder { pub struct $name(pub T); impl Responder for $name { - fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> Outcome<'b> { + fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> ResponseOutcome<'b> { let mime = Mime(TopLevel::$top, SubLevel::$sub, vec![]); res.headers_mut().set(header::ContentType(mime)); self.0.respond(res) diff --git a/lib/src/response/empty.rs b/lib/src/response/empty.rs index 79460c4c..5756e948 100644 --- a/lib/src/response/empty.rs +++ b/lib/src/response/empty.rs @@ -1,6 +1,6 @@ use std::io::Write; -use response::{Outcome, Responder}; +use response::{ResponseOutcome, Outcome, Responder}; use http::hyper::{header, FreshHyperResponse}; use http::hyper::StatusCode; @@ -13,20 +13,20 @@ impl Empty { } impl Responder for Empty { - fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> Outcome<'a> { + fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> ResponseOutcome<'a> { res.headers_mut().set(header::ContentLength(0)); *(res.status_mut()) = self.0; let mut stream = res.start().unwrap(); stream.write_all(b"").unwrap(); - Outcome::Complete + Outcome::Success } } pub struct Forward; impl Responder for Forward { - fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> Outcome<'a> { + fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> ResponseOutcome<'a> { Outcome::FailForward(res) } } diff --git a/lib/src/response/flash.rs b/lib/src/response/flash.rs index 626ac881..f08e4c81 100644 --- a/lib/src/response/flash.rs +++ b/lib/src/response/flash.rs @@ -1,6 +1,6 @@ use std::convert::AsRef; -use response::{Outcome, Responder}; +use response::{ResponseOutcome, Responder}; use request::{Request, FromRequest}; use http::hyper::{HyperSetCookie, HyperCookiePair, FreshHyperResponse}; @@ -43,7 +43,7 @@ impl Flash { } impl Responder for Flash { - fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> Outcome<'b> { + fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> ResponseOutcome<'b> { trace_!("Flash: setting message: {}:{}", self.name, self.message); res.headers_mut().set(HyperSetCookie(vec![self.cookie_pair()])); self.responder.respond(res) diff --git a/lib/src/response/mod.rs b/lib/src/response/mod.rs index c07ba10e..34df4727 100644 --- a/lib/src/response/mod.rs +++ b/lib/src/response/mod.rs @@ -13,7 +13,7 @@ pub use self::responder::Responder; pub use self::empty::{Empty, Forward}; pub use self::redirect::Redirect; pub use self::with_status::StatusResponse; -pub use self::outcome::Outcome; +pub use self::outcome::{Outcome, ResponseOutcome}; pub use self::flash::Flash; pub use self::named_file::NamedFile; pub use self::stream::Stream; diff --git a/lib/src/response/named_file.rs b/lib/src/response/named_file.rs index 57301374..a59bab2c 100644 --- a/lib/src/response/named_file.rs +++ b/lib/src/response/named_file.rs @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf}; use std::io; use std::ops::{Deref, DerefMut}; -use response::{Responder, Outcome}; +use response::{Responder, ResponseOutcome}; use http::hyper::{header, FreshHyperResponse}; use http::ContentType; @@ -33,7 +33,7 @@ impl NamedFile { } impl Responder for NamedFile { - fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> Outcome<'a> { + fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> ResponseOutcome<'a> { if let Some(ext) = self.path().extension() { let ext_string = ext.to_string_lossy().to_lowercase(); let content_type = ContentType::from_extension(&ext_string); diff --git a/lib/src/response/outcome.rs b/lib/src/response/outcome.rs index 4bfb2c15..6e92c2ea 100644 --- a/lib/src/response/outcome.rs +++ b/lib/src/response/outcome.rs @@ -5,21 +5,26 @@ use term_painter::ToStyle; use http::hyper::FreshHyperResponse; -pub enum Outcome<'h> { +pub enum Outcome { /// Signifies a response that completed sucessfully. - Complete, - /// Signifies a response that failed internally. - Bad(FreshHyperResponse<'h>), - /// Signifies a failing response where no further processing should happen. + Success, + /// Signifies a failing response that started responding but fail, so no + /// further processing can occur. FailStop, - /// Signifies a failing response whose request should be processed further. - FailForward(FreshHyperResponse<'h>), + /// Signifies a response that failed internally without beginning to + /// respond but no further processing should occur. + Bad(T), + /// Signifies a failing response that failed internally without beginning to + /// respond. Further processing should be attempted. + FailForward(T), } -impl<'h> Outcome<'h> { +pub type ResponseOutcome<'a> = Outcome>; + +impl Outcome { pub fn as_str(&self) -> &'static str { match *self { - Outcome::Complete => "Complete", + Outcome::Success => "Success", Outcome::FailStop => "FailStop", Outcome::Bad(..) => "Bad", Outcome::FailForward(..) => "FailForward", @@ -28,7 +33,7 @@ impl<'h> Outcome<'h> { fn as_int(&self) -> isize { match *self { - Outcome::Complete => 0, + Outcome::Success => 0, Outcome::Bad(..) => 1, Outcome::FailStop => 2, Outcome::FailForward(..) => 3, @@ -36,22 +41,22 @@ impl<'h> Outcome<'h> { } } -impl<'h> PartialEq for Outcome<'h> { - fn eq(&self, other: &Outcome<'h>) -> bool { +impl PartialEq for Outcome { + fn eq(&self, other: &Outcome) -> bool { self.as_int() == other.as_int() } } -impl<'h> fmt::Debug for Outcome<'h> { +impl fmt::Debug for Outcome { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Outcome::{}", self.as_str()) } } -impl<'h> fmt::Display for Outcome<'h> { +impl fmt::Display for Outcome { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - Outcome::Complete => write!(f, "{}", Green.paint("Complete")), + Outcome::Success => write!(f, "{}", Green.paint("Success")), Outcome::Bad(..) => write!(f, "{}", Yellow.paint("Bad Completion")), Outcome::FailStop => write!(f, "{}", Red.paint("Failed")), Outcome::FailForward(..) => write!(f, "{}", Cyan.paint("Forwarding")), diff --git a/lib/src/response/redirect.rs b/lib/src/response/redirect.rs index 1e20493f..46aeb60d 100644 --- a/lib/src/response/redirect.rs +++ b/lib/src/response/redirect.rs @@ -1,4 +1,4 @@ -use response::{Outcome, Responder}; +use response::{ResponseOutcome, Outcome, Responder}; use http::hyper::{header, FreshHyperResponse, StatusCode}; #[derive(Debug)] @@ -27,11 +27,11 @@ impl Redirect { } impl<'a> Responder for Redirect { - fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> Outcome<'b> { + fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> ResponseOutcome<'b> { res.headers_mut().set(header::ContentLength(0)); res.headers_mut().set(header::Location(self.1.clone())); *(res.status_mut()) = self.0; res.send(b"").unwrap(); - Outcome::Complete + Outcome::Success } } diff --git a/lib/src/response/responder.rs b/lib/src/response/responder.rs index 06d09740..5e235cf6 100644 --- a/lib/src/response/responder.rs +++ b/lib/src/response/responder.rs @@ -2,7 +2,7 @@ use std::io::{Read, Write}; use std::fs::File; use std::fmt; -use response::Outcome; +use response::{Outcome, ResponseOutcome}; use http::mime::{Mime, TopLevel, SubLevel}; use http::hyper::{header, FreshHyperResponse, StatusCode}; @@ -11,29 +11,29 @@ use http::hyper::{header, FreshHyperResponse, StatusCode}; // In particular, we want to try the next ranked route when when parsing // parameters doesn't work out. pub trait Responder { - fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> Outcome<'a>; + fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> ResponseOutcome<'a>; } impl<'a> Responder for &'a str { - fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> Outcome<'b> { + fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> ResponseOutcome<'b> { if res.headers().get::().is_none() { let mime = Mime(TopLevel::Text, SubLevel::Plain, vec![]); res.headers_mut().set(header::ContentType(mime)); } res.send(self.as_bytes()).unwrap(); - Outcome::Complete + Outcome::Success } } impl Responder for String { - fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> Outcome<'a> { + fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> ResponseOutcome<'a> { if res.headers().get::().is_none() { let mime = Mime(TopLevel::Text, SubLevel::Html, vec![]); res.headers_mut().set(header::ContentType(mime)); } res.send(self.as_bytes()).unwrap(); - Outcome::Complete + Outcome::Success } } @@ -42,7 +42,7 @@ impl Responder for String { // a way to retrieve a file based on its fd, strangely enough. See... // https://stackoverflow.com/questions/1188757/getting-filename-from-file-descriptor-in-c impl Responder for File { - fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> Outcome<'a> { + fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> ResponseOutcome<'a> { let size = self.metadata().unwrap().len(); res.headers_mut().set(header::ContentLength(size)); @@ -53,12 +53,12 @@ impl Responder for File { let mut stream = res.start().unwrap(); stream.write_all(&v).unwrap(); - Outcome::Complete + Outcome::Success } } impl Responder for Option { - fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> Outcome<'a> { + fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> ResponseOutcome<'a> { if self.is_none() { trace!("Option is none."); // TODO: Should this be a 404 or 500? @@ -71,7 +71,7 @@ impl Responder for Option { impl Responder for Result { // prepend with `default` when using impl specialization - default fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> Outcome<'a> { + default fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> ResponseOutcome<'a> { if self.is_err() { error_!("{:?}", self.as_ref().err().unwrap()); // TODO: Should this be a 404 or 500? @@ -83,7 +83,7 @@ impl Responder for Result { } impl Responder for Result { - fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> Outcome<'a> { + fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> ResponseOutcome<'a> { match *self { Ok(ref mut responder) => responder.respond(res), Err(ref mut responder) => responder.respond(res), diff --git a/lib/src/response/stream.rs b/lib/src/response/stream.rs index c8796d74..d5923c18 100644 --- a/lib/src/response/stream.rs +++ b/lib/src/response/stream.rs @@ -1,6 +1,6 @@ use std::io::{Read, Write, ErrorKind}; -use response::{Responder, Outcome}; +use response::{Responder, Outcome, ResponseOutcome}; use http::hyper::FreshHyperResponse; // TODO: Support custom chunk sizes. @@ -26,7 +26,7 @@ impl Stream { } impl Responder for Stream { - fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> Outcome<'a> { + fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> ResponseOutcome<'a> { let mut stream = res.start().unwrap(); let mut buffer = [0; CHUNK_SIZE]; let mut complete = false; @@ -55,6 +55,6 @@ impl Responder for Stream { return Outcome::FailStop; } - Outcome::Complete + Outcome::Success } } diff --git a/lib/src/response/with_status.rs b/lib/src/response/with_status.rs index eb08292c..806f2ad9 100644 --- a/lib/src/response/with_status.rs +++ b/lib/src/response/with_status.rs @@ -1,4 +1,4 @@ -use response::{Responder, Outcome}; +use response::{Responder, ResponseOutcome}; use http::hyper::{StatusCode, FreshHyperResponse}; pub struct StatusResponse { @@ -16,7 +16,7 @@ impl StatusResponse { } impl Responder for StatusResponse { - fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> Outcome<'b> { + fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> ResponseOutcome<'b> { *(res.status_mut()) = self.status; self.responder.respond(res) } diff --git a/lib/src/rocket.rs b/lib/src/rocket.rs index c315e169..e73e5ac0 100644 --- a/lib/src/rocket.rs +++ b/lib/src/rocket.rs @@ -75,7 +75,7 @@ impl Rocket { // Get the result if we failed forward so we can try again. res = match outcome { - Outcome::Complete | Outcome::FailStop => return, + Outcome::Success | Outcome::FailStop => return, Outcome::FailForward(r) => r, Outcome::Bad(r) => return self.handle_internal_error(&request, r), };