1
1
#[ macro_export]
2
2
macro_rules! impl_conversions {
3
3
// Capture pairs separated by commas, where each pair is separated by =>
4
- ( $( $target_type: ty => $value_variant: path) ,* $( , ) ?) => {
4
+ ( $( $target_type: ty $ ( as $cast : ty ) ? => $value_variant: path) ,* $( , ) ?) => {
5
5
$(
6
6
impl FromValue for $target_type {
7
7
fn from_value( expr: & Value ) -> Result <Self , ExecutionError > {
8
8
if let $value_variant( v) = expr {
9
- Ok ( v. clone( ) )
9
+ $( if <$target_type>:: MAX as $cast < * v {
10
+ return Err ( ExecutionError :: CastOverflow {
11
+ value: * v as f64 ,
12
+ source_ty: std:: any:: type_name:: <$cast>( ) ,
13
+ target_ty: std:: any:: type_name:: <$target_type>( ) ,
14
+ } )
15
+ } else if <$target_type>:: MIN as $cast > * v {
16
+ return Err ( ExecutionError :: CastOverflow {
17
+ value: * v as f64 ,
18
+ source_ty: std:: any:: type_name:: <$cast>( ) ,
19
+ target_ty: std:: any:: type_name:: <$target_type>( ) ,
20
+ } )
21
+ } ) ?
22
+ Ok ( v. clone( ) $( as $cast as $target_type) ?)
10
23
} else {
11
24
Err ( ExecutionError :: UnexpectedType {
12
25
got: format!( "{:?}" , expr) ,
@@ -20,7 +33,22 @@ macro_rules! impl_conversions {
20
33
fn from_value( expr: & Value ) -> Result <Self , ExecutionError > {
21
34
match expr {
22
35
Value :: Null => Ok ( None ) ,
23
- $value_variant( v) => Ok ( Some ( v. clone( ) ) ) ,
36
+ $value_variant( v) => {
37
+ $( if <$target_type>:: MAX as $cast < * v {
38
+ return Err ( ExecutionError :: CastOverflow {
39
+ value: * v as f64 ,
40
+ source_ty: std:: any:: type_name:: <$cast>( ) ,
41
+ target_ty: std:: any:: type_name:: <$target_type>( ) ,
42
+ } )
43
+ } else if <$target_type>:: MIN as $cast > * v {
44
+ return Err ( ExecutionError :: CastOverflow {
45
+ value: * v as f64 ,
46
+ source_ty: std:: any:: type_name:: <$cast>( ) ,
47
+ target_ty: std:: any:: type_name:: <$target_type>( ) ,
48
+ } )
49
+ } ) ?
50
+ Ok ( Some ( v. clone( ) $( as $cast as $target_type) ?) )
51
+ } ,
24
52
_ => Err ( ExecutionError :: UnexpectedType {
25
53
got: format!( "{:?}" , expr) ,
26
54
want: stringify!( $target_type) . to_string( ) ,
@@ -31,19 +59,19 @@ macro_rules! impl_conversions {
31
59
32
60
impl From <$target_type> for Value {
33
61
fn from( value: $target_type) -> Self {
34
- $value_variant( value)
62
+ $value_variant( value $ ( as $cast ) ? )
35
63
}
36
64
}
37
65
38
66
impl $crate:: magic:: IntoResolveResult for $target_type {
39
67
fn into_resolve_result( self ) -> ResolveResult {
40
- Ok ( $value_variant( self ) )
68
+ Ok ( $value_variant( self $ ( as $cast ) ? ) )
41
69
}
42
70
}
43
71
44
72
impl $crate:: magic:: IntoResolveResult for Result <$target_type, ExecutionError > {
45
73
fn into_resolve_result( self ) -> ResolveResult {
46
- self . map( $value_variant)
74
+ self . map( |it| $value_variant( it $ ( as $cast ) ? ) )
47
75
}
48
76
}
49
77
0 commit comments