#uuid #short #generate #npm-package #alphabet #parse #string

no-std short-uuid

A library to generate and parse short uuids

5 releases

new 0.1.4 Nov 16, 2024
0.1.3 Aug 29, 2024
0.1.2 Feb 23, 2024
0.1.1 Feb 23, 2024
0.1.0 Feb 23, 2024

#225 in Data structures

Download history 2893/week @ 2024-07-28 2522/week @ 2024-08-04 2766/week @ 2024-08-11 2313/week @ 2024-08-18 2943/week @ 2024-08-25 2277/week @ 2024-09-01 2531/week @ 2024-09-08 2315/week @ 2024-09-15 2378/week @ 2024-09-22 2253/week @ 2024-09-29 2429/week @ 2024-10-06 2558/week @ 2024-10-13 3006/week @ 2024-10-20 2636/week @ 2024-10-27 2655/week @ 2024-11-03 2641/week @ 2024-11-10

11,073 downloads per month
Used in apigen

MIT license

24KB
359 lines

short-uuid

Generate and translate standard UUIDs into shorter or just different formats and back.

A port of the JavaScript npm package short-uuid so big thanks to the author.

An example of short uuid string in default flickrBase58 alphabet:

mhvXdrZT4jP5T8vBxuvm75

Getting started

Install the package with cargo:

cargo add short-uuid

or add it to your Cargo.toml:

[dependencies]
short-uuid = "0.1.4"

Examples

Generate short uuidv4 encoded in flickrBase58 format:

use short_uuid::ShortUuid;

let shortened_uuid = ShortUuid::generate();

Generate short uuidv4 encoded in flickrBase58 format using macro:

use short_uuid::short;

let shortened_uuid = short!();

Generate short uuidv4 using custom alphabet:

use short_uuid::{ShortUuidCustom, CustomTranslator};

let custom_alphabet = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
let translator = CustomTranslator::new(custom_alphabet).unwrap();

let custom_short = ShortUuidCustom::generate(&translator);
let custom_short_string = custom_short.to_string();

Get shortened uuid from standard uuid:

use short_uuid::ShortUuid;
// create normal uuid v4
let uuid = uuid::Uuid::new_v4();

let short = ShortUuid::from_uuid(&uuid);

Get shortened uuid from standard uuid using custom alphabet:

use short_uuid::{ShortUuidCustom, CustomTranslator};

let custom_alphabet = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
let translator = CustomTranslator::new(custom_alphabet).unwrap();

let uuid = uuid::Uuid::new_v4();
let short_custom = ShortUuidCustom::from_uuid(&uuid, &translator);
let short_custom_string = short_custom.to_string();

Get shortened uuid from uuid string:

use short_uuid::ShortUuid;

let uuid_str = "3cfb46e7-c391-42ef-90b8-0c1d9508e752";
let short_uuid = ShortUuid::from_uuid_str(&uuid_str);

Get shortened uuid from uuid string using custom alphabet:

use short_uuid::{ShortUuidCustom, CustomTranslator};

let custom_alphabet = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
let translator = CustomTranslator::new(custom_alphabet).unwrap();

let uuid_str = "3cfb46e7-c391-42ef-90b8-0c1d9508e752";
let short_custom = ShortUuidCustom::from_uuid_str(&uuid_str, &translator).unwrap();
let short_custom_string = short_custom.to_string();

References

Dependencies

~225KB