Skip to content

Commit

Permalink
Use thiserror to handle display and from, release 0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
h4sh3d committed Mar 19, 2021
1 parent b16021c commit 8cc7306
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 46 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "base58-monero"
description = "Library with support for encoding/decoding Monero base58 strings."
keywords = ["monero", "base58", "base58-check"]
version = "0.2.0"
version = "0.2.1"
authors = ["h4sh3d <[email protected]>"]
license = "MIT"
documentation = "https://docs.rs/base58-monero"
Expand All @@ -27,6 +27,7 @@ tiny-keccak = { version = "2.0.1", features = ["keccak"], optional = true }
async-stream = { version = "0.2.0", optional = true }
tokio = { version = "0.2.6", features = ["stream", "io-util"], optional = true }
futures-util = { version = "0.3.1", optional = true }
thiserror = "1.0.24"

[dev-dependencies]
hex = "0.3"
Expand Down
55 changes: 11 additions & 44 deletions src/base58.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ use tokio::io::AsyncReadExt;
#[cfg(feature = "stream")]
use tokio::stream::Stream;

use std::fmt::Display;
use thiserror::Error;

#[cfg(feature = "stream")]
use std::io;
use std::num::Wrapping;
use std::{error, fmt};

/// Base58 alphabet, does not contains visualy similar characters
pub const BASE58_CHARS: &[u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
Expand All @@ -93,51 +93,25 @@ pub const FULL_ENCODED_BLOCK_SIZE: usize = ENCODED_BLOCK_SIZES[FULL_BLOCK_SIZE];
pub const CHECKSUM_SIZE: usize = 4;

/// Possible errors when encoding/decoding base58 and base58-check strings
#[derive(Error, Debug)]
pub enum Error {
/// Invalid block size, must be `1..=8`
#[error("Invalid block size error")]
InvalidBlockSize,
/// Symbol not in base58 alphabet
#[error("Invalid symbol error")]
InvalidSymbol,
#[cfg(feature = "check")]
/// Invalid 4-bytes checksum
#[cfg(feature = "check")]
#[error("Invalid checksum error")]
InvalidChecksum,
/// Decoding overflow
#[error("Overflow error")]
Overflow,
#[cfg(feature = "stream")]
/// IO error on stream
Io(io::Error),
}

impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::InvalidBlockSize => write!(f, "invalid block size"),
Error::InvalidSymbol => write!(f, "invalid symbol"),
#[cfg(feature = "check")]
Error::InvalidChecksum => write!(f, "invalid checksum"),
Error::Overflow => write!(f, "overflow"),
#[cfg(feature = "stream")]
Error::Io(e) => write!(f, "overflow, {}", e),
}
}
}

impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self)
}
}

impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::InvalidBlockSize | Error::InvalidSymbol | Error::Overflow => None,
#[cfg(feature = "check")]
Error::InvalidChecksum => None,
#[cfg(feature = "stream")]
Error::Io(e) => Some(e),
}
}
#[cfg(feature = "stream")]
#[error("IO error: {0}")]
Io(#[from] io::Error),
}

impl PartialEq for Error {
Expand Down Expand Up @@ -170,13 +144,6 @@ impl PartialEq for Error {
}
}

#[cfg(feature = "stream")]
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}

/// Utility type for handling results with base58 error type
pub type Result<T> = std::result::Result<T, Error>;

Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
//! ```
#![recursion_limit = "256"]

// Coding conventions
#![forbid(unsafe_code)]

Expand Down

0 comments on commit 8cc7306

Please sign in to comment.