@@ -487,6 +487,12 @@ impl Int {
487487 return self . 0 >= 0
488488 }
489489
490+ /// BigNum can only contain unsigned u64 values
491+ ///
492+ /// This function will return the BigNum representation
493+ /// only in case the underlying i128 value is positive.
494+ ///
495+ /// Otherwise nothing will be returned (undefined).
490496 pub fn as_positive ( & self ) -> Option < BigNum > {
491497 if self . is_positive ( ) {
492498 Some ( to_bignum ( self . 0 as u64 ) )
@@ -495,6 +501,12 @@ impl Int {
495501 }
496502 }
497503
504+ /// BigNum can only contain unsigned u64 values
505+ ///
506+ /// This function will return the *absolute* BigNum representation
507+ /// only in case the underlying i128 value is negative.
508+ ///
509+ /// Otherwise nothing will be returned (undefined).
498510 pub fn as_negative ( & self ) -> Option < BigNum > {
499511 if !self . is_positive ( ) {
500512 Some ( to_bignum ( ( -self . 0 ) as u64 ) )
@@ -503,10 +515,37 @@ impl Int {
503515 }
504516 }
505517
518+ /// !!! DEPRECATED !!!
519+ /// Returns an i32 value in case the underlying original i128 value is within the limits.
520+ /// Otherwise will just return an empty value (undefined).
521+ #[ deprecated(
522+ since = "10.0.0" ,
523+ note = "Unsafe ignoring of possible boundary error and it's not clear from the function name. Use `as_i32_or_nothing`, `as_i32_or_fail`, or `to_str`"
524+ ) ]
506525 pub fn as_i32 ( & self ) -> Option < i32 > {
526+ self . as_i32_or_nothing ( )
527+ }
528+
529+ /// Returns the underlying value converted to i32 if possible (within limits)
530+ /// Otherwise will just return an empty value (undefined).
531+ pub fn as_i32_or_nothing ( & self ) -> Option < i32 > {
507532 use std:: convert:: TryFrom ;
508533 i32:: try_from ( self . 0 ) . ok ( )
509534 }
535+
536+ /// Returns the underlying value converted to i32 if possible (within limits)
537+ /// JsError in case of out of boundary overflow
538+ pub fn as_i32_or_fail ( & self ) -> Result < i32 , JsError > {
539+ use std:: convert:: TryFrom ;
540+ i32:: try_from ( self . 0 )
541+ . map_err ( |e| JsError :: from_str ( & format ! ( "{}" , e) ) )
542+ }
543+
544+ /// Returns string representation of the underlying i128 value directly.
545+ /// Might contain the minus sign (-) in case of negative value.
546+ pub fn to_str ( & self ) -> String {
547+ format ! ( "{}" , self . 0 )
548+ }
510549}
511550
512551impl cbor_event:: se:: Serialize for Int {
@@ -2192,4 +2231,60 @@ mod tests {
21922231 Bip32PublicKey :: from_bytes( & hex:: decode( self_key_hex) . unwrap( ) ) . unwrap( ) . to_raw_key( ) . hash( )
21932232 ) ;
21942233 }
2234+
2235+ #[ test]
2236+ fn int_to_str ( ) {
2237+ assert_eq ! ( Int :: new( & BigNum ( u64 :: max_value( ) ) ) . to_str( ) , u64 :: max_value( ) . to_string( ) ) ;
2238+ assert_eq ! ( Int :: new( & BigNum ( u64 :: min_value( ) ) ) . to_str( ) , u64 :: min_value( ) . to_string( ) ) ;
2239+ assert_eq ! ( Int :: new_negative( & BigNum ( u64 :: max_value( ) ) ) . to_str( ) , ( -( u64 :: max_value( ) as i128 ) ) . to_string( ) ) ;
2240+ assert_eq ! ( Int :: new_negative( & BigNum ( u64 :: min_value( ) ) ) . to_str( ) , ( -( u64 :: min_value( ) as i128 ) ) . to_string( ) ) ;
2241+ assert_eq ! ( Int :: new_i32( 142 ) . to_str( ) , "142" ) ;
2242+ assert_eq ! ( Int :: new_i32( -142 ) . to_str( ) , "-142" ) ;
2243+ }
2244+
2245+ #[ test]
2246+ fn int_as_i32_or_nothing ( ) {
2247+
2248+ let over_pos_i32 = ( i32:: max_value ( ) as i64 ) + 1 ;
2249+ assert ! ( Int :: new( & BigNum ( over_pos_i32 as u64 ) ) . as_i32_or_nothing( ) . is_none( ) ) ;
2250+
2251+ let valid_pos_i32 = ( i32:: max_value ( ) as i64 ) ;
2252+ assert_eq ! ( Int :: new( & BigNum ( valid_pos_i32 as u64 ) ) . as_i32_or_nothing( ) . unwrap( ) , i32 :: max_value( ) ) ;
2253+
2254+ let over_neg_i32 = ( i32:: min_value ( ) as i64 ) - 1 ;
2255+ assert ! ( Int :: new_negative( & BigNum ( ( -over_neg_i32) as u64 ) ) . as_i32_or_nothing( ) . is_none( ) ) ;
2256+
2257+ let valid_neg_i32 = ( i32:: min_value ( ) as i64 ) ;
2258+ assert_eq ! ( Int :: new_negative( & BigNum ( ( -valid_neg_i32) as u64 ) ) . as_i32_or_nothing( ) . unwrap( ) , i32 :: min_value( ) ) ;
2259+
2260+ assert ! ( Int :: new( & BigNum ( u64 :: max_value( ) ) ) . as_i32_or_nothing( ) . is_none( ) ) ;
2261+ assert_eq ! ( Int :: new( & BigNum ( i32 :: max_value( ) as u64 ) ) . as_i32_or_nothing( ) . unwrap( ) , i32 :: max_value( ) ) ;
2262+ assert_eq ! ( Int :: new_negative( & BigNum ( i32 :: max_value( ) as u64 ) ) . as_i32_or_nothing( ) . unwrap( ) , -i32 :: max_value( ) ) ;
2263+
2264+ assert_eq ! ( Int :: new_i32( 42 ) . as_i32_or_nothing( ) . unwrap( ) , 42 ) ;
2265+ assert_eq ! ( Int :: new_i32( -42 ) . as_i32_or_nothing( ) . unwrap( ) , -42 ) ;
2266+ }
2267+
2268+ #[ test]
2269+ fn int_as_i32_or_fail ( ) {
2270+
2271+ let over_pos_i32 = ( i32:: max_value ( ) as i64 ) + 1 ;
2272+ assert ! ( Int :: new( & BigNum ( over_pos_i32 as u64 ) ) . as_i32_or_fail( ) . is_err( ) ) ;
2273+
2274+ let valid_pos_i32 = ( i32:: max_value ( ) as i64 ) ;
2275+ assert_eq ! ( Int :: new( & BigNum ( valid_pos_i32 as u64 ) ) . as_i32_or_fail( ) . unwrap( ) , i32 :: max_value( ) ) ;
2276+
2277+ let over_neg_i32 = ( i32:: min_value ( ) as i64 ) - 1 ;
2278+ assert ! ( Int :: new_negative( & BigNum ( ( -over_neg_i32) as u64 ) ) . as_i32_or_fail( ) . is_err( ) ) ;
2279+
2280+ let valid_neg_i32 = ( i32:: min_value ( ) as i64 ) ;
2281+ assert_eq ! ( Int :: new_negative( & BigNum ( ( -valid_neg_i32) as u64 ) ) . as_i32_or_fail( ) . unwrap( ) , i32 :: min_value( ) ) ;
2282+
2283+ assert ! ( Int :: new( & BigNum ( u64 :: max_value( ) ) ) . as_i32_or_fail( ) . is_err( ) ) ;
2284+ assert_eq ! ( Int :: new( & BigNum ( i32 :: max_value( ) as u64 ) ) . as_i32_or_fail( ) . unwrap( ) , i32 :: max_value( ) ) ;
2285+ assert_eq ! ( Int :: new_negative( & BigNum ( i32 :: max_value( ) as u64 ) ) . as_i32_or_fail( ) . unwrap( ) , -i32 :: max_value( ) ) ;
2286+
2287+ assert_eq ! ( Int :: new_i32( 42 ) . as_i32_or_fail( ) . unwrap( ) , 42 ) ;
2288+ assert_eq ! ( Int :: new_i32( -42 ) . as_i32_or_fail( ) . unwrap( ) , -42 ) ;
2289+ }
21952290}
0 commit comments