From 56f69550a41e3e41bc8b3aa71f4b90e6678e1ada Mon Sep 17 00:00:00 2001 From: J/A Date: Fri, 22 Oct 2021 16:21:35 -0500 Subject: [PATCH 1/5] Add min length documentation for alphabet --- src/builder.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/builder.rs b/src/builder.rs index f87da53..6d471fd 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -72,7 +72,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>>(mut self, alphabet: T) -> HarshBuilder { self.alphabet = Some(alphabet.into()); self From d651cae22af1d376b33fa08e41b595fa530d022e Mon Sep 17 00:00:00 2001 From: J/A Date: Fri, 22 Oct 2021 16:24:13 -0500 Subject: [PATCH 2/5] Update version/error message --- Cargo.toml | 2 +- src/builder.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 244b509..cf4fb87 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/builder.rs b/src/builder.rs index 6d471fd..a832e4a 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -22,11 +22,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), From efb963ea3784d14558484c4def4c2cfbbcef8554 Mon Sep 17 00:00:00 2001 From: J/A Date: Fri, 22 Oct 2021 16:30:43 -0500 Subject: [PATCH 3/5] Clippy scouting Clippy recommended I avoid an apprently needless call to .collect(), which seems like it would be a substantial performance increase. --- Cargo.toml | 2 +- src/harsh.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cf4fb87..9f04491 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ categories = ["encoding", "value-formatting", "web-programming"] [dev-dependencies] criterion = "0.3.3" -quickcheck = "0.9.2" +quickcheck = "1.0.3" [[bench]] name = "benchmarks" diff --git a/src/harsh.rs b/src/harsh.rs index c2c5326..acd136c 100644 --- a/src/harsh.rs +++ b/src/harsh.rs @@ -134,7 +134,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); @@ -186,7 +186,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> = segments .into_iter() From 090ee4381331d465bf72582fd24fa1db14bf15d3 Mon Sep 17 00:00:00 2001 From: J/A Date: Fri, 22 Oct 2021 16:37:13 -0500 Subject: [PATCH 4/5] Formatting conventions --- examples/decode.rs | 3 ++- src/builder.rs | 3 ++- src/harsh.rs | 3 ++- src/lib.rs | 6 ++++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/decode.rs b/examples/decode.rs index c2e4d54..2893f87 100644 --- a/examples/decode.rs +++ b/examples/decode.rs @@ -1,6 +1,7 @@ -use harsh::Harsh; use std::{env, error::Error}; +use harsh::Harsh; + fn main() -> Result<(), Box> { let harsh = Harsh::default(); let input = env::args().nth(1).expect("Wut?"); diff --git a/src/builder.rs b/src/builder.rs index a832e4a..c198879 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -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"; diff --git a/src/harsh.rs b/src/harsh.rs index acd136c..0d5e49e 100644 --- a/src/harsh.rs +++ b/src/harsh.rs @@ -1,6 +1,7 @@ -use crate::{builder::HarshBuilder, shuffle}; use std::{error, fmt, result, str}; +use crate::{builder::HarshBuilder, shuffle}; + type Result = result::Result; #[derive(Clone, Debug)] diff --git a/src/lib.rs b/src/lib.rs index c0967b0..b926f0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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() { From 6c21789f526ffcf7456a38dba9892425e4b8c18b Mon Sep 17 00:00:00 2001 From: J/A Date: Sat, 23 Oct 2021 02:26:34 -0500 Subject: [PATCH 5/5] Remove quickcheck tests These don't like the CI implementation. --- Cargo.toml | 3 +-- tests/quickcheck.rs | 36 ------------------------------------ 2 files changed, 1 insertion(+), 38 deletions(-) delete mode 100644 tests/quickcheck.rs diff --git a/Cargo.toml b/Cargo.toml index 9f04491..2d75f39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,7 @@ categories = ["encoding", "value-formatting", "web-programming"] [dependencies] [dev-dependencies] -criterion = "0.3.3" -quickcheck = "1.0.3" +criterion = "0.3.5" [[bench]] name = "benchmarks" diff --git a/tests/quickcheck.rs b/tests/quickcheck.rs deleted file mode 100644 index 8a02263..0000000 --- a/tests/quickcheck.rs +++ /dev/null @@ -1,36 +0,0 @@ -#[macro_use] -extern crate quickcheck; - -use harsh::Harsh; -use quickcheck::TestResult; - -quickcheck! { - fn decode_no_panic(encoded: String) -> () { - let harsh = Harsh::default(); - let _ = harsh.decode(encoded); - } -} - -quickcheck! { - fn encode_always_decodable(numbers: Vec) -> TestResult { - if numbers.is_empty() { - return TestResult::discard(); - } - let harsh = Harsh::default(); - let encoded = harsh.encode(&numbers); - harsh.decode(encoded).expect("Unable to decode value"); - TestResult::passed() - } -} - -quickcheck! { - fn min_length_always_met(numbers: Vec, min_length: usize) -> TestResult { - if numbers.is_empty() { - return TestResult::discard(); - } - let harsh = Harsh::builder().length(min_length).build().expect("Unable to create harsh"); - let encoded = harsh.encode(&numbers); - assert!(encoded.len() >= min_length); - TestResult::passed() - } -}