From 5a5c24678f56bb6b002d2242cd21d19c4e99c8d8 Mon Sep 17 00:00:00 2001 From: gengteng Date: Wed, 27 Sep 2023 13:26:20 +0800 Subject: [PATCH] use ref as args --- src/extra/typed_path.rs | 2 +- src/lib.rs | 52 +++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/extra/typed_path.rs b/src/extra/typed_path.rs index 6704c22..f0d5edf 100644 --- a/src/extra/typed_path.rs +++ b/src/extra/typed_path.rs @@ -57,6 +57,6 @@ impl TypedPath for Valid { const PATH: &'static str = T::PATH; } -impl TypedPath for ValidEx { +impl TypedPath for ValidEx { const PATH: &'static str = T::PATH; } diff --git a/src/lib.rs b/src/lib.rs index 5076929..f12ca5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,7 +66,7 @@ impl DerefMut for Valid { } } -impl Display for ValidEx { +impl Display for ValidEx { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } @@ -93,9 +93,9 @@ impl Valid { /// examples of `Valid`, and does not demonstrate concrete usage of /// `ValidEx`, but the usage is analogous. #[derive(Debug, Clone, Copy, Default)] -pub struct ValidEx(pub E); +pub struct ValidEx(pub E, pub A); -impl Deref for ValidEx { +impl Deref for ValidEx { type Target = E; fn deref(&self) -> &Self::Target { @@ -103,7 +103,7 @@ impl Deref for ValidEx { } } -impl DerefMut for ValidEx { +impl DerefMut for ValidEx { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } @@ -115,7 +115,7 @@ impl Display for Valid { } } -impl ValidEx { +impl ValidEx { /// Consume the `ValidEx` extractor and returns the inner type. pub fn into_inner(self) -> E { self.0 @@ -377,65 +377,67 @@ where } #[async_trait] -impl<'v, S, B, E> FromRequest for ValidEx +impl FromRequest for ValidEx where S: Send + Sync, B: Send + Sync + 'static, - E: HasValidateArgs<'v> + FromRequest, - E::ValidateArgs: ValidateArgs<'v>, - <>::ValidateArgs as ValidateArgs<'v>>::Args: FromRef, + E: for<'v> HasValidateArgs<'v> + FromRequest, + for<'v> >::ValidateArgs: ValidateArgs<'v, Args = &'v A>, + A: FromRef, ValidationContext: FromRef, { type Rejection = ValidRejection<>::Rejection>; async fn from_request(req: Request, state: &S) -> Result { - let context: ValidationContext = FromRef::from_ref(state); + let ValidationContext { response_builder }: ValidationContext = FromRef::from_ref(state); let inner = E::from_request(req, state) .await .map_err(|e| ValidRejection { error: ValidError::Inner(e), - response_builder: context.response_builder, + response_builder, })?; - let args = FromRef::from_ref(state); + + let args: A = FromRef::from_ref(state); inner .get_validate_args() - .validate_args(args) + .validate_args(&args) .map_err(|e| ValidRejection { error: ValidError::Valid(e), - response_builder: context.response_builder, + response_builder, })?; - Ok(ValidEx(inner)) + Ok(ValidEx(inner, args)) } } #[async_trait] -impl<'v, S, E> FromRequestParts for ValidEx +impl FromRequestParts for ValidEx where S: Send + Sync, - E: HasValidateArgs<'v> + FromRequestParts, - E::ValidateArgs: ValidateArgs<'v>, - <>::ValidateArgs as ValidateArgs<'v>>::Args: FromRef, + E: for<'v> HasValidateArgs<'v> + FromRequestParts, + for<'v> >::ValidateArgs: ValidateArgs<'v, Args = &'v A>, + A: FromRef, ValidationContext: FromRef, { type Rejection = ValidRejection<>::Rejection>; async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { - let context: ValidationContext = FromRef::from_ref(state); + let ValidationContext { response_builder }: ValidationContext = FromRef::from_ref(state); let inner = E::from_request_parts(parts, state) .await .map_err(|e| ValidRejection { error: ValidError::Inner(e), - response_builder: context.response_builder, + response_builder, })?; - let args = FromRef::from_ref(state); + let args: A = FromRef::from_ref(state); inner .get_validate_args() - .validate_args(args) + .validate_args(&args) .map_err(|e| ValidRejection { error: ValidError::Valid(e), - response_builder: context.response_builder, + response_builder, })?; - Ok(ValidEx(inner)) + + Ok(ValidEx(inner, args)) } }