Skip to content

Commit 21db153

Browse files
committed
general cleanup
1 parent c517814 commit 21db153

File tree

6 files changed

+52
-54
lines changed

6 files changed

+52
-54
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# CHANGELOG
22

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

68
**v0.4.1:**
79
- Derive `Clone` trait [[PR #6](https://github.com/sqids/sqids-rust/pull/6)]

Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ readme = "README.md"
1111
keywords = ["ids", "encode", "short", "sqids", "hashids"]
1212

1313
[dependencies]
14-
derive_builder = "0.20.0"
15-
serde = "1.0.197"
16-
serde_json = "1.0.114"
17-
thiserror = "1.0.57"
14+
derive_builder = "0.20.2"
15+
serde = "1.0.216"
16+
serde_json = "1.0.133"
17+
thiserror = "2.0.8"

deny.toml

+17-26
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
1-
targets = [
2-
{ triple = "x86_64-unknown-linux-gnu" },
3-
{ triple = "aarch64-unknown-linux-gnu" },
4-
{ triple = "x86_64-unknown-linux-musl" },
5-
{ triple = "aarch64-apple-darwin" },
6-
{ triple = "x86_64-apple-darwin" },
7-
{ triple = "x86_64-pc-windows-msvc" },
8-
]
1+
[graph]
2+
all-features = true
93

104
[advisories]
11-
vulnerability = "deny"
12-
unmaintained = "warn"
13-
unsound = "deny"
14-
yanked = "deny"
15-
notice = "warn"
5+
version = 2
166
ignore = []
177

188
[licenses]
19-
unlicensed = "deny"
20-
allow-osi-fsf-free = "either"
21-
copyleft = "deny"
22-
default = "warn"
23-
unused-allowed-license = "deny"
24-
confidence-threshold = 0.95
25-
allow = []
26-
exceptions = []
9+
version = 2
10+
allow = [
11+
"MIT",
12+
"Apache-2.0",
13+
"Unicode-3.0"
14+
]
2715

28-
[sources]
29-
unknown-registry = "deny"
30-
unknown-git = "deny"
16+
exceptions = []
3117

3218
[bans]
3319
multiple-versions = "allow"
3420
wildcards = "deny"
35-
highlight = "simplest-path"
36-
skip-tree = []
21+
deny = []
22+
23+
[sources]
24+
unknown-registry = "deny"
25+
unknown-git = "deny"
26+
allow-registry = ["https://github.com/rust-lang/crates.io-index"]
27+
allow-git = []

src/lib.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,24 @@ impl Sqids {
335335
}
336336

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

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

345-
if result <= u64::MAX.into() {
346-
Some(result.try_into().unwrap())
347-
} else {
348-
None
344+
if let Some(new_result) = result.checked_mul(base) {
345+
if let Some(final_result) = new_result.checked_add(idx) {
346+
result = final_result;
347+
} else {
348+
return None;
349+
}
350+
} else {
351+
return None;
352+
}
349353
}
354+
355+
Some(result)
350356
}
351357

352358
fn shuffle(alphabet: &[char]) -> Vec<char> {

tests/decoding.rs

-15
This file was deleted.

tests/encoding.rs

+14
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,17 @@ fn decoding_invalid_character() {
118118
let numbers: Vec<u64> = vec![];
119119
assert_eq!(sqids.decode("*"), numbers);
120120
}
121+
122+
#[test]
123+
fn decoding_number_maximum_value() {
124+
let sqids = Sqids::default();
125+
let numbers = sqids.decode("ABARpJzdz9");
126+
assert_eq!(numbers, [9_007_199_254_740_991]); // 2 ^ 53
127+
}
128+
129+
#[test]
130+
fn decoding_number_overflows() {
131+
let sqids = Sqids::default();
132+
let numbers = sqids.decode("0J4AEXRN106Z0"); // `https://github.com/sqids/sqids-rust/pull/7`
133+
assert_eq!(numbers, Vec::<u64>::new());
134+
}

0 commit comments

Comments
 (0)