Skip to content

Commit

Permalink
fix: spec update (PR #7) - blocklist filtering in uppercase-only alph…
Browse files Browse the repository at this point in the history
…abet
  • Loading branch information
4kimov committed Aug 30, 2023
1 parent bb0db82 commit 60d9f76
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2023-06-15
toolchain: stable
override: true
components: rustfmt, clippy
- name: Set up cargo cache
Expand All @@ -33,12 +33,10 @@ jobs:
run: |
cargo install --locked cargo-deny || true
cargo install --locked cargo-outdated || true
cargo install --locked cargo-udeps || true
- name: Check
run: |
cargo deny check
cargo outdated --exit-code 1
cargo udeps
rm -rf ~/.cargo/advisory-db
- name: Test
run: cargo test --all
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

**v0.2.1:**
- Bug fix: spec update (PR #7): blocklist filtering in uppercase-only alphabet [[PR #7](https://github.com/sqids/sqids-spec/pull/7)]
- Updating Github Actions to use stable toolchain instead of nightly
- Cargo update

**v0.2.0:**
- Bug fix: test for decoding an invalid ID with a repeating reserved character
- Cargo update
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ description = "Generate YouTube-like ids from numbers."
repository = "https://github.com/sqids/sqids-rust"
documentation = "https://docs.rs/sqids"
homepage = "https://sqids.org/rust"
version = "0.2.0"
version = "0.2.1"
license = "MIT"
edition = "2021"
readme = "README.md"
keywords = ["ids", "encode", "sqids", "hashids"]

[dependencies]
derive_more = "0.99.17"
serde = "1.0.183"
serde_json = "1.0.104"
serde = "1.0.188"
serde_json = "1.0.105"
14 changes: 8 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,23 @@ impl Sqids {
return Err(Error::AlphabetUniqueCharacters);
}

if options.min_length < Self::min_value() as usize ||
options.min_length > options.alphabet.len()
if options.min_length < Self::min_value() as usize
|| options.min_length > options.alphabet.len()
{
return Err(Error::MinLength {
min: Self::min_value() as usize,
max: options.alphabet.len(),
});
}

let lowercase_alphabet: Vec<char> =
alphabet.iter().map(|c| c.to_ascii_lowercase()).collect();
let filtered_blocklist: HashSet<String> = options
.blocklist
.iter()
.filter_map(|word| {
let word = word.to_lowercase();
if word.len() >= 3 && word.chars().all(|c| alphabet.contains(&c)) {
if word.len() >= 3 && word.chars().all(|c| lowercase_alphabet.contains(&c)) {
Some(word)
} else {
None
Expand Down Expand Up @@ -244,9 +246,9 @@ impl Sqids {
}

if self.min_length > id.len() {
id = id[..1].to_string() +
&alphabet[..(self.min_length - id.len())].iter().collect::<String>() +
&id[1..]
id = id[..1].to_string()
+ &alphabet[..(self.min_length - id.len())].iter().collect::<String>()
+ &id[1..]
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/blocklist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,19 @@ fn match_against_short_blocklist_word() {

assert_eq!(sqids.decode(&sqids.encode(&[1000]).unwrap()), vec![1000]);
}

#[test]
fn blocklist_filtering_in_constructor() {
let sqids = Sqids::new(Some(Options::new(
Some("ABCDEFGHIJKLMNOPQRSTUVWXYZ".to_string()),
None,
Some(HashSet::from(["sqnmpn".to_owned()])), // lowercase blocklist in only-uppercase alphabet
)))
.unwrap();

let id = sqids.encode(&[1, 2, 3]).unwrap();
let numbers = sqids.decode(&id);

assert_eq!(id, "ULPBZGBM".to_string()); // without blocklist, would've been "SQNMPN"
assert_eq!(numbers, vec![1, 2, 3]);
}

0 comments on commit 60d9f76

Please sign in to comment.