From b7a11933335792418b51a0d166bda080b8cb0678 Mon Sep 17 00:00:00 2001 From: Dominic Date: Thu, 14 Jan 2021 18:45:32 +0100 Subject: [PATCH] make all fields of response private, we're breaking change anyways Closes #34 Related to #27 --- CHANGELOG.md | 4 ++++ src/response.rs | 25 +++++++++++++++---------- src/result/mod.rs | 2 -- src/result/no_content.rs | 1 - src/result/raw.rs | 1 - src/result/result.rs | 1 - src/result/success.rs | 1 - src/routing.rs | 1 - tests/resource_error.rs | 8 ++++---- 9 files changed, 23 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf19e66..6a0929d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added + - Support custom HTTP response headers + ### Changed - The cors handler can now copy headers from the request if desired + - All fields of `Response` are now private ## [0.1.1] - 2020-12-28 ### Added diff --git a/src/response.rs b/src/response.rs index b29aa4e..8f378ca 100644 --- a/src/response.rs +++ b/src/response.rs @@ -7,17 +7,12 @@ use mime::{Mime, APPLICATION_JSON}; /// A response, used to create the final gotham response from. #[derive(Debug)] pub struct Response { - #[deprecated(since = "0.1.2", note = "This field will be private in an upcomming release")] - pub status: StatusCode, - #[deprecated(since = "0.1.2", note = "This field will be private in an upcomming release")] - pub body: Body, - #[deprecated(since = "0.1.2", note = "This field will be private in an upcomming release")] - pub mime: Option, - #[deprecated(since = "0.1.2", note = "This field will be private in an upcomming release")] - pub headers: HeaderMap + pub(crate) status: StatusCode, + pub(crate) body: Body, + pub(crate) mime: Option, + pub(crate) headers: HeaderMap } -#[allow(deprecated)] impl Response { /// Create a new [Response] from raw data. #[must_use = "Creating a response is pointless if you don't use it"] @@ -63,8 +58,18 @@ impl Response { } } + /// Return the status code of this [Response]. + pub fn status(&self) -> StatusCode { + self.status + } + + /// Return the mime type of this [Response]. + pub fn mime(&self) -> Option<&Mime> { + self.mime.as_ref() + } + /// Add an HTTP header to the [Response]. - pub fn add_header(&mut self, name: HeaderName, value: HeaderValue) { + pub fn header(&mut self, name: HeaderName, value: HeaderValue) { self.headers.insert(name, value); } diff --git a/src/result/mod.rs b/src/result/mod.rs index 7531dd1..526af33 100644 --- a/src/result/mod.rs +++ b/src/result/mod.rs @@ -99,7 +99,6 @@ fn errorlog(e: E) { #[cfg(not(feature = "errorlog"))] fn errorlog(_e: E) {} -#[allow(deprecated)] fn handle_error(e: E) -> Pin> + Send>> where E: Display + IntoResponseError @@ -161,7 +160,6 @@ mod test { struct MsgError; #[test] - #[allow(deprecated)] fn result_from_future() { let nc = NoContent::default(); let res = block_on(nc.into_response()).unwrap(); diff --git a/src/result/no_content.rs b/src/result/no_content.rs index e4ab932..0c4fe05 100644 --- a/src/result/no_content.rs +++ b/src/result/no_content.rs @@ -92,7 +92,6 @@ where } #[cfg(test)] -#[allow(deprecated)] mod test { use super::*; use futures_executor::block_on; diff --git a/src/result/raw.rs b/src/result/raw.rs index 27d59c0..6b3b520 100644 --- a/src/result/raw.rs +++ b/src/result/raw.rs @@ -131,7 +131,6 @@ mod test { use mime::TEXT_PLAIN; #[test] - #[allow(deprecated)] fn raw_response() { let msg = "Test"; let raw = Raw::new(msg, TEXT_PLAIN); diff --git a/src/result/result.rs b/src/result/result.rs index 971689b..f22d756 100644 --- a/src/result/result.rs +++ b/src/result/result.rs @@ -49,7 +49,6 @@ where } #[cfg(test)] -#[allow(deprecated)] mod test { use super::*; use crate::result::OrAllTypes; diff --git a/src/result/success.rs b/src/result/success.rs index 40922a3..a3816ea 100644 --- a/src/result/success.rs +++ b/src/result/success.rs @@ -124,7 +124,6 @@ mod test { } #[test] - #[allow(deprecated)] fn success_always_successfull() { let success: Success = Msg::default().into(); let res = block_on(success.into_response()).expect("didn't expect error response"); diff --git a/src/routing.rs b/src/routing.rs index 9076b2c..70d2369 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -81,7 +81,6 @@ pub trait DrawResourceRoutes { fn remove(&mut self); } -#[allow(deprecated)] fn response_from(res: Response, state: &State) -> gotham::hyper::Response { let mut r = create_empty_response(state, res.status); let headers = r.headers_mut(); diff --git a/tests/resource_error.rs b/tests/resource_error.rs index c850175..9325dc5 100644 --- a/tests/resource_error.rs +++ b/tests/resource_error.rs @@ -21,8 +21,8 @@ mod resource_error { fn io_error() { let err = Error::IoError(std::io::Error::last_os_error()); let res = err.into_response_error().unwrap(); - assert_eq!(res.status, StatusCode::INTERNAL_SERVER_ERROR); - assert_eq!(res.mime, Some(APPLICATION_JSON)); + assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR); + assert_eq!(res.mime(), Some(&APPLICATION_JSON)); } #[test] @@ -31,7 +31,7 @@ mod resource_error { assert_eq!(&format!("{}", err), "Internal Server Error: Brocken"); let res = err.into_response_error().unwrap(); - assert_eq!(res.status, StatusCode::INTERNAL_SERVER_ERROR); - assert_eq!(res.mime, None); // TODO shouldn't this be a json error message? + assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR); + assert_eq!(res.mime(), None); // TODO shouldn't this be a json error message? } }