1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-31 08:57:00 +02:00

added extractor configuration system

This commit is contained in:
Nikolay Kim
2019-03-03 00:57:48 -08:00
parent 08fcb6891e
commit 6df85e32df
9 changed files with 210 additions and 50 deletions

View File

@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::cell::{Ref, RefMut};
use std::rc::Rc;
@@ -167,9 +168,18 @@ impl<P> std::ops::DerefMut for ServiceRequest<P> {
pub struct ServiceFromRequest<P> {
req: HttpRequest,
payload: Payload<P>,
config: Option<Rc<Extensions>>,
}
impl<P> ServiceFromRequest<P> {
pub(crate) fn new(req: ServiceRequest<P>, config: Option<Rc<Extensions>>) -> Self {
Self {
req: req.req,
payload: req.payload,
config,
}
}
#[inline]
pub fn into_request(self) -> HttpRequest {
self.req
@@ -180,6 +190,16 @@ impl<P> ServiceFromRequest<P> {
pub fn error_response<E: Into<Error>>(self, err: E) -> ServiceResponse {
ServiceResponse::new(self.req, err.into().into())
}
/// Load extractor configuration
pub fn load_config<T: Clone + Default + 'static>(&self) -> Cow<T> {
if let Some(ref ext) = self.config {
if let Some(cfg) = ext.get::<T>() {
return Cow::Borrowed(cfg);
}
}
Cow::Owned(T::default())
}
}
impl<P> std::ops::Deref for ServiceFromRequest<P> {
@@ -204,15 +224,6 @@ impl<P> HttpMessage for ServiceFromRequest<P> {
}
}
impl<P> From<ServiceRequest<P>> for ServiceFromRequest<P> {
fn from(req: ServiceRequest<P>) -> Self {
Self {
req: req.req,
payload: req.payload,
}
}
}
pub struct ServiceResponse<B = Body> {
request: HttpRequest,
response: Response<B>,