File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ pub enum ValueError {
77 InvalidString ( std:: str:: Utf8Error ) ,
88 /// Encountered string with \0 bytes.
99 StringWithZeroBytes ( std:: ffi:: NulError ) ,
10+ /// Value out of range.
11+ OutOfRange ,
1012 /// Internal error.
1113 Internal ( String ) ,
1214 ///
@@ -35,6 +37,7 @@ impl fmt::Display for ValueError {
3537 e
3638 ) ,
3739 StringWithZeroBytes ( _) => write ! ( f, "String contains \\ 0 bytes" , ) ,
40+ OutOfRange => write ! ( f, "Value conversion failed - out of range" ) ,
3841 Internal ( e) => write ! ( f, "Value conversion failed - internal error: {}" , e) ,
3942 BigIntOverflow => write ! ( f, "BigInt overflow" ) ,
4043 UnexpectedType => write ! ( f, "Could not convert - received unexpected type" ) ,
Original file line number Diff line number Diff line change @@ -489,15 +489,33 @@ impl TryFrom<OwnedJsValue> for i32 {
489489 type Error = ValueError ;
490490
491491 fn try_from ( value : OwnedJsValue ) -> Result < Self , Self :: Error > {
492- value. to_int ( )
492+ if value. is_int ( ) {
493+ return value. to_int ( ) ;
494+ } else if value. is_float ( ) {
495+ let f = value. to_float ( ) ?;
496+ if f. fract ( ) != 0.0 {
497+ return Err ( ValueError :: UnexpectedType ) ;
498+ }
499+ if f < ( i32:: MIN as f64 ) || f > ( i32:: MAX as f64 ) {
500+ return Err ( ValueError :: OutOfRange ) ;
501+ }
502+ return Ok ( f as i32 ) ;
503+ }
504+ Err ( ValueError :: UnexpectedType )
493505 }
494506}
495507
496508impl TryFrom < OwnedJsValue > for f64 {
497509 type Error = ValueError ;
498510
499511 fn try_from ( value : OwnedJsValue ) -> Result < Self , Self :: Error > {
500- value. to_float ( )
512+ if value. is_float ( ) {
513+ return value. to_float ( ) ;
514+ } else if value. is_int ( ) {
515+ let i = value. to_int ( ) ?;
516+ return Ok ( i as f64 ) ;
517+ }
518+ Err ( ValueError :: UnexpectedType )
501519 }
502520}
503521
You can’t perform that action at this time.
0 commit comments