Skip to content

Commit

Permalink
general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
4kimov committed Dec 21, 2024
1 parent c517814 commit 21db153
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 54 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# CHANGELOG

**v0.4.2:**
- Fix u64 overflow
- Fix u64 overflow panic [[PR #5](https://github.com/sqids/sqids-rust/pull/7)]
- Cargo update
- `cargo deny` update

**v0.4.1:**
- Derive `Clone` trait [[PR #6](https://github.com/sqids/sqids-rust/pull/6)]
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "README.md"
keywords = ["ids", "encode", "short", "sqids", "hashids"]

[dependencies]
derive_builder = "0.20.0"
serde = "1.0.197"
serde_json = "1.0.114"
thiserror = "1.0.57"
derive_builder = "0.20.2"
serde = "1.0.216"
serde_json = "1.0.133"
thiserror = "2.0.8"
43 changes: 17 additions & 26 deletions deny.toml
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
targets = [
{ triple = "x86_64-unknown-linux-gnu" },
{ triple = "aarch64-unknown-linux-gnu" },
{ triple = "x86_64-unknown-linux-musl" },
{ triple = "aarch64-apple-darwin" },
{ triple = "x86_64-apple-darwin" },
{ triple = "x86_64-pc-windows-msvc" },
]
[graph]
all-features = true

[advisories]
vulnerability = "deny"
unmaintained = "warn"
unsound = "deny"
yanked = "deny"
notice = "warn"
version = 2
ignore = []

[licenses]
unlicensed = "deny"
allow-osi-fsf-free = "either"
copyleft = "deny"
default = "warn"
unused-allowed-license = "deny"
confidence-threshold = 0.95
allow = []
exceptions = []
version = 2
allow = [
"MIT",
"Apache-2.0",
"Unicode-3.0"
]

[sources]
unknown-registry = "deny"
unknown-git = "deny"
exceptions = []

[bans]
multiple-versions = "allow"
wildcards = "deny"
highlight = "simplest-path"
skip-tree = []
deny = []

[sources]
unknown-registry = "deny"
unknown-git = "deny"
allow-registry = ["https://github.com/rust-lang/crates.io-index"]
allow-git = []
22 changes: 14 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,24 @@ impl Sqids {
}

fn to_number(&self, id: &str, alphabet: &[char]) -> Option<u64> {
let mut result = 0;
let mut result: u64 = 0;
let base = alphabet.len() as u64;

for c in id.chars() {
let idx = alphabet.iter().position(|&x| x == c).unwrap();
result = result * alphabet.len() as u128 + idx as u128;
}
let idx = alphabet.iter().position(|&x| x == c).unwrap() as u64;

if result <= u64::MAX.into() {
Some(result.try_into().unwrap())
} else {
None
if let Some(new_result) = result.checked_mul(base) {
if let Some(final_result) = new_result.checked_add(idx) {
result = final_result;
} else {
return None;
}
} else {
return None;
}
}

Some(result)
}

fn shuffle(alphabet: &[char]) -> Vec<char> {
Expand Down
15 changes: 0 additions & 15 deletions tests/decoding.rs

This file was deleted.

14 changes: 14 additions & 0 deletions tests/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,17 @@ fn decoding_invalid_character() {
let numbers: Vec<u64> = vec![];
assert_eq!(sqids.decode("*"), numbers);
}

#[test]
fn decoding_number_maximum_value() {
let sqids = Sqids::default();
let numbers = sqids.decode("ABARpJzdz9");
assert_eq!(numbers, [9_007_199_254_740_991]); // 2 ^ 53
}

#[test]
fn decoding_number_overflows() {
let sqids = Sqids::default();
let numbers = sqids.decode("0J4AEXRN106Z0"); // `https://github.com/sqids/sqids-rust/pull/7`
assert_eq!(numbers, Vec::<u64>::new());
}

0 comments on commit 21db153

Please sign in to comment.