Skip to content

Commit

Permalink
Merge pull request #24 from archer884/docs/add-min-length-documentati…
Browse files Browse the repository at this point in the history
…on-for-alphabet

Add min length documentation for alphabet
  • Loading branch information
archer884 authored Oct 23, 2021
2 parents c1b6a46 + 6c21789 commit ad95798
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 50 deletions.
5 changes: 2 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.1"
version = "0.2.2"
edition = "2018"
description = "Hashids implementation for Rust"
readme = "README.md"
Expand All @@ -14,8 +14,7 @@ categories = ["encoding", "value-formatting", "web-programming"]
[dependencies]

[dev-dependencies]
criterion = "0.3.3"
quickcheck = "0.9.2"
criterion = "0.3.5"

[[bench]]
name = "benchmarks"
Expand Down
3 changes: 2 additions & 1 deletion examples/decode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use harsh::Harsh;
use std::{env, error::Error};

use harsh::Harsh;

fn main() -> Result<(), Box<dyn Error>> {
let harsh = Harsh::default();
let input = env::args().nth(1).expect("Wut?");
Expand Down
12 changes: 7 additions & 5 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{harsh::Harsh, shuffle};
use std::{error, fmt, result};

use crate::{harsh::Harsh, shuffle};

const DEFAULT_ALPHABET: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
const DEFAULT_SEPARATORS: &[u8] = b"cfhistuCFHISTU";

Expand All @@ -22,11 +23,11 @@ pub enum BuildError {
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";
"alphabet must include at least 16 characters";
static ILLEGAL_CHARACTER_MESSAGE: &str =
"The provided alphabet contains an illegal character";
"alphabet contains an illegal character";
static SEPARATOR_MESSAGE: &str =
"The provided separators contain a character not found in the alphabet";
"separators contain a character not found in the alphabet";

match self {
BuildError::AlphabetLength => write!(f, "{}", ALPHABET_LENGTH_MESSAGE),
Expand Down Expand Up @@ -72,7 +73,8 @@ impl HarshBuilder {
/// Provides an alphabet.
///
/// Note that this alphabet will be converted into a `[u8]` before use, meaning
/// that multi-byte utf8 character values should be avoided.
/// that multi-byte utf8 character values should be avoided. The alphabet must
/// include at least sixteen characters.
pub fn alphabet<T: Into<Vec<u8>>>(mut self, alphabet: T) -> HarshBuilder {
self.alphabet = Some(alphabet.into());
self
Expand Down
7 changes: 4 additions & 3 deletions src/harsh.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{builder::HarshBuilder, shuffle};
use std::{error, fmt, result, str};

use crate::{builder::HarshBuilder, shuffle};

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

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -134,7 +135,7 @@ impl Harsh {

if buffer.len() < self.hash_length {
let guard_index = (nhash as usize
+ buffer.bytes().nth(2).expect("hellfire and damnation") as usize)
+ buffer.as_bytes()[2] as usize)
% self.guards.len();
let guard = self.guards[guard_index];
buffer.push(guard as char);
Expand Down Expand Up @@ -186,7 +187,7 @@ impl Harsh {

let lottery = value[0];
let value = &value[1..];
let segments: Vec<_> = value.split(|u| self.separators.contains(u)).collect();
let segments = value.split(|u| self.separators.contains(u));

let result: Option<Vec<_>> = segments
.into_iter()
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@
mod builder;
mod harsh;

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

fn shuffle(values: &mut [u8], salt: &[u8]) {
if salt.is_empty() {
Expand Down
36 changes: 0 additions & 36 deletions tests/quickcheck.rs

This file was deleted.

0 comments on commit ad95798

Please sign in to comment.