1
1
use proc_macro2:: TokenStream ;
2
- use quote:: quote;
2
+ use quote:: { quote, ToTokens } ;
3
3
4
4
use crate :: common:: {
5
5
gen:: error:: { gen_error_type_name, gen_impl_error_trait} ,
@@ -8,13 +8,13 @@ use crate::common::{
8
8
9
9
use super :: super :: models:: FloatValidator ;
10
10
11
- pub fn gen_validation_error_type < T > (
11
+ pub fn gen_validation_error_type < T : ToTokens > (
12
12
type_name : & TypeName ,
13
13
validators : & [ FloatValidator < T > ] ,
14
14
) -> TokenStream {
15
15
let error_type_name = gen_error_type_name ( type_name) ;
16
16
let definition = gen_definition ( & error_type_name, validators) ;
17
- let impl_display_trait = gen_impl_display_trait ( & error_type_name, validators) ;
17
+ let impl_display_trait = gen_impl_display_trait ( type_name , & error_type_name, validators) ;
18
18
let impl_error_trait = gen_impl_error_trait ( & error_type_name) ;
19
19
20
20
quote ! {
@@ -62,28 +62,29 @@ fn gen_definition<T>(
62
62
}
63
63
}
64
64
65
- fn gen_impl_display_trait < T > (
65
+ fn gen_impl_display_trait < T : ToTokens > (
66
+ type_name : & TypeName ,
66
67
error_type_name : & ErrorTypeName ,
67
68
validators : & [ FloatValidator < T > ] ,
68
69
) -> TokenStream {
69
70
let match_arms = validators. iter ( ) . map ( |validator| match validator {
70
- FloatValidator :: Greater ( _ ) => quote ! {
71
- #error_type_name:: GreaterViolated => write!( f, "too small" )
71
+ FloatValidator :: Greater ( val ) => quote ! {
72
+ #error_type_name:: GreaterViolated => write!( f, "{} is too small. The value must be greater than {:#?}." , stringify! ( #type_name ) , #val )
72
73
} ,
73
- FloatValidator :: GreaterOrEqual ( _ ) => quote ! {
74
- #error_type_name:: GreaterOrEqualViolated => write!( f, "too small" )
74
+ FloatValidator :: GreaterOrEqual ( val ) => quote ! {
75
+ #error_type_name:: GreaterOrEqualViolated => write!( f, "{} is too small. The value must be greater or equal to {:#?}." , stringify! ( #type_name ) , #val )
75
76
} ,
76
- FloatValidator :: LessOrEqual ( _ ) => quote ! {
77
- #error_type_name:: LessOrEqualViolated => write!( f, "too big" )
77
+ FloatValidator :: LessOrEqual ( val ) => quote ! {
78
+ #error_type_name:: LessOrEqualViolated => write!( f, "{} is too big. The value must be less than {:#?}." , stringify! ( #type_name ) , #val )
78
79
} ,
79
- FloatValidator :: Less ( _ ) => quote ! {
80
- #error_type_name:: LessViolated => write!( f, "too big" )
80
+ FloatValidator :: Less ( val ) => quote ! {
81
+ #error_type_name:: LessViolated => write!( f, "{} is too big. The value must be less or equal to {:#?}." , stringify! ( #type_name ) , #val )
81
82
} ,
82
83
FloatValidator :: Predicate ( _) => quote ! {
83
- #error_type_name:: PredicateViolated => write!( f, "invalid" )
84
+ #error_type_name:: PredicateViolated => write!( f, "{} failed the predicate test." , stringify! ( #type_name ) )
84
85
} ,
85
86
FloatValidator :: Finite => quote ! {
86
- #error_type_name:: FiniteViolated => write!( f, "not finite" )
87
+ #error_type_name:: FiniteViolated => write!( f, "{} is not finite." , stringify! ( #type_name ) )
87
88
} ,
88
89
} ) ;
89
90
0 commit comments