Skip to content

Commit db46f45

Browse files
committed
Deprecates Int::as_i32 and created new functions as_i32_or_nothing, as_i32_or_fail, and Int::to_str plus added tests and some new comments
1 parent 9948df4 commit db46f45

File tree

2 files changed

+100
-5
lines changed

2 files changed

+100
-5
lines changed

rust/src/metadata.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ mod tests {
930930
assert_eq!(map.get_str("sender_id").unwrap().as_text().unwrap(), "jkfdsufjdk34h3Sdfjdhfduf873");
931931
assert_eq!(map.get_str("comment").unwrap().as_text().unwrap(), "happy birthday");
932932
let tags = map.get_str("tags").unwrap().as_list().unwrap();
933-
let tags_i32 = tags.0.iter().map(|md| md.as_int().unwrap().as_i32().unwrap()).collect::<Vec<i32>>();
933+
let tags_i32 = tags.0.iter().map(|md| md.as_int().unwrap().as_i32_or_fail().unwrap()).collect::<Vec<i32>>();
934934
assert_eq!(tags_i32, vec![0, 264, -1024, 32]);
935935
let output_str = decode_metadatum_to_json_str(&metadata, MetadataJsonSchema::NoConversions).expect("decode failed");
936936
let input_json: serde_json::Value = serde_json::from_str(&input_str).unwrap();
@@ -991,11 +991,11 @@ mod tests {
991991
fn json_encoding_check_example_metadatum(metadata: &TransactionMetadatum) {
992992
let map = metadata.as_map().unwrap();
993993
assert_eq!(map.get(&TransactionMetadatum::new_bytes(hex::decode("8badf00d").unwrap()).unwrap()).unwrap().as_bytes().unwrap(), hex::decode("deadbeef").unwrap());
994-
assert_eq!(map.get_i32(9).unwrap().as_int().unwrap().as_i32().unwrap(), 5);
994+
assert_eq!(map.get_i32(9).unwrap().as_int().unwrap().as_i32_or_fail().unwrap(), 5);
995995
let inner_map = map.get_str("obj").unwrap().as_map().unwrap();
996996
let a = inner_map.get_str("a").unwrap().as_list().unwrap();
997997
let a1 = a.get(0).as_map().unwrap();
998-
assert_eq!(a1.get_i32(5).unwrap().as_int().unwrap().as_i32().unwrap(), 2);
998+
assert_eq!(a1.get_i32(5).unwrap().as_int().unwrap().as_i32_or_fail().unwrap(), 2);
999999
let a2 = a.get(1).as_map().unwrap();
10001000
assert_eq!(a2.keys().len(), 0);
10011001
}
@@ -1025,11 +1025,11 @@ mod tests {
10251025

10261026
let map = metadata.as_map().unwrap();
10271027
let key = map.keys().get(0);
1028-
assert_eq!(map.get(&key).unwrap().as_int().unwrap().as_i32().unwrap(), 5);
1028+
assert_eq!(map.get(&key).unwrap().as_int().unwrap().as_i32_or_fail().unwrap(), 5);
10291029
let key_list = key.as_list().unwrap();
10301030
assert_eq!(key_list.len(), 2);
10311031
let key_map = key_list.get(0).as_map().unwrap();
1032-
assert_eq!(key_map.get_i32(5).unwrap().as_int().unwrap().as_i32().unwrap(), -7);
1032+
assert_eq!(key_map.get_i32(5).unwrap().as_int().unwrap().as_i32_or_fail().unwrap(), -7);
10331033
assert_eq!(key_map.get_str("hello").unwrap().as_text().unwrap(), "world");
10341034
let key_bytes = key_list.get(1).as_bytes().unwrap();
10351035
assert_eq!(key_bytes, hex::decode("ff00ff00").unwrap());

rust/src/utils.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

512551
impl 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

Comments
 (0)