Implement Error for base58 error type

This commit is contained in:
Andrew Poelstra 2015-10-25 10:16:05 -05:00
parent 3491c5057e
commit 34edf48b93
1 changed files with 29 additions and 0 deletions

View File

@ -14,6 +14,8 @@
//! # Base58 encoder and decoder //! # Base58 encoder and decoder
use std::{error, fmt};
use byteorder::{ByteOrder, LittleEndian, WriteBytesExt}; use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
use util::hash::Sha256dHash; use util::hash::Sha256dHash;
@ -34,6 +36,33 @@ pub enum Error {
Other(String) Other(String)
} }
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::BadByte(b) => write!(f, "invalid base58 character 0x{:x}", b),
Error::BadChecksum(exp, actual) => write!(f, "base58ck checksum 0x{:x} does not match expected 0x{:x}", actual, exp),
Error::InvalidLength(ell) => write!(f, "length {} invalid for this base58 type", ell),
Error::InvalidVersion(ref v) => write!(f, "version {:?} invalid for this base58 type", v),
Error::TooShort(_) => write!(f, "base58ck data not even long enough for a checksum"),
Error::Other(ref s) => f.write_str(s)
}
}
}
impl error::Error for Error {
fn cause(&self) -> Option<&error::Error> { None }
fn description(&self) -> &'static str {
match *self {
Error::BadByte(_) => "invalid b58 character",
Error::BadChecksum(_, _) => "invalid b58ck checksum",
Error::InvalidLength(_) => "invalid length for b58 type",
Error::InvalidVersion(_) => "invalid version for b58 type",
Error::TooShort(_) => "b58ck data less than 4 bytes",
Error::Other(_) => "unknown b58 error"
}
}
}
static BASE58_CHARS: &'static [u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; static BASE58_CHARS: &'static [u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
static BASE58_DIGITS: [Option<u8>; 128] = [ static BASE58_DIGITS: [Option<u8>; 128] = [