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:
@@ -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>,
|
||||
|
Reference in New Issue
Block a user