Skip to content

Commit

Permalink
Use Rust convention for expect
Browse files Browse the repository at this point in the history
The `expect` string is supposed to describe what we expect not describe
the error case.

Use Rust convention for `expect` strings.
  • Loading branch information
tcharding committed Oct 25, 2023
1 parent 48d7eb6 commit 3771167
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 35 deletions.
26 changes: 13 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//!
//! // Encode arbitrary data using "abc" as the human-readable part and append a bech32m checksum.
//! let hrp = Hrp::parse("abc").expect("valid hrp");
//! let string = bech32::encode::<Bech32m>(hrp, &DATA).expect("failed to encode string");
//! let string = bech32::encode::<Bech32m>(hrp, &DATA).expect("valid data");
//! assert_eq!(string, STRING);
//!
//! // Encode arbitrary data as a Bitcoin taproot address.
Expand All @@ -46,7 +46,7 @@
//!
//! // No-alloc: Encode without allocating (ignoring that String::new() allocates :).
//! let mut buf = String::new();
//! bech32::encode_to_fmt::<Bech32m, String>(&mut buf, hrp, &DATA).expect("failed to encode to buffer");
//! bech32::encode_to_fmt::<Bech32m, String>(&mut buf, hrp, &DATA).expect("valid data");
//! assert_eq!(buf, STRING);
//! # }
//! ```
Expand All @@ -67,7 +67,7 @@
//! // The input address MUST include a valid bech32 or bech32m checksum, for individual specific
//! // checksum algorithms see [`decode_bech32`], [`decode_bech32m`], [`decode_no_checksum`] or use
//! // the [`primitives::decode::CheckedHrpstring`] type directly.
//! let (hrp, data) = bech32::decode(&STRING).expect("failed to decode");
//! let (hrp, data) = bech32::decode(&STRING).expect("valid string");
//! assert_eq!(hrp, Hrp::parse("abc").unwrap());
//! assert_eq!(data, DATA);
//!
Expand All @@ -76,7 +76,7 @@
//! assert_eq!(program, DATA);
//!
//! // No-alloc: Decode a bech32m checksummed address without allocating.
//! let p = CheckedHrpstring::new::<Bech32m>(&STRING).expect("failed to parse string");
//! let p = CheckedHrpstring::new::<Bech32m>(&STRING).expect("valid string");
//! assert_eq!(hrp, p.hrp());
//! assert!(p.byte_iter().eq(DATA.iter().map(|&b| b))); // We yield bytes not references.
//!
Expand Down Expand Up @@ -189,7 +189,7 @@ pub use {
///
/// // You can control the checksum algorithm directly by using the [`CheckedHrpstring`] type.
/// let p = CheckedHrpstring::new::<Bech32>(&BECH32).expect("valid bech32 string with valid bech32 checksum");
/// let p = CheckedHrpstring::new::<Bech32m>(&BECH32M).expect("valid bech32 string with valid bech32 checksum");
/// let p = CheckedHrpstring::new::<Bech32m>(&BECH32M).expect("valid bech32 string with valid bech32m checksum");
/// let p = CheckedHrpstring::new::<NoChecksum>(&NO_CHECKSUM).expect("valid bech32 string with no checksum");
/// # }
/// ```
Expand Down Expand Up @@ -404,15 +404,15 @@ mod tests {
#[test]
fn encode_bech32m() {
let hrp = Hrp::parse_unchecked("test");
let got = encode::<Bech32m>(hrp, &DATA).expect("failed to encode");
let got = encode::<Bech32m>(hrp, &DATA).expect("valid data");
let want = "test1lu08d6qejxtdg4y5r3zarvary0c5xw7kmz4lky";
assert_eq!(got, want);
}

#[test]
fn encode_bech32_lower() {
let hrp = Hrp::parse_unchecked("test");
let got = encode_lower::<Bech32>(hrp, &DATA).expect("failed to encode");
let got = encode_lower::<Bech32>(hrp, &DATA).expect("valid data");
let want = "test1lu08d6qejxtdg4y5r3zarvary0c5xw7kw79nnx";
assert_eq!(got, want);
}
Expand All @@ -422,7 +422,7 @@ mod tests {
fn encode_bech32_lower_to_writer() {
let hrp = Hrp::parse_unchecked("test");
let mut buf = Vec::new();
encode_lower_to_writer::<Bech32, _>(&mut buf, hrp, &DATA).expect("failed to encode");
encode_lower_to_writer::<Bech32, _>(&mut buf, hrp, &DATA).expect("valid data");

let got = std::str::from_utf8(&buf).expect("ascii is valid utf8");
let want = "test1lu08d6qejxtdg4y5r3zarvary0c5xw7kw79nnx";
Expand All @@ -432,7 +432,7 @@ mod tests {
#[test]
fn encode_bech32_upper() {
let hrp = Hrp::parse_unchecked("test");
let got = encode_upper::<Bech32>(hrp, &DATA).expect("failed to encode");
let got = encode_upper::<Bech32>(hrp, &DATA).expect("valid data");
let want = "TEST1LU08D6QEJXTDG4Y5R3ZARVARY0C5XW7KW79NNX";
assert_eq!(got, want);
}
Expand All @@ -442,7 +442,7 @@ mod tests {
fn encode_bech32_upper_to_writer() {
let hrp = Hrp::parse_unchecked("test");
let mut buf = Vec::new();
encode_upper_to_writer::<Bech32, _>(&mut buf, hrp, &DATA).expect("failed to encode");
encode_upper_to_writer::<Bech32, _>(&mut buf, hrp, &DATA).expect("valid data");

let got = std::str::from_utf8(&buf).expect("ascii is valid utf8");
let want = "TEST1LU08D6QEJXTDG4Y5R3ZARVARY0C5XW7KW79NNX";
Expand All @@ -452,7 +452,7 @@ mod tests {
#[test]
fn decode_bech32m() {
let s = "test1lu08d6qejxtdg4y5r3zarvary0c5xw7kmz4lky";
let (hrp, data) = decode(s).expect("failed to encode");
let (hrp, data) = decode(s).expect("valid data");

assert_eq!(hrp, Hrp::parse_unchecked("test"));
assert_eq!(data, DATA);
Expand All @@ -461,7 +461,7 @@ mod tests {
#[test]
fn decode_bech32_lower() {
let s = "test1lu08d6qejxtdg4y5r3zarvary0c5xw7kw79nnx";
let (hrp, data) = decode(s).expect("failed to encode");
let (hrp, data) = decode(s).expect("valid data");

assert_eq!(hrp, Hrp::parse_unchecked("test"));
assert_eq!(data, DATA);
Expand All @@ -470,7 +470,7 @@ mod tests {
#[test]
fn decode_bech32_upper() {
let s = "TEST1LU08D6QEJXTDG4Y5R3ZARVARY0C5XW7KW79NNX";
let (hrp, data) = decode(s).expect("failed to encode");
let (hrp, data) = decode(s).expect("valid data");

assert_eq!(hrp, Hrp::parse_unchecked("TEST"));
assert_eq!(data, DATA);
Expand Down
6 changes: 3 additions & 3 deletions src/primitives/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ mod tests {
#[test]
fn check_hrp_uppercase_returns_lower() {
let addr = "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4";
let unchecked = UncheckedHrpstring::new(addr).expect("failed to parse address");
let unchecked = UncheckedHrpstring::new(addr).expect("valid address");
assert_eq!(unchecked.hrp(), Hrp::parse_unchecked("bc"));
}

Expand All @@ -942,9 +942,9 @@ mod tests {
"an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio";

let hrp = Hrp::parse_unchecked(hrps);
let s = crate::encode::<Bech32>(hrp, &[]).expect("failed to encode empty buffer");
let s = crate::encode::<Bech32>(hrp, &[]).expect("valid data");

let unchecked = UncheckedHrpstring::new(&s).expect("failed to parse address");
let unchecked = UncheckedHrpstring::new(&s).expect("valid string");
assert_eq!(unchecked.hrp(), hrp);
}

Expand Down
4 changes: 2 additions & 2 deletions src/primitives/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//!
//! // Get a stream of characters representing the bech32 encoded
//! // address using "bc" for the human-readable part.
//! let hrp = Hrp::parse("bc").expect("bc is valid hrp string");
//! let hrp = Hrp::parse("bc").expect("a valid hrp string");
//! let chars = witness_prog
//! .iter()
//! .copied()
Expand Down Expand Up @@ -59,7 +59,7 @@ use crate::{Checksum, Fe32};
///
/// let data = [0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4];
///
/// let hrp = Hrp::parse("abc").expect("bc is valid hrp string");
/// let hrp = Hrp::parse("abc").expect("a valid hrp string");
/// let chars = data
/// .iter()
/// .copied()
Expand Down
4 changes: 2 additions & 2 deletions src/primitives/gf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@ mod tests {

#[test]
fn from_upper_char() {
let lower = Fe32::from_char('q').expect("failed to create fe32 from lowercase ascii char");
let upper = Fe32::from_char('Q').expect("failed to create fe32 from uppercase ascii char");
let lower = Fe32::from_char('q').expect("a valid lowercase ascii char");
let upper = Fe32::from_char('Q').expect("a valid uppercase ascii char");

assert_eq!(lower, upper);
}
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/hrp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ mod tests {
$(
#[test]
fn $test_name() {
let hrp = Hrp::parse($hrp).expect(&format!("failed to parse hrp {}", $hrp));
let hrp = Hrp::parse($hrp).expect(&format!("a valid hrp {}", $hrp));

// Test ByteIter forwards.
for (got, want) in hrp.byte_iter().zip($hrp.bytes()) {
Expand Down
20 changes: 9 additions & 11 deletions src/segwit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//!
//! // Decode a Bitcoin bech32 segwit address.
//! let address = "bc1q2s3rjwvam9dt2ftt4sqxqjf3twav0gdx0k0q2etxflx38c3x8tnssdmnjq";
//! let (hrp, witness_version, witness_program) = segwit::decode(address).expect("failed to decode address");
//! let (hrp, witness_version, witness_program) = segwit::decode(address).expect("valid address");
//! # }
//! ```
//!
Expand Down Expand Up @@ -68,7 +68,7 @@ pub use {
/// ```
/// use bech32::segwit;
/// let address = "bc1py3m7vwnghyne9gnvcjw82j7gqt2rafgdmlmwmqnn3hvcmdm09rjqcgrtxs";
/// let (_hrp, _witness_version, _witness_program) = segwit::decode(address).expect("failed to decode address");
/// let (_hrp, _witness_version, _witness_program) = segwit::decode(address).expect("valid address");
/// ```
#[cfg(feature = "alloc")]
#[inline]
Expand Down Expand Up @@ -356,8 +356,8 @@ mod tests {
];

for address in addresses {
let (hrp, version, program) = decode(address).expect("failed to decode valid address");
let encoded = encode(hrp, version, &program).expect("failed to encode address");
let (hrp, version, program) = decode(address).expect("valid address");
let encoded = encode(hrp, version, &program).expect("valid data");
assert_eq!(encoded, address);
}
}
Expand All @@ -373,8 +373,7 @@ mod tests {
fn encode_lower_to_fmt() {
let program = witness_program();
let mut address = String::new();
encode_to_fmt_unchecked(&mut address, hrp::BC, VERSION_0, &program)
.expect("failed to encode address to QR code");
encode_to_fmt_unchecked(&mut address, hrp::BC, VERSION_0, &program).expect("valid data");

let want = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4";
assert_eq!(address, want);
Expand All @@ -385,7 +384,7 @@ mod tests {
let program = witness_program();
let mut address = String::new();
encode_upper_to_fmt_unchecked(&mut address, hrp::BC, VERSION_0, &program)
.expect("failed to encode address to QR code");
.expect("valid data");

let want = "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4";
assert_eq!(address, want);
Expand All @@ -397,7 +396,7 @@ mod tests {
let program = witness_program();
let mut buf = Vec::new();
encode_lower_to_writer_unchecked(&mut buf, hrp::BC, VERSION_0, &program)
.expect("failed to encode");
.expect("valid data");

let address = std::str::from_utf8(&buf).expect("ascii is valid utf8");
let want = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4";
Expand All @@ -410,7 +409,7 @@ mod tests {
let program = witness_program();
let mut buf = Vec::new();
encode_upper_to_writer_unchecked(&mut buf, hrp::BC, VERSION_0, &program)
.expect("failed to encode");
.expect("valid data");

let address = std::str::from_utf8(&buf).expect("ascii is valid utf8");
let want = "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4";
Expand All @@ -423,8 +422,7 @@ mod tests {
let program = witness_program();
let mut buf = Vec::new();
let hrp = Hrp::parse_unchecked("BC");
encode_lower_to_writer_unchecked(&mut buf, hrp, VERSION_0, &program)
.expect("failed to encode");
encode_lower_to_writer_unchecked(&mut buf, hrp, VERSION_0, &program).expect("valid data");

let address = std::str::from_utf8(&buf).expect("ascii is valid utf8");
let want = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4";
Expand Down
2 changes: 1 addition & 1 deletion tests/bip_173_test_vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ macro_rules! check_valid_address_roundtrip {
// tested by the test vectors. However when BIP-350 came into effect only witness
// version 0 uses bech32 (and this is enforced by encode/decode).
if let Ok((hrp, bech32::Fe32::Q, program)) = bech32::segwit::decode($addr) {
let encoded = bech32::segwit::encode_v0(hrp, &program).expect("failed to encode address");
let encoded = bech32::segwit::encode_v0(hrp, &program).expect("valid data");
// The bips specifically say that encoder should output lowercase characters so we uppercase manually.
if encoded != $addr {
let got = encoded.to_uppercase();
Expand Down
4 changes: 2 additions & 2 deletions tests/bip_350_test_vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ macro_rules! check_valid_address_roundtrip {
#[test]
#[cfg(feature = "alloc")]
fn $test_name() {
let (hrp, version, program) = bech32::segwit::decode($addr).expect("failed to decode valid address");
let encoded = bech32::segwit::encode(hrp, version, &program).expect("failed to encode address");
let (hrp, version, program) = bech32::segwit::decode($addr).expect("valid address");
let encoded = bech32::segwit::encode(hrp, version, &program).expect("valid data");

// The bips specifically say that encoder should output lowercase characters so we uppercase manually.
if encoded != $addr {
Expand Down

0 comments on commit 3771167

Please sign in to comment.