use core::fmt;
use internals::write_err;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Decode(InvalidCharacterError),
IncorrectChecksum(IncorrectChecksumError),
TooShort(TooShortError),
}
internals::impl_from_infallible!(Error);
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Error::*;
match *self {
Decode(ref e) => write_err!(f, "decode"; e),
IncorrectChecksum(ref e) => write_err!(f, "incorrect checksum"; e),
TooShort(ref e) => write_err!(f, "too short"; e),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use Error::*;
match *self {
Decode(ref e) => Some(e),
IncorrectChecksum(ref e) => Some(e),
TooShort(ref e) => Some(e),
}
}
}
impl From<InvalidCharacterError> for Error {
#[inline]
fn from(e: InvalidCharacterError) -> Self { Self::Decode(e) }
}
impl From<IncorrectChecksumError> for Error {
#[inline]
fn from(e: IncorrectChecksumError) -> Self { Self::IncorrectChecksum(e) }
}
impl From<TooShortError> for Error {
#[inline]
fn from(e: TooShortError) -> Self { Self::TooShort(e) }
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncorrectChecksumError {
pub(super) incorrect: u32,
pub(super) expected: u32,
}
impl IncorrectChecksumError {
pub fn incorrect_checksum(&self) -> (u32, u32) { (self.incorrect, self.expected) }
}
impl fmt::Display for IncorrectChecksumError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"base58 checksum {:#x} does not match expected {:#x}",
self.incorrect, self.expected
)
}
}
#[cfg(feature = "std")]
impl std::error::Error for IncorrectChecksumError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TooShortError {
pub(super) length: usize,
}
impl TooShortError {
pub fn invalid_base58_length(&self) -> usize { self.length }
}
impl fmt::Display for TooShortError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"base58 decoded data was not long enough, must be at least 4 byte: {}",
self.length
)
}
}
#[cfg(feature = "std")]
impl std::error::Error for TooShortError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidCharacterError {
pub(super) invalid: u8,
}
impl InvalidCharacterError {
pub fn invalid_base58_character(&self) -> u8 { self.invalid }
}
impl fmt::Display for InvalidCharacterError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid base58 character {:#x}", self.invalid)
}
}
#[cfg(feature = "std")]
impl std::error::Error for InvalidCharacterError {}