9 releases (3 major breaking)

3.0.0 Apr 14, 2024
2.1.0 Apr 14, 2024
2.0.0 Apr 4, 2024
1.0.0 Apr 2, 2024
0.1.0 Apr 2, 2024

#685 in Parser implementations

Download history 22/week @ 2024-07-22 47/week @ 2024-07-29 15/week @ 2024-08-05 45/week @ 2024-08-12 23/week @ 2024-08-19 31/week @ 2024-08-26 39/week @ 2024-09-02 67/week @ 2024-09-09 32/week @ 2024-09-16 65/week @ 2024-09-23 55/week @ 2024-09-30 20/week @ 2024-10-07 85/week @ 2024-10-14 83/week @ 2024-10-21 75/week @ 2024-10-28 53/week @ 2024-11-04

298 downloads per month
Used in tower-request-id-nano

MIT/Apache

47KB
623 lines

nid

CI status crates.io docs Apache 2.0 or MIT Licenses

Generate and parse Nano IDs.

Nano ID is a small, secure, URL-friendly, unique string ID. Here's an example of a Nano ID:

qjH-6uGrFy0QgNJtUh0_c

This crate is a Rust implementation of the original Nano ID library written in JavaScript. Please refer to the original library for the detailed explanation of Nano ID.

Getting started

Add the following to your Cargo.toml:

[dependencies]
nid = "3.0.0"

When you want a new Nano ID, you can generate one using the Nanoid::new method.

use nid::Nanoid;
let id: Nanoid = Nanoid::new();

You can parse a string into a Nano ID using Nanoid::try_from_str, std::str::FromStr or TryFrom<String>.

use nid::Nanoid;
let id: Nanoid = Nanoid::try_from_str("K8N4Q7MNmeHJ-OHHoVDcz")?;
let id: Nanoid = "3hYR3muA_xvjMrrrqFWxF".parse()?;
let id: Nanoid = "iH26rJ8CpRz-gfIh7TSRu".to_string().try_into()?;

If the Nano ID string is constant, you can also use the nanoid macro to parse it at compile time.

use nid::{nanoid, Nanoid};
let id = nanoid!("ClCrhcvy5kviH5ZozARfi");
const ID: Nanoid = nanoid!("9vZZWqFI_rTou3Mutq1LH");

The length of the Nano ID is 21 by default. You can change it by specifying the generic parameter.

use nid::Nanoid;
let id: Nanoid<10> = "j1-SOTHHxi".parse()?;

You can also use a different alphabet. The list of available alphabets is in the alphabet module.

use nid::{alphabet::Base62Alphabet, Nanoid};
let id: Nanoid<10, Base62Alphabet> = Nanoid::new();

Examples

use nid::{alphabet::Base62Alphabet, Nanoid};

// Generate a new Nano ID and print it.
let id: Nanoid = Nanoid::new();
println!("{}", id);

// Parse a string into a Nano ID and convert it back to a string.
let id: Nanoid = "abcdefg1234567UVWXYZ_".parse()?;
let s = id.to_string();

// Parse a string into a Nano ID with a different length and alphabet.
let id: Nanoid<9, Base62Alphabet> = "abc123XYZ".parse()?;

Features

Comparison with other implementations of Nano ID

nanoid and nano-id are other implementations of Nano ID in Rust. The main difference between nid and the other implementations is that nid has Nanoid type to represent Nano IDs. This type provides a safe way to generate and parse Nano IDs. This is similar to uuid crate, which provides Uuid type to represent UUIDs.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.5–1MB
~23K SLoC