From 5b15f15ce340c370ed84d4b6ab32a34852d43879 Mon Sep 17 00:00:00 2001 From: gengteng Date: Wed, 27 Sep 2023 10:31:45 +0800 Subject: [PATCH] impl HasValidateArgs for all extractors --- src/extra.rs | 18 ++++++++++++++++-- src/extra/form.rs | 11 +++++++++-- src/extra/protobuf.rs | 11 +++++++++-- src/extra/query.rs | 11 +++++++++-- src/extra/typed_path.rs | 14 ++++++-------- src/form.rs | 11 +++++++++-- src/json.rs | 11 +++++++++-- src/lib.rs | 12 ++++++++++++ src/msgpack.rs | 18 ++++++++++++++++-- src/path.rs | 11 +++++++++-- src/query.rs | 11 +++++++++-- src/typed_header.rs | 11 +++++++++-- src/typed_multipart.rs | 18 ++++++++++++++++-- src/yaml.rs | 11 +++++++++-- 14 files changed, 147 insertions(+), 32 deletions(-) diff --git a/src/extra.rs b/src/extra.rs index faafa43..82b1f68 100644 --- a/src/extra.rs +++ b/src/extra.rs @@ -236,9 +236,9 @@ pub mod query; #[cfg(feature = "extra_typed_path")] pub mod typed_path; -use crate::HasValidate; +use crate::{HasValidate, HasValidateArgs}; use axum_extra::extract::{Cached, WithRejection}; -use validator::Validate; +use validator::{Validate, ValidateArgs}; impl HasValidate for Cached { type Validate = T; @@ -248,6 +248,13 @@ impl HasValidate for Cached { } } +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Cached { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + impl HasValidate for WithRejection { type Validate = T; fn get_validate(&self) -> &T { @@ -255,6 +262,13 @@ impl HasValidate for WithRejection { } } +impl<'v, T: ValidateArgs<'v>, R> HasValidateArgs<'v> for WithRejection { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + #[cfg(test)] mod tests { use crate::tests::{Rejection, ValidTest}; diff --git a/src/extra/form.rs b/src/extra/form.rs index cc22471..459f2b4 100644 --- a/src/extra/form.rs +++ b/src/extra/form.rs @@ -38,9 +38,9 @@ //! } //! ``` -use crate::HasValidate; +use crate::{HasValidate, HasValidateArgs}; use axum_extra::extract::Form; -use validator::Validate; +use validator::{Validate, ValidateArgs}; impl HasValidate for Form { type Validate = T; @@ -49,6 +49,13 @@ impl HasValidate for Form { } } +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Form { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + #[cfg(test)] mod tests { use crate::tests::{ValidTest, ValidTestParameter}; diff --git a/src/extra/protobuf.rs b/src/extra/protobuf.rs index 5ad13c2..d03f8fc 100644 --- a/src/extra/protobuf.rs +++ b/src/extra/protobuf.rs @@ -40,9 +40,9 @@ //! } //! ``` -use crate::HasValidate; +use crate::{HasValidate, HasValidateArgs}; use axum_extra::protobuf::Protobuf; -use validator::Validate; +use validator::{Validate, ValidateArgs}; impl HasValidate for Protobuf { type Validate = T; @@ -51,6 +51,13 @@ impl HasValidate for Protobuf { } } +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Protobuf { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + #[cfg(test)] mod tests { use crate::tests::{ValidTest, ValidTestParameter}; diff --git a/src/extra/query.rs b/src/extra/query.rs index 205a861..c67b20e 100644 --- a/src/extra/query.rs +++ b/src/extra/query.rs @@ -41,9 +41,9 @@ //! } //! ``` -use crate::HasValidate; +use crate::{HasValidate, HasValidateArgs}; use axum_extra::extract::Query; -use validator::Validate; +use validator::{Validate, ValidateArgs}; impl HasValidate for Query { type Validate = T; @@ -52,6 +52,13 @@ impl HasValidate for Query { } } +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Query { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + #[cfg(test)] mod tests { use crate::tests::{ValidTest, ValidTestParameter}; diff --git a/src/extra/typed_path.rs b/src/extra/typed_path.rs index 90601b6..84d95a4 100644 --- a/src/extra/typed_path.rs +++ b/src/extra/typed_path.rs @@ -49,16 +49,14 @@ //! } //! ``` -use crate::Valid; +use crate::{Valid, ValidArgs}; use axum_extra::routing::TypedPath; -use std::fmt::{Display, Formatter}; - -impl Display for Valid { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) - } -} +use std::fmt::Display; impl TypedPath for Valid { const PATH: &'static str = T::PATH; } + +impl TypedPath for ValidArgs { + const PATH: &'static str = T::PATH; +} diff --git a/src/form.rs b/src/form.rs index 33d56ba..bd1df34 100644 --- a/src/form.rs +++ b/src/form.rs @@ -38,9 +38,9 @@ //! } //! ``` -use crate::HasValidate; +use crate::{HasValidate, HasValidateArgs}; use axum::Form; -use validator::Validate; +use validator::{Validate, ValidateArgs}; impl HasValidate for Form { type Validate = T; @@ -49,6 +49,13 @@ impl HasValidate for Form { } } +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Form { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + #[cfg(test)] mod tests { use crate::tests::{ValidTest, ValidTestParameter}; diff --git a/src/json.rs b/src/json.rs index a61552d..0068805 100644 --- a/src/json.rs +++ b/src/json.rs @@ -38,9 +38,9 @@ //! } //! ``` -use crate::HasValidate; +use crate::{HasValidate, HasValidateArgs}; use axum::Json; -use validator::Validate; +use validator::{Validate, ValidateArgs}; impl HasValidate for Json { type Validate = T; @@ -49,6 +49,13 @@ impl HasValidate for Json { } } +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Json { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + #[cfg(test)] mod tests { use crate::tests::{ValidTest, ValidTestParameter}; diff --git a/src/lib.rs b/src/lib.rs index 6000fe3..8e91ab5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,6 +66,12 @@ impl DerefMut for Valid { } } +impl Display for ValidArgs { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + impl Valid { /// Consume the `Valid` extractor and returns the inner type. pub fn into_inner(self) -> E { @@ -101,6 +107,12 @@ impl DerefMut for ValidArgs { } } +impl Display for Valid { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + impl ValidArgs { /// Consume the `ValidArgs` extractor and returns the inner type. pub fn into_inner(self) -> E { diff --git a/src/msgpack.rs b/src/msgpack.rs index 24f5f0f..ec95332 100644 --- a/src/msgpack.rs +++ b/src/msgpack.rs @@ -47,9 +47,9 @@ //! } //! ``` -use crate::HasValidate; +use crate::{HasValidate, HasValidateArgs}; use axum_msgpack::{MsgPack, MsgPackRaw}; -use validator::Validate; +use validator::{Validate, ValidateArgs}; impl HasValidate for MsgPack { type Validate = T; @@ -58,6 +58,13 @@ impl HasValidate for MsgPack { } } +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for MsgPack { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + impl HasValidate for MsgPackRaw { type Validate = T; fn get_validate(&self) -> &T { @@ -65,6 +72,13 @@ impl HasValidate for MsgPackRaw { } } +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for MsgPackRaw { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + #[cfg(test)] mod tests { use crate::tests::{ValidTest, ValidTestParameter}; diff --git a/src/path.rs b/src/path.rs index 36f9758..e01f1c6 100644 --- a/src/path.rs +++ b/src/path.rs @@ -37,9 +37,9 @@ //! } //! ``` -use crate::HasValidate; +use crate::{HasValidate, HasValidateArgs}; use axum::extract::Path; -use validator::Validate; +use validator::{Validate, ValidateArgs}; impl HasValidate for Path { type Validate = T; @@ -47,3 +47,10 @@ impl HasValidate for Path { &self.0 } } + +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Path { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} diff --git a/src/query.rs b/src/query.rs index 863acc9..1292d0b 100644 --- a/src/query.rs +++ b/src/query.rs @@ -41,9 +41,9 @@ //! } //! ``` -use crate::HasValidate; +use crate::{HasValidate, HasValidateArgs}; use axum::extract::Query; -use validator::Validate; +use validator::{Validate, ValidateArgs}; impl HasValidate for Query { type Validate = T; @@ -52,6 +52,13 @@ impl HasValidate for Query { } } +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Query { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + #[cfg(test)] mod tests { use crate::tests::{ValidTest, ValidTestParameter}; diff --git a/src/typed_header.rs b/src/typed_header.rs index 346d059..8fae4be 100644 --- a/src/typed_header.rs +++ b/src/typed_header.rs @@ -61,9 +61,9 @@ //! } //! ``` -use crate::HasValidate; +use crate::{HasValidate, HasValidateArgs}; use axum::TypedHeader; -use validator::Validate; +use validator::{Validate, ValidateArgs}; impl HasValidate for TypedHeader { type Validate = T; @@ -72,6 +72,13 @@ impl HasValidate for TypedHeader { } } +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for TypedHeader { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + #[cfg(test)] mod tests { use crate::tests::{ValidTest, ValidTestParameter}; diff --git a/src/typed_multipart.rs b/src/typed_multipart.rs index 1183009..b989a2a 100644 --- a/src/typed_multipart.rs +++ b/src/typed_multipart.rs @@ -50,9 +50,9 @@ //! } //! ``` -use crate::HasValidate; +use crate::{HasValidate, HasValidateArgs}; use axum_typed_multipart::{BaseMultipart, TypedMultipart}; -use validator::Validate; +use validator::{Validate, ValidateArgs}; impl HasValidate for BaseMultipart { type Validate = T; @@ -61,6 +61,13 @@ impl HasValidate for BaseMultipart { } } +impl<'v, T: ValidateArgs<'v>, R> HasValidateArgs<'v> for BaseMultipart { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.data + } +} + impl HasValidate for TypedMultipart { type Validate = T; fn get_validate(&self) -> &T { @@ -68,6 +75,13 @@ impl HasValidate for TypedMultipart { } } +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for TypedMultipart { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + #[cfg(test)] mod tests { use crate::tests::{ValidTest, ValidTestParameter}; diff --git a/src/yaml.rs b/src/yaml.rs index eaa40b8..0ebffc9 100644 --- a/src/yaml.rs +++ b/src/yaml.rs @@ -42,9 +42,9 @@ //! } //! ``` -use crate::HasValidate; +use crate::{HasValidate, HasValidateArgs}; use axum_yaml::Yaml; -use validator::Validate; +use validator::{Validate, ValidateArgs}; impl HasValidate for Yaml { type Validate = T; @@ -53,6 +53,13 @@ impl HasValidate for Yaml { } } +impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Yaml { + type ValidateArgs = T; + fn get_validate_args(&self) -> &Self::ValidateArgs { + &self.0 + } +} + #[cfg(test)] mod tests { use crate::tests::{ValidTest, ValidTestParameter};