Skip to content

Commit 1bcd1f4

Browse files
authored
chore: Added trait validated_definition macro (#24)
1 parent 1bb2ac0 commit 1bcd1f4

File tree

3 files changed

+40
-58
lines changed

3 files changed

+40
-58
lines changed

src/garde.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//! For usage examples, see the documentation for [`Validated`]
1515
//!
1616
17+
use crate::validated_definition;
1718
use ::garde::Validate;
1819
use actix_web::dev::{ServiceFactory, ServiceRequest};
1920
use actix_web::http::StatusCode;
@@ -49,35 +50,7 @@ use thiserror::Error;
4950
/// ```
5051
pub struct Validated<T>(pub T);
5152

52-
impl<T> Validated<T> {
53-
pub fn into_inner(self) -> T {
54-
self.0
55-
}
56-
}
57-
58-
impl<T> std::ops::Deref for Validated<T> {
59-
type Target = T;
60-
61-
fn deref(&self) -> &Self::Target {
62-
&self.0
63-
}
64-
}
65-
66-
impl<T> std::ops::DerefMut for Validated<T> {
67-
fn deref_mut(&mut self) -> &mut Self::Target {
68-
&mut self.0
69-
}
70-
}
71-
72-
impl<T> Debug for Validated<T>
73-
where
74-
T: Debug,
75-
{
76-
#[inline]
77-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78-
f.debug_tuple("Validated").field(&self.0).finish()
79-
}
80-
}
53+
validated_definition!();
8154

8255
/// Future that extracts and validates actix requests using the Actix Web [`FromRequest`] trait
8356
///

src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,39 @@ pub use crate::garde::Validated;
1515

1616
#[cfg(all(feature = "custom", not(feature = "validator"), not(feature = "garde")))]
1717
pub use crate::custom::Validated;
18+
19+
macro_rules! validated_definition {
20+
() => {
21+
impl<T> Validated<T> {
22+
pub fn into_inner(self) -> T {
23+
self.0
24+
}
25+
}
26+
27+
impl<T> std::ops::Deref for Validated<T> {
28+
type Target = T;
29+
30+
fn deref(&self) -> &Self::Target {
31+
&self.0
32+
}
33+
}
34+
35+
impl<T> std::ops::DerefMut for Validated<T> {
36+
fn deref_mut(&mut self) -> &mut Self::Target {
37+
&mut self.0
38+
}
39+
}
40+
41+
impl<T> Debug for Validated<T>
42+
where
43+
T: Debug,
44+
{
45+
#[inline]
46+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47+
f.debug_tuple("Validated").field(&self.0).finish()
48+
}
49+
}
50+
};
51+
}
52+
53+
pub(crate) use validated_definition;

src/validator.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//! For usage examples, see the documentation for [`Validated`]
1515
//!
1616
17+
use crate::validated_definition;
1718
use ::validator::Validate;
1819
use actix_web::dev::{ServiceFactory, ServiceRequest};
1920
use actix_web::http::StatusCode;
@@ -50,35 +51,7 @@ use validator::{ValidationError, ValidationErrors, ValidationErrorsKind};
5051
/// ```
5152
pub struct Validated<T>(pub T);
5253

53-
impl<T> Validated<T> {
54-
pub fn into_inner(self) -> T {
55-
self.0
56-
}
57-
}
58-
59-
impl<T> std::ops::Deref for Validated<T> {
60-
type Target = T;
61-
62-
fn deref(&self) -> &Self::Target {
63-
&self.0
64-
}
65-
}
66-
67-
impl<T> std::ops::DerefMut for Validated<T> {
68-
fn deref_mut(&mut self) -> &mut Self::Target {
69-
&mut self.0
70-
}
71-
}
72-
73-
impl<T> Debug for Validated<T>
74-
where
75-
T: Debug,
76-
{
77-
#[inline]
78-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79-
f.debug_tuple("Validated").field(&self.0).finish()
80-
}
81-
}
54+
validated_definition!();
8255

8356
/// Future that extracts and validates actix requests using the Actix Web [`FromRequest`] trait
8457
///

0 commit comments

Comments
 (0)