-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from yassun7010/feat_support_vec_error_custom_fn
feat: support FnOnce<T> -> Result<(), Vec<Error>>.
- Loading branch information
Showing
8 changed files
with
235 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod array; | ||
mod composited; | ||
pub mod custom; | ||
pub mod error; | ||
mod generic; | ||
mod numeric; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/// This function is used to avoid [rustc(E0282)](https://doc.rust-lang.org/error_codes/E0282.html) error in `#[validate(custom)]` validator on the struct. | ||
#[inline] | ||
pub fn wrap_closure_validation<T: ?Sized, M: IntoVecErrors>( | ||
data: &T, | ||
f: impl FnOnce(&T) -> Result<(), M>, | ||
) -> Result<(), Vec<crate::validation::Error>> { | ||
f(data).map_err(|e| e.into_vec_errors()) | ||
} | ||
|
||
#[inline] | ||
pub fn wrap_into_vec_errors<M: IntoVecErrors>( | ||
result: Result<(), M>, | ||
) -> Result<(), Vec<crate::validation::Error>> { | ||
result.map_err(|e| e.into_vec_errors()) | ||
} | ||
|
||
pub trait IntoVecErrors { | ||
fn into_vec_errors(self) -> Vec<crate::validation::Error>; | ||
} | ||
|
||
impl IntoVecErrors for Vec<crate::validation::Error> { | ||
fn into_vec_errors(self) -> Vec<crate::validation::Error> { | ||
self | ||
} | ||
} | ||
|
||
impl IntoVecErrors for crate::validation::Error { | ||
fn into_vec_errors(self) -> Vec<crate::validation::Error> { | ||
vec![self] | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_custom_fn_single_error() { | ||
fn single_error(data: &i32) -> Result<(), crate::validation::Error> { | ||
if *data > 0 { | ||
Ok(()) | ||
} else { | ||
Err(crate::validation::Error::Custom( | ||
"Value must be greater than 0".to_string(), | ||
)) | ||
} | ||
} | ||
|
||
assert!(wrap_closure_validation(&1i32, single_error).is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_custom_fn_multiple_errors() { | ||
fn multiple_errors(data: &i32) -> Result<(), Vec<crate::validation::Error>> { | ||
let mut errors = Vec::new(); | ||
if *data > 0 { | ||
return Ok(()); | ||
} else { | ||
errors.push(crate::validation::Error::Custom( | ||
"Value must be greater than 0".to_string(), | ||
)); | ||
} | ||
|
||
if *data < 10 { | ||
return Ok(()); | ||
} else { | ||
errors.push(crate::validation::Error::Custom( | ||
"Value must be less than 10".to_string(), | ||
)); | ||
} | ||
|
||
if errors.is_empty() { | ||
Ok(()) | ||
} else { | ||
Err(errors) | ||
} | ||
} | ||
|
||
assert!(wrap_closure_validation(&1i32, multiple_errors).is_ok()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters