Go to file
Ivan Akimov cf377536d0 wip
2023-07-18 14:32:39 -05:00
.github/workflows wip 2023-07-18 14:13:05 -05:00
src wip 2023-07-18 14:32:39 -05:00
tests wip 2023-07-18 14:32:39 -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 wip 2023-07-18 13:19:47 -05:00
CHANGELOG.md wip 2023-06-29 08:17:40 -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 wip 2023-07-18 14:32:39 -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::new(None)?;
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