From b9e010facc11ff3d31c170d4f201561ba2c78815 Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Fri, 15 Dec 2023 20:22:23 +0800 Subject: [PATCH] chore(core): sealed RequestExt and ResponseExt traits (#115) --- viz-core/src/request.rs | 7 ++- viz-core/src/response.rs | 93 ++++++++++++++++++---------------------- 2 files changed, 48 insertions(+), 52 deletions(-) diff --git a/viz-core/src/request.rs b/viz-core/src/request.rs index a394caa8..d11ed191 100644 --- a/viz-core/src/request.rs +++ b/viz-core/src/request.rs @@ -37,7 +37,7 @@ use crate::types::{ParamsError, PathDeserializer, RouteInfo}; /// The [`Request`] Extension. #[async_trait] -pub trait RequestExt: Sized { +pub trait RequestExt: private::Sealed + Sized { /// Get URL's schema of this request. fn schema(&self) -> Option<&http::uri::Scheme>; @@ -445,3 +445,8 @@ impl RequestExt for Request { RealIp::parse(self) } } + +mod private { + pub trait Sealed {} + impl Sealed for super::Request {} +} diff --git a/viz-core/src/response.rs b/viz-core/src/response.rs index 272b8246..0701c6c8 100644 --- a/viz-core/src/response.rs +++ b/viz-core/src/response.rs @@ -5,7 +5,7 @@ use crate::{async_trait, header, Bytes, Error, OutgoingBody, Response, Result, S /// The [`Response`] Extension. #[async_trait] -pub trait ResponseExt: Sized { +pub trait ResponseExt: private::Sealed + Sized { /// Get the size of this response's body. fn content_length(&self) -> Option; @@ -99,33 +99,60 @@ pub trait ResponseExt: Sized { /// or as an attachment, that is downloaded and saved locally. /// /// [mdn]: - fn attachment(value: &str) -> Self; + fn attachment(value: &str) -> Response { + let val = header::HeaderValue::from_str(value) + .expect("content-disposition is not the correct value"); + let mut resp = Response::default(); + resp.headers_mut().insert(header::CONTENT_DISPOSITION, val); + resp + } /// The [`Content-Location`][mdn] header indicates an alternate location for the returned data. /// /// [mdn]: - fn location(location: T) -> Self + fn location(location: T) -> Response where - T: AsRef; + T: AsRef, + { + let val = header::HeaderValue::try_from(location.as_ref()) + .expect("location is not the correct value"); + let mut resp = Response::default(); + resp.headers_mut().insert(header::CONTENT_LOCATION, val); + resp + } /// The response redirects to the specified URL. /// /// [mdn]: - fn redirect(url: T) -> Self + fn redirect(url: T) -> Response where - T: AsRef; + T: AsRef, + { + let val = + header::HeaderValue::try_from(url.as_ref()).expect("url is not the correct value"); + let mut resp = Response::default(); + resp.headers_mut().insert(header::LOCATION, val); + resp + } /// The response redirects to the specified URL and the status code. /// /// [mdn]: - fn redirect_with_status(uri: T, status: StatusCode) -> Self + fn redirect_with_status(url: T, status: StatusCode) -> Response where - T: AsRef; + T: AsRef, + { + assert!(status.is_redirection(), "not a redirection status code"); + + let mut resp = Self::redirect(url); + *resp.status_mut() = status; + resp + } /// The response redirects to the [`303`][mdn]. /// /// [mdn]: - fn see_other(url: T) -> Self + fn see_other(url: T) -> Response where T: AsRef, { @@ -135,7 +162,7 @@ pub trait ResponseExt: Sized { /// The response redirects to the [`307`][mdn]. /// /// [mdn]: - fn temporary(url: T) -> Self + fn temporary(url: T) -> Response where T: AsRef, { @@ -145,7 +172,7 @@ pub trait ResponseExt: Sized { /// The response redirects to the [`308`][mdn]. /// /// [mdn]: - fn permanent(url: T) -> Self + fn permanent(url: T) -> Response where T: AsRef, { @@ -201,45 +228,9 @@ impl ResponseExt for Response { )); Ok(resp) } +} - fn attachment(value: &str) -> Self { - let val = header::HeaderValue::from_str(value) - .expect("content-disposition is not the correct value"); - let mut resp = Self::default(); - resp.headers_mut().insert(header::CONTENT_DISPOSITION, val); - resp - } - - fn location(location: T) -> Self - where - T: AsRef, - { - let val = header::HeaderValue::try_from(location.as_ref()) - .expect("location is not the correct value"); - let mut resp = Self::default(); - resp.headers_mut().insert(header::CONTENT_LOCATION, val); - resp - } - - fn redirect(url: T) -> Self - where - T: AsRef, - { - let val = - header::HeaderValue::try_from(url.as_ref()).expect("url is not the correct value"); - let mut resp = Self::default(); - resp.headers_mut().insert(header::LOCATION, val); - resp - } - - fn redirect_with_status(url: T, status: StatusCode) -> Self - where - T: AsRef, - { - assert!(status.is_redirection(), "not a redirection status code"); - - let mut resp = Self::redirect(url); - *resp.status_mut() = status; - resp - } +mod private { + pub trait Sealed {} + impl Sealed for super::Response {} }