aboutsummaryrefslogtreecommitdiff
path: root/src/response.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-01-24 22:37:36 +0100
committerMartin Fischer <martin@push-f.com>2021-01-24 22:49:57 +0100
commit76e92d7281b45ce506046a8946b7fde3355c485d (patch)
treee3a261e993450047a3a366eae0091efc0948377c /src/response.rs
parent4ba2d050bdf1a3c0070f3aa2331c82745611af1f (diff)
define & impl traits instead of wrapping types
bump version to 0.3.0
Diffstat (limited to 'src/response.rs')
-rw-r--r--src/response.rs72
1 files changed, 31 insertions, 41 deletions
diff --git a/src/response.rs b/src/response.rs
index 39e6c98..9c92e4c 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -1,60 +1,50 @@
-//! Provides the [`Response`] convenience wrapper.
+//! Provides the [`SputnikBuilder`] trait.
use cookie::Cookie;
-use hyper::{StatusCode, header::{self, HeaderName, HeaderValue}};
+use hyper::{StatusCode, header::{self, HeaderValue}, http};
use time::{Duration, OffsetDateTime};
+use hyper::http::response::Builder;
-type HyperResponse = hyper::Response<hyper::Body>;
+pub trait SputnikBuilder {
+ /// Adds a Set-Cookie header.
+ fn set_cookie(&mut self, cookie: Cookie);
-/// Convenience wrapper around [`hyper::Response`].
-pub struct Response {
- res: HyperResponse
-}
+ /// Adds a Set-Cookie header to delete a cookie.
+ fn delete_cookie(&mut self, name: &str);
-impl Into<HyperResponse> for Response {
- fn into(self) -> HyperResponse {
- self.res
- }
+ /// Sets the Content-Type.
+ fn content_type(self, mime: mime::Mime) -> Builder;
}
-impl Response {
- pub fn new() -> Self {
- Response{res: HyperResponse::new(hyper::Body::empty())}
- }
-
- pub fn status(&mut self) -> &mut StatusCode {
- self.res.status_mut()
- }
-
- pub fn body(&mut self) -> &mut hyper::Body {
- self.res.body_mut()
- }
- pub fn headers(&mut self) -> &mut hyper::HeaderMap<header::HeaderValue> {
- self.res.headers_mut()
- }
+pub fn redirect(location: &str, code: StatusCode) -> Builder {
+ Builder::new().status(code).header(header::LOCATION, location)
+}
- pub fn set_header<S: AsRef<str>>(&mut self, header: HeaderName, value: S) {
- self.res.headers_mut().insert(header, HeaderValue::from_str(value.as_ref()).unwrap());
+impl SputnikBuilder for Builder {
+ fn set_cookie(&mut self, cookie: Cookie) {
+ self.headers_mut().unwrap().insert(header::SET_COOKIE, HeaderValue::from_str(&cookie.encoded().to_string()).unwrap());
}
- pub fn set_content_type(&mut self, mime: mime::Mime) {
- self.res.headers_mut().insert(header::CONTENT_TYPE, mime.to_string().parse().unwrap());
+ fn delete_cookie(&mut self, name: &str) {
+ let mut cookie = Cookie::new(name, "");
+ cookie.set_max_age(Duration::seconds(0));
+ cookie.set_expires(OffsetDateTime::now_utc() - Duration::days(365));
+ self.set_cookie(cookie);
}
- pub fn redirect<S: AsRef<str>>(&mut self, location: S, code: StatusCode) {
- *self.res.status_mut() = code;
- self.set_header(header::LOCATION, location);
+ fn content_type(self, mime: mime::Mime) -> Self {
+ self.header(header::CONTENT_TYPE, mime.to_string())
}
+}
- pub fn set_cookie(&mut self, cookie: Cookie) {
- self.res.headers_mut().append(header::SET_COOKIE, cookie.encoded().to_string().parse().unwrap());
- }
+pub trait EmptyBuilder<B> {
+ /// Consume the builder with an empty body.
+ fn empty(self) -> http::Result<http::response::Response<B>>;
+}
- pub fn delete_cookie(&mut self, name: &str) {
- let mut cookie = Cookie::new(name, "");
- cookie.set_max_age(Duration::seconds(0));
- cookie.set_expires(OffsetDateTime::now_utc() - Duration::days(365));
- self.set_cookie(cookie);
+impl EmptyBuilder<hyper::Body> for Builder {
+ fn empty(self) -> http::Result<http::response::Response<hyper::Body>> {
+ self.body(hyper::Body::empty())
}
} \ No newline at end of file