We have a special type for wrapping integer parsing errors, use it. To test this I added the following tests: #[test] pub fn debug_absolute_error_conversion_height() { let invalid_height = LOCK_TIME_THRESHOLD + 1; let _ = Height::from_consensus(invalid_height).unwrap(); } #[test] pub fn debug_absolute_error_conversion_time() { let invalid_time = LOCK_TIME_THRESHOLD - 1; let _ = Time::from_consensus(invalid_time).unwrap(); } #[test] #[cfg(feature = "std")] pub fn debug_absolute_error_conversion_height_string() { let invalid_height = std::format!("{:x}", LOCK_TIME_THRESHOLD + 1); let _ = Height::from_hex(&invalid_height).unwrap(); } #[test] #[cfg(feature = "std")] pub fn debug_absolute_error_conversion_time_string() { let invalid_time = std::format!("{:x}", LOCK_TIME_THRESHOLD - 1); let _ = Time::from_hex(&invalid_time).unwrap(); } #[test] #[cfg(feature = "std")] pub fn debug_absolute_error_height_invalid_hex_string() { let _ = Height::from_hex("somerandomshit").unwrap(); } #[test] #[cfg(feature = "std")] pub fn debug_absolute_error_time_invalid_hex_string() { let _ = Time::from_hex("somerandomshit").unwrap(); } Which resulted in the following output Before: ---- locktime::absolute::tests::debug_absolute_error_conversion_height stdout ---- thread 'locktime::absolute::tests::debug_absolute_error_conversion_height' panicked at units/src/locktime/absolute.rs:431:56: called `Result::unwrap()` on an `Err` value: ConversionError { unit: Blocks, input: 500000001 } note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ---- locktime::absolute::tests::debug_absolute_error_conversion_height_string stdout ---- thread 'locktime::absolute::tests::debug_absolute_error_conversion_height_string' panicked at units/src/locktime/absolute.rs:444:51: called `Result::unwrap()` on an `Err` value: ParseHeightError(Conversion(500000001)) ---- locktime::absolute::tests::debug_absolute_error_conversion_time stdout ---- thread 'locktime::absolute::tests::debug_absolute_error_conversion_time' panicked at units/src/locktime/absolute.rs:437:52: called `Result::unwrap()` on an `Err` value: ConversionError { unit: Seconds, input: 499999999 } ---- locktime::absolute::tests::debug_absolute_error_height_invalid_hex_string stdout ---- thread 'locktime::absolute::tests::debug_absolute_error_height_invalid_hex_string' panicked at units/src/locktime/absolute.rs:457:52: called `Result::unwrap()` on an `Err` value: ParseHeightError(InvalidInteger { source: ParseIntError { kind: InvalidDigit }, input: "somerandomshit" }) ---- locktime::absolute::tests::debug_absolute_error_conversion_time_string stdout ---- thread 'locktime::absolute::tests::debug_absolute_error_conversion_time_string' panicked at units/src/locktime/absolute.rs:451:47: called `Result::unwrap()` on an `Err` value: ParseTimeError(Conversion(499999999)) ---- locktime::absolute::tests::debug_absolute_error_time_invalid_hex_string stdout ---- thread 'locktime::absolute::tests::debug_absolute_error_time_invalid_hex_string' panicked at units/src/locktime/absolute.rs:464:50: called `Result::unwrap()` on an `Err` value: ParseTimeError(InvalidInteger { source: ParseIntError { kind: InvalidDigit }, input: "somerandomshit" }) After: ---- locktime::absolute::tests::debug_absolute_error_conversion_height stdout ---- thread 'locktime::absolute::tests::debug_absolute_error_conversion_height' panicked at units/src/locktime/absolute.rs:432:56: called `Result::unwrap()` on an `Err` value: ConversionError { unit: Blocks, input: 500000001 } ---- locktime::absolute::tests::debug_absolute_error_conversion_height_string stdout ---- thread 'locktime::absolute::tests::debug_absolute_error_conversion_height_string' panicked at units/src/locktime/absolute.rs:445:51: called `Result::unwrap()` on an `Err` value: ParseHeightError(Conversion(500000001)) ---- locktime::absolute::tests::debug_absolute_error_conversion_time stdout ---- thread 'locktime::absolute::tests::debug_absolute_error_conversion_time' panicked at units/src/locktime/absolute.rs:438:52: called `Result::unwrap()` on an `Err` value: ConversionError { unit: Seconds, input: 499999999 } ---- locktime::absolute::tests::debug_absolute_error_conversion_time_string stdout ---- thread 'locktime::absolute::tests::debug_absolute_error_conversion_time_string' panicked at units/src/locktime/absolute.rs:452:47: called `Result::unwrap()` on an `Err` value: ParseTimeError(Conversion(499999999)) ---- locktime::absolute::tests::debug_absolute_error_height_invalid_hex_string stdout ---- thread 'locktime::absolute::tests::debug_absolute_error_height_invalid_hex_string' panicked at units/src/locktime/absolute.rs:458:52: called `Result::unwrap()` on an `Err` value: ParseHeightError(ParseInt(ParseIntError { input: "somerandomshit", bits: 32, is_signed: false, source: ParseIntError { kind: InvalidDigit } })) ---- locktime::absolute::tests::debug_absolute_error_time_invalid_hex_string stdout ---- thread 'locktime::absolute::tests::debug_absolute_error_time_invalid_hex_string' panicked at units/src/locktime/absolute.rs:465:50: called `Result::unwrap()` on an `Err` value: ParseTimeError(ParseInt(ParseIntError { input: "somerandomshit", bits: 32, is_signed: false, source: ParseIntError { kind: InvalidDigit } })) |
||
---|---|---|
.. | ||
contrib | ||
src | ||
CHANGELOG.md | ||
Cargo.toml | ||
README.md |
README.md
Bitcoin Units
This crate provides basic Bitcoin numeric units such as Amount
.
Minimum Supported Rust Version (MSRV)
This library should always compile with any combination of features on Rust 1.63.0.
Licensing
The code in this project is licensed under the Creative Commons CC0 1.0 Universal license. We use the SPDX license list and SPDX IDs.