Skip to content

Commit 2e06b36

Browse files
committed
Merge #154: Do a bunch of cleanups
8426386 Use string not address in lib.rs (Tobin C. Harding) c9aa166 Use NoData instead of MissingWitnessVersion (Tobin C. Harding) 2eac8e6 Fix colum width typo in rustdoc (Tobin C. Harding) Pull request description: This is the first 4 patches from #142, pushing up separately because that PR is quite complicated to review well and these cleanups are unrelated. ACKs for top commit: apoelstra: ACK 8426386 Tree-SHA512: 154bce12df068e80f17aa3fa5096390ce43eafd5efb9ed2319e0e26767ebb178b729f268bdb2a76c8a709c867d226d67522999da31dbb6f0bc30eb7106c0be9e
2 parents d60e09d + 8426386 commit 2e06b36

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

src/lib.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
//! use bech32::{hrp, segwit, Hrp, Bech32m};
3333
//!
3434
//! const DATA: [u8; 20] = [0xab; 20]; // Arbitrary data to be encoded.
35-
//! const ADDR: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu";
35+
//! const STRING: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu";
3636
//! const TAP_ADDR: &str = "bc1p4w46h2at4w46h2at4w46h2at4w46h2at5kreae";
3737
//!
3838
//! // Encode arbitrary data using "abc" as the human-readable part and append a bech32m checksum.
3939
//! let hrp = Hrp::parse("abc").expect("valid hrp");
40-
//! let address = bech32::encode::<Bech32m>(hrp, &DATA).expect("failed to encode address");
41-
//! assert_eq!(address, ADDR);
40+
//! let string = bech32::encode::<Bech32m>(hrp, &DATA).expect("failed to encode string");
41+
//! assert_eq!(string, STRING);
4242
//!
4343
//! // Encode arbitrary data as a Bitcoin taproot address.
4444
//! let taproot_address = segwit::encode(hrp::BC, segwit::VERSION_1, &DATA).expect("valid witness version and program");
@@ -47,7 +47,7 @@
4747
//! // No-alloc: Encode without allocating (ignoring that String::new() allocates :).
4848
//! let mut buf = String::new();
4949
//! bech32::encode_to_fmt::<Bech32m, String>(&mut buf, hrp, &DATA).expect("failed to encode to buffer");
50-
//! assert_eq!(buf, ADDR);
50+
//! assert_eq!(buf, STRING);
5151
//! # }
5252
//! ```
5353
//!
@@ -59,15 +59,15 @@
5959
//! use bech32::{hrp, segwit, Hrp, Bech32m};
6060
//!
6161
//! const DATA: [u8; 20] = [0xab; 20]; // Arbitrary data to be encoded.
62-
//! const ADDR: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu";
62+
//! const STRING: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu";
6363
//! const TAP_ADDR: &str = "bc1p4w46h2at4w46h2at4w46h2at4w46h2at5kreae";
6464
//!
6565
//! // Decode a bech32 encoded string that includes a bech32/bech32m checksum.
6666
//! //
6767
//! // The input address MUST include a valid bech32 or bech32m checksum, for individual specific
6868
//! // checksum algorithms see [`decode_bech32`], [`decode_bech32m`], [`decode_no_checksum`] or use
6969
//! // the [`primitives::decode::CheckedHrpstring`] type directly.
70-
//! let (hrp, data) = bech32::decode(&ADDR).expect("failed to decode");
70+
//! let (hrp, data) = bech32::decode(&STRING).expect("failed to decode");
7171
//! assert_eq!(hrp, Hrp::parse("abc").unwrap());
7272
//! assert_eq!(data, DATA);
7373
//!
@@ -76,7 +76,7 @@
7676
//! assert_eq!(program, DATA);
7777
//!
7878
//! // No-alloc: Decode a bech32m checksummed address without allocating.
79-
//! let p = CheckedHrpstring::new::<Bech32m>(&ADDR).expect("failed to parse address");
79+
//! let p = CheckedHrpstring::new::<Bech32m>(&STRING).expect("failed to parse string");
8080
//! assert_eq!(hrp, p.hrp());
8181
//! assert!(p.byte_iter().eq(DATA.iter().map(|&b| b))); // We yield bytes not references.
8282
//!
@@ -166,7 +166,8 @@ pub use {
166166
/// If this function succeeds the input string was found to be well formed (hrp, separator, bech32
167167
/// characters), and to have either a valid bech32m checksum or a valid bech32 checksum.
168168
///
169-
/// If your input string has no checksum use the [`CheckedHrpstring`] constructor, which allows selecting the checksum algorithm explicitly.
169+
/// If your input string has no checksum use the [`CheckedHrpstring`] constructor, which allows
170+
/// selecting the checksum algorithm explicitly.
170171
///
171172
/// # Returns
172173
///
@@ -182,14 +183,14 @@ pub use {
182183
/// const BECH32M: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu";
183184
/// const NO_CHECKSUM: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at";
184185
///
185-
/// let (hrp, data) = decode(&BECH32).expect("valid address with valid bech32 checksum");
186-
/// let (hrp, data) = decode(&BECH32M).expect("valid address with valid bech32m checksum");
186+
/// let (hrp, data) = decode(&BECH32).expect("valid bech32 string with valid bech32 checksum");
187+
/// let (hrp, data) = decode(&BECH32M).expect("valid bech32 string with valid bech32m checksum");
187188
/// assert!(decode(&NO_CHECKSUM).is_err());
188189
///
189190
/// // You can control the checksum algorithm directly by using the [`CheckedHrpstring`] type.
190-
/// let p = CheckedHrpstring::new::<Bech32>(&BECH32).expect("valid address with valid bech32 checksum");
191-
/// let p = CheckedHrpstring::new::<Bech32m>(&BECH32M).expect("valid address with valid bech32 checksum");
192-
/// let p = CheckedHrpstring::new::<NoChecksum>(&NO_CHECKSUM).expect("valid address with no checksum");
191+
/// let p = CheckedHrpstring::new::<Bech32>(&BECH32).expect("valid bech32 string with valid bech32 checksum");
192+
/// let p = CheckedHrpstring::new::<Bech32m>(&BECH32M).expect("valid bech32 string with valid bech32 checksum");
193+
/// let p = CheckedHrpstring::new::<NoChecksum>(&NO_CHECKSUM).expect("valid bech32 string with no checksum");
193194
/// # }
194195
/// ```
195196
#[cfg(feature = "alloc")]
@@ -344,7 +345,7 @@ pub fn encode_upper_to_writer<Ck: Checksum, W: std::io::Write>(
344345
Ok(())
345346
}
346347

347-
/// An error while decoding an address.
348+
/// An error while decoding a bech32 string.
348349
#[cfg(feature = "alloc")]
349350
#[derive(Debug, Clone, PartialEq, Eq)]
350351
#[non_exhaustive]

src/primitives/decode.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl<'s> CheckedHrpstring<'s> {
271271
#[inline]
272272
pub fn validate_segwit(mut self) -> Result<SegwitHrpstring<'s>, SegwitHrpstringError> {
273273
if self.data.is_empty() {
274-
return Err(SegwitHrpstringError::MissingWitnessVersion);
274+
return Err(SegwitHrpstringError::NoData);
275275
}
276276
// Unwrap ok since check_characters checked the bech32-ness of this char.
277277
let witness_version = Fe32::from_char(self.data[0].into()).unwrap();
@@ -371,7 +371,7 @@ impl<'s> SegwitHrpstring<'s> {
371371
let unchecked = UncheckedHrpstring::new(s)?;
372372

373373
if unchecked.data.is_empty() {
374-
return Err(SegwitHrpstringError::MissingWitnessVersion);
374+
return Err(SegwitHrpstringError::NoData);
375375
}
376376

377377
// Unwrap ok since check_characters (in `Self::new`) checked the bech32-ness of this char.
@@ -549,8 +549,8 @@ where
549549
pub enum SegwitHrpstringError {
550550
/// Error while parsing the encoded address string.
551551
Unchecked(UncheckedHrpstringError),
552-
/// The witness version byte is missing.
553-
MissingWitnessVersion,
552+
/// No data found after removing the checksum.
553+
NoData,
554554
/// Invalid witness version (must be 0-16 inclusive).
555555
InvalidWitnessVersion(Fe32),
556556
/// Invalid padding on the witness data.
@@ -567,7 +567,7 @@ impl fmt::Display for SegwitHrpstringError {
567567

568568
match *self {
569569
Unchecked(ref e) => write_err!(f, "parsing unchecked hrpstring failed"; e),
570-
MissingWitnessVersion => write!(f, "the witness version byte is missing"),
570+
NoData => write!(f, "no data found after removing the checksum"),
571571
InvalidWitnessVersion(fe) => write!(f, "invalid segwit witness version: {}", fe),
572572
Padding(ref e) => write_err!(f, "invalid padding on the witness data"; e),
573573
WitnessLength(ref e) => write_err!(f, "invalid witness length"; e),
@@ -586,7 +586,7 @@ impl std::error::Error for SegwitHrpstringError {
586586
Padding(ref e) => Some(e),
587587
WitnessLength(ref e) => Some(e),
588588
Checksum(ref e) => Some(e),
589-
MissingWitnessVersion | InvalidWitnessVersion(_) => None,
589+
NoData | InvalidWitnessVersion(_) => None,
590590
}
591591
}
592592
}

0 commit comments

Comments
 (0)