Go to file
Ivan Akimov 9cbe68f3d1 bump
2023-08-11 20:23:04 -05:00
.github/workflows wip 2023-07-20 15:11:17 -05:00
src check for an invalid id with a repeating reserved character 2023-08-11 20:21:59 -05:00
tests check for an invalid id with a repeating reserved character 2023-08-11 20:21:59 -05:00
.clippy.toml wip 2023-07-18 14:32:39 -05:00
.gitattributes wip 2023-07-18 14:32:39 -05:00
.gitignore wip 2023-07-17 17:15:56 -05:00
Cargo.toml bump 2023-08-11 20:23:04 -05:00
CHANGELOG.md check for an invalid id with a repeating reserved character 2023-08-11 20:21:59 -05:00
deny.toml wip 2023-07-18 13:19:47 -05:00
LICENSE wip 2023-06-29 08:17:40 -05:00
README.md readme 2023-07-20 20:51:06 -05:00
release.toml wip 2023-07-18 14:32:39 -05:00
rustfmt.toml wip 2023-07-18 14:32:39 -05:00

Sqids Rust

Github Actions

Sqids (pronounced "squids") is a small library that lets you generate YouTube-looking IDs from numbers. It's good for link shortening, fast & URL-safe ID generation and decoding back into numbers for quicker database lookups.

Getting started

Add using cargo:

cargo add sqids

Examples

Simple encode & decode:

let sqids = Sqids::default();
let id = sqids.encode(&[1, 2, 3])?; // "8QRLaD"
let numbers = sqids.decode(&id); // [1, 2, 3]

Randomize IDs by providing a custom alphabet:

let sqids = Sqids::new(Some(Options::new(
  Some("FxnXM1kBN6cuhsAvjW3Co7l2RePyY8DwaU04Tzt9fHQrqSVKdpimLGIJOgb5ZE".to_string()),
  None,
  None,
)))?;
let id = sqids.encode(&[1, 2, 3])?; // "B5aMa3"
let numbers = sqids.decode(&id); // [1, 2, 3]

Enforce a minimum length for IDs:

let sqids = Sqids::new(Some(Options::new(
  None,
  Some(10),
  None,
)))?;
let id = sqids.encode(&[1, 2, 3])?; // "75JT1cd0dL"
let numbers = sqids.decode(&id); // [1, 2, 3]

Prevent specific words from appearing anywhere in the auto-generated IDs:

let sqids = Sqids::new(Some(Options::new(
  None,
  None,
  Some(HashSet::from(["word1".to_string(), "word2".to_string()])),
)))?;
let id = sqids.encode(&[1, 2, 3])?; // "8QRLaD"
let numbers = sqids.decode(&id); // [1, 2, 3]

License

MIT