Skip to content

Commit

Permalink
Merge #72: Implement std::error::Error::source
Browse files Browse the repository at this point in the history
5b520d0 Implement std::error::Error::source (Tobin C. Harding)
6052c60 Add full stops to error rustdocs (Tobin C. Harding)

Pull request description:

  Now that we have MSRV of 1.41.1 we can implement `source` to improve the ergonomics of our errors.

  Patch 1 is preparatory cleanup.

ACKs for top commit:
  apoelstra:
    ACK 5b520d0

Tree-SHA512: e39b90295c3b030090a79c82f7c3bb94aae0e97600fcb54092979e5320cffd490ee54af8af95f104d092a9ba8bffec2b2101c17e91ffd4494593462ddf1c95c6
  • Loading branch information
apoelstra committed Aug 22, 2022
2 parents d12513f + 5b520d0 commit ae9f18a
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,22 +659,22 @@ const GEN: [u32; 5] = [
0x2a14_62b3,
];

/// Error types for Bech32 encoding / decoding
/// Error types for Bech32 encoding / decoding.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Error {
/// String does not contain the separator character
/// String does not contain the separator character.
MissingSeparator,
/// The checksum does not match the rest of the data
/// The checksum does not match the rest of the data.
InvalidChecksum,
/// The data or human-readable part is too long or too short
/// The data or human-readable part is too long or too short.
InvalidLength,
/// Some part of the string contains an invalid character
/// Some part of the string contains an invalid character.
InvalidChar(char),
/// Some part of the data has an invalid value
/// Some part of the data has an invalid value.
InvalidData(u8),
/// The bit conversion failed due to a padding issue
/// The bit conversion failed due to a padding issue.
InvalidPadding,
/// The whole string must be of one case
/// The whole string must be of one case.
MixedCase,
}

Expand All @@ -692,17 +692,14 @@ impl fmt::Display for Error {
}
}

#[cfg(any(feature = "std", test))]
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn description(&self) -> &str {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use self::Error::*;

match *self {
Error::MissingSeparator => "missing human-readable separator",
Error::InvalidChecksum => "invalid checksum",
Error::InvalidLength => "invalid length",
Error::InvalidChar(_) => "invalid character",
Error::InvalidData(_) => "invalid data point",
Error::InvalidPadding => "invalid padding",
Error::MixedCase => "mixed-case strings not allowed",
MissingSeparator | InvalidChecksum | InvalidLength | InvalidChar(_)
| InvalidData(_) | InvalidPadding | MixedCase => None,
}
}
}
Expand Down

0 comments on commit ae9f18a

Please sign in to comment.