diff --git a/Cargo.toml b/Cargo.toml index ef62f72..ab641e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -json = "0.12.4" +json = "^0.12.4" +num-bigint = "^0.4.3" \ No newline at end of file diff --git a/src/types/u256.rs b/src/types/u256.rs index bb6ca15..36da9a5 100644 --- a/src/types/u256.rs +++ b/src/types/u256.rs @@ -48,7 +48,14 @@ impl From<&str> for U256 { }; U256([high, low]) } else { - todo!("Decimal import"); + let digits = item.parse::().unwrap().to_u64_digits(); + assert!(digits.len() <= 4); + U256([ + u128::from(*digits.get(3).unwrap_or(&0)) << 64 + | u128::from(*digits.get(2).unwrap_or(&0)), + u128::from(*digits.get(1).unwrap_or(&0)) << 64 + | u128::from(*digits.get(0).unwrap_or(&0)), + ]) } } } @@ -149,4 +156,22 @@ mod test { U256::from(u128::MAX) + U256::from(1) ); } + + #[test] + fn from_decimal() { + assert_eq!(U256::from("0"), U256::from(0)); + assert_eq!(U256::from("10"), U256::from(10)); + assert_eq!( + U256::from("680564733841876926926749214863536422910"), + U256::from(u128::MAX) + U256::from(u128::MAX) + ); + assert_eq!( + U256::from("000680564733841876926926749214863536422910"), + U256::from(u128::MAX) + U256::from(u128::MAX) + ); + assert_eq!( + U256::from("340282366920938463463374607431768211456"), + U256::from(u128::MAX) + U256::from(1) + ); + } }