Skip to content

Commit f8ebe75

Browse files
committed
Merge #116: Do random improvements
30e7e48 Clean up comments in private error module (Tobin C. Harding) 7ea5793 embedded: Run formatter (Tobin C. Harding) db157f9 Improve rustdoc (Tobin C. Harding) e4f16cf Implement error traits for gf32::Error (Tobin C. Harding) a2209d5 Remove crate level examples (Tobin C. Harding) fdea41f Remove incorrect todo (Tobin C. Harding) 2fa4fc5 Add formatter to git pre-commit hook (Tobin C. Harding) Pull request description: Pull all the unrelated random improvements out of #113 so that we can review/merge them more easily. Nothing controversial here except perhaps commit: `a2209d5 Remove crate level examples`. Note also that commit: `1a746b7 Clean up comments in private error module` is new. ACKs for top commit: apoelstra: ACK 30e7e48 Tree-SHA512: 896a025d29659c7305a2fb1d524915fe628dfdb97158381216e50d203730955b4c6aa350dcf26c8a70b18e39cb61e024646ea1303f4bd20a5015b71a464397f4
2 parents 2495b06 + 30e7e48 commit f8ebe75

File tree

6 files changed

+35
-52
lines changed

6 files changed

+35
-52
lines changed

Diff for: embedded/no-allocator/src/main.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
#![no_main]
88
#![no_std]
99

10-
use panic_halt as _;
11-
1210
use arrayvec::{ArrayString, ArrayVec};
13-
use bech32::{self, u5, ComboError, FromBase32, ToBase32, Variant, Hrp};
11+
use bech32::{self, u5, ComboError, FromBase32, Hrp, ToBase32, Variant};
1412
use cortex_m_rt::entry;
1513
use cortex_m_semihosting::{debug, hprintln};
14+
use panic_halt as _;
1615

1716
// Note: `#[global_allocator]` is NOT set.
1817

@@ -26,9 +25,7 @@ fn main() -> ! {
2625

2726
let hrp = Hrp::parse("bech32").unwrap();
2827

29-
bech32::encode_to_fmt_anycase(&mut encoded, hrp, &base32, Variant::Bech32)
30-
.unwrap()
31-
.unwrap();
28+
bech32::encode_to_fmt_anycase(&mut encoded, hrp, &base32, Variant::Bech32).unwrap().unwrap();
3229
test(&*encoded == "bech321qqqsyrhqy2a");
3330

3431
hprintln!("{}", encoded).unwrap();

Diff for: embedded/with-allocator/src/main.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
#![no_std]
44

55
extern crate alloc;
6-
use panic_halt as _;
7-
8-
use self::alloc::string::ToString;
9-
use self::alloc::vec;
10-
use self::alloc::vec::Vec;
116
use core::alloc::Layout;
127

138
use alloc_cortex_m::CortexMHeap;
14-
use bech32::{self, FromBase32, ToBase32, Variant, Hrp};
9+
use bech32::{self, FromBase32, Hrp, ToBase32, Variant};
1510
use cortex_m::asm;
1611
use cortex_m_rt::entry;
1712
use cortex_m_semihosting::{debug, hprintln};
13+
use panic_halt as _;
14+
15+
use self::alloc::string::ToString;
16+
use self::alloc::vec;
17+
use self::alloc::vec::Vec;
1818

1919
#[global_allocator]
2020
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
@@ -27,12 +27,7 @@ fn main() -> ! {
2727
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }
2828

2929
let hrp = Hrp::parse("bech32").unwrap();
30-
let encoded = bech32::encode(
31-
hrp,
32-
vec![0x00, 0x01, 0x02].to_base32(),
33-
Variant::Bech32,
34-
)
35-
.unwrap();
30+
let encoded = bech32::encode(hrp, vec![0x00, 0x01, 0x02].to_base32(), Variant::Bech32).unwrap();
3631
test(encoded == "bech321qqqsyrhqy2a".to_string());
3732

3833
hprintln!("{}", encoded).unwrap();

Diff for: githooks/pre-commit

+3
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ git diff-index --check --cached $against -- || exit 1
4747

4848
# Check that code lints cleanly.
4949
cargo clippy --all-features -- -D warnings || exit 1
50+
51+
# Check that there are no formatting issues.
52+
cargo +nightly fmt --check || exit 1

Diff for: src/error.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
// Written by the Rust Bitcoin developers.
21
// SPDX-License-Identifier: CC0-1.0
32

4-
// TODO: We can get this from `bitcoin-internals` crate once that is released.
5-
6-
//! # Error
7-
//!
8-
//! Error handling macros and helpers.
9-
//!
10-
113
/// Formats error.
124
///
135
/// If `std` feature is OFF appends error source (delimited by `: `). We do this because

Diff for: src/lib.rs

+3-24
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,6 @@
1111
//!
1212
//! The original description in [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki)
1313
//! has more details. See also [BIP-0350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki).
14-
//!
15-
#![cfg_attr(
16-
feature = "std",
17-
doc = "
18-
# Examples
19-
```
20-
use bech32::{self, FromBase32, ToBase32, Variant, Hrp};
21-
let hrp = Hrp::parse(\"bech32\").expect(\"bech32 is valid\");
22-
let encoded = bech32::encode(hrp, vec![0x00, 0x01, 0x02].to_base32(), Variant::Bech32).unwrap();
23-
assert_eq!(encoded, \"bech321qqqsyrhqy2a\".to_string());
24-
let (hrp, data, variant) = bech32::decode(&encoded).unwrap();
25-
assert_eq!(hrp.to_string(), \"bech32\");
26-
assert_eq!(Vec::<u8>::from_base32(&data).unwrap(), vec![0x00, 0x01, 0x02]);
27-
assert_eq!(variant, Variant::Bech32);
28-
```
29-
"
30-
)]
31-
//!
3214
3315
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
3416
// Experimental features we need.
@@ -452,9 +434,8 @@ pub fn encode_to_fmt_anycase<T: AsRef<[u5]>>(
452434
///
453435
/// This method is intended for implementing traits from [`std::fmt`].
454436
///
455-
/// # Errors
437+
/// # Deviations from standard.
456438
///
457-
/// * Deviations from standard.
458439
/// * No length limits are enforced for the data part.
459440
pub fn encode_without_checksum_to_fmt<T: AsRef<[u5]>>(
460441
fmt: &mut dyn fmt::Write,
@@ -502,9 +483,8 @@ impl Variant {
502483

503484
/// Encodes a bech32 payload to string.
504485
///
505-
/// # Errors
486+
/// # Deviations from standard.
506487
///
507-
/// * Deviations from standard.
508488
/// * No length limits are enforced for the data part.
509489
#[cfg(feature = "alloc")]
510490
pub fn encode<T: AsRef<[u5]>>(hrp: Hrp, data: T, variant: Variant) -> Result<String, Error> {
@@ -515,9 +495,8 @@ pub fn encode<T: AsRef<[u5]>>(hrp: Hrp, data: T, variant: Variant) -> Result<Str
515495

516496
/// Encodes a bech32 payload to string without the checksum.
517497
///
518-
/// # Errors
498+
/// # Deviations from standard.
519499
///
520-
/// * Deviations from standard.
521500
/// * No length limits are enforced for the data part.
522501
#[cfg(feature = "alloc")]
523502
pub fn encode_without_checksum<T: AsRef<[u5]>>(hrp: Hrp, data: T) -> Result<String, Error> {

Diff for: src/primitives/gf32.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use core::{fmt, num, ops};
1616
#[cfg(all(test, mutate))]
1717
use mutagen::mutate;
1818

19+
use crate::write_err;
20+
1921
/// Logarithm table of each bech32 element, as a power of alpha = Z.
2022
///
2123
/// Includes Q as 0 but this is false; you need to exclude Q because it has no discrete log. If we
@@ -317,12 +319,27 @@ pub enum Error {
317319
}
318320

319321
impl fmt::Display for Error {
320-
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { todo!() }
322+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
323+
use Error::*;
324+
325+
match *self {
326+
NotAByte(ref e) => write_err!(f, "invalid field element"; e),
327+
InvalidByte(ref b) => write!(f, "invalid byte in field element: {:#04x}", b),
328+
InvalidChar(ref c) => write!(f, "invalid char in field element: {}", c),
329+
}
330+
}
321331
}
322332

323333
#[cfg(feature = "std")]
324334
impl std::error::Error for Error {
325-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { todo!() }
335+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
336+
use Error::*;
337+
338+
match *self {
339+
NotAByte(ref e) => Some(e),
340+
InvalidByte(_) | InvalidChar(_) => None,
341+
}
342+
}
326343
}
327344

328345
impl From<num::TryFromIntError> for Error {

0 commit comments

Comments
 (0)