Skip to content

Commit

Permalink
Export builder and error types from crate root
Browse files Browse the repository at this point in the history
  • Loading branch information
archer884 committed Sep 28, 2020
1 parent e8de043 commit c1b6a46
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "harsh"
version = "0.2.0"
version = "0.2.1"
edition = "2018"
description = "Hashids implementation for Rust"
readme = "README.md"
Expand All @@ -14,8 +14,8 @@ categories = ["encoding", "value-formatting", "web-programming"]
[dependencies]

[dev-dependencies]
criterion = "0.3"
quickcheck = "0.9"
criterion = "0.3.3"
quickcheck = "0.9.2"

[[bench]]
name = "benchmarks"
Expand Down
20 changes: 10 additions & 10 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::{error, fmt, result};
const DEFAULT_ALPHABET: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
const DEFAULT_SEPARATORS: &[u8] = b"cfhistuCFHISTU";

pub type Result<T, E = BuildHarshError> = result::Result<T, E>;
pub type Result<T, E = BuildError> = result::Result<T, E>;

/// Represents potential errors encountered during `Harsh` initialization.
#[derive(Clone, Debug)]
pub enum BuildHarshError {
pub enum BuildError {
/// Error returned when the provided alphabet has insufficient distinct elements
AlphabetLength,

Expand All @@ -19,7 +19,7 @@ pub enum BuildHarshError {
Separator,
}

impl fmt::Display for BuildHarshError {
impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
static ALPHABET_LENGTH_MESSAGE: &str =
"The provided alphabet does not contain enough unique characters";
Expand All @@ -29,16 +29,16 @@ impl fmt::Display for BuildHarshError {
"The provided separators contain a character not found in the alphabet";

match self {
BuildHarshError::AlphabetLength => write!(f, "{}", ALPHABET_LENGTH_MESSAGE),
BuildHarshError::IllegalCharacter(c) => {
BuildError::AlphabetLength => write!(f, "{}", ALPHABET_LENGTH_MESSAGE),
BuildError::IllegalCharacter(c) => {
write!(f, "{} ({})", ILLEGAL_CHARACTER_MESSAGE, c)
}
BuildHarshError::Separator => write!(f, "{}", SEPARATOR_MESSAGE),
BuildError::Separator => write!(f, "{}", SEPARATOR_MESSAGE),
}
}
}

impl error::Error for BuildHarshError {}
impl error::Error for BuildError {}

/// A builder used to configure and create a Harsh instance.
#[derive(Debug, Default)]
Expand Down Expand Up @@ -103,7 +103,7 @@ impl HarshBuilder {

let alphabet = unique_alphabet(&self.alphabet)?;
if alphabet.len() < MINIMUM_ALPHABET_LENGTH {
return Err(BuildHarshError::AlphabetLength);
return Err(BuildError::AlphabetLength);
}

let salt = self.salt.unwrap_or_else(Vec::new);
Expand Down Expand Up @@ -137,7 +137,7 @@ fn unique_alphabet(alphabet: &Option<Vec<u8>>) -> Result<Vec<u8>> {

for &item in alphabet {
if item == b' ' {
return Err(BuildHarshError::IllegalCharacter(item as char));
return Err(BuildError::IllegalCharacter(item as char));
}

if !reg.contains(&item) {
Expand All @@ -147,7 +147,7 @@ fn unique_alphabet(alphabet: &Option<Vec<u8>>) -> Result<Vec<u8>> {
}

if ret.len() < 16 {
Err(BuildHarshError::AlphabetLength)
Err(BuildError::AlphabetLength)
} else {
Ok(ret)
}
Expand Down
24 changes: 12 additions & 12 deletions src/harsh.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{builder::HarshBuilder, shuffle};
use std::{error, fmt, result, str};

type Result<T, E = HarshError> = result::Result<T, E>;
type Result<T, E = Error> = result::Result<T, E>;

#[derive(Clone, Debug)]
pub enum HarshError {
pub enum Error {
Hex,
Decode(DecodeError),
}
Expand All @@ -26,23 +26,23 @@ impl fmt::Display for DecodeError {

impl error::Error for DecodeError {}

impl fmt::Display for HarshError {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
HarshError::Hex => f.write_str("Failed to decode hex value"),
HarshError::Decode(e) => match e {
Error::Hex => f.write_str("Failed to decode hex value"),
Error::Decode(e) => match e {
DecodeError::Value => f.write_str("Found bad value"),
DecodeError::Hash => f.write_str("Malformed hashid"),
},
}
}
}

impl error::Error for HarshError {
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
HarshError::Hex => None,
HarshError::Decode(ref e) => Some(e),
Error::Hex => None,
Error::Decode(ref e) => Some(e),
}
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ impl Harsh {
}

if value.len() < 2 {
return Err(HarshError::Decode(DecodeError::Hash));
return Err(Error::Decode(DecodeError::Hash));
}

let mut alphabet = self.alphabet.clone();
Expand All @@ -203,12 +203,12 @@ impl Harsh {
.collect();

match result {
None => Err(HarshError::Decode(DecodeError::Value)),
None => Err(Error::Decode(DecodeError::Value)),
Some(result) => {
if self.encode(&result) == input.as_ref() {
Ok(result)
} else {
Err(HarshError::Decode(DecodeError::Hash))
Err(Error::Decode(DecodeError::Hash))
}
}
}
Expand All @@ -228,7 +228,7 @@ impl Harsh {

match values {
Some(values) => Ok(self.encode(&values)),
None => Err(HarshError::Hex),
None => Err(Error::Hex),
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
mod builder;
mod harsh;

pub use crate::harsh::Harsh;
pub use crate::builder::{BuildError, HarshBuilder};
pub use crate::harsh::{Error, Harsh};

fn shuffle(values: &mut [u8], salt: &[u8]) {
if salt.is_empty() {
Expand Down

0 comments on commit c1b6a46

Please sign in to comment.