pub struct Nanoid<const N: usize = 21, A: Alphabet = Base64UrlAlphabet> { /* private fields */ }
Expand description
A Nano ID.
§Generic parameters
N
: The length of the Nano ID. The default is21
.A
: The alphabet used in the Nano ID. The default isBase64UrlAlphabet
.
§Generating
When you want a new Nano ID, you can generate one using the Nanoid::new
.
use nid::Nanoid;
let id: Nanoid = Nanoid::new();
§Parsing
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 you try to parse an invalid Nano ID, you will get an error.
use nid::{Nanoid, ParseError};
let result: Result<Nanoid, _> = "61psxw-too_short".parse();
assert!(matches!(result, Err(ParseError::InvalidLength { .. })));
let result: Result<Nanoid, _> = "6yt_invalid_char#####".to_string().try_into();
assert!(matches!(result, Err(ParseError::InvalidCharacter(_))));
§Converting to a string
You can get the string representation of the Nano ID using Nanoid::as_str
, AsRef<str>
or Display
.
use nid::Nanoid;
let id: Nanoid = "Z9ifKfmBL7j69naN7hthu".parse()?;
// Convert to &str
assert_eq!(id.as_str(), "Z9ifKfmBL7j69naN7hthu");
assert_eq!(id.as_ref(), "Z9ifKfmBL7j69naN7hthu");
// Convert to String
assert_eq!(id.to_string(), "Z9ifKfmBL7j69naN7hthu");
§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()?;
Implementations§
source§impl<const N: usize, A: Alphabet> Nanoid<N, A>
impl<const N: usize, A: Alphabet> Nanoid<N, A>
sourcepub fn new() -> Self
pub fn new() -> Self
Generate a new Nano ID using random number generator seeded by the system.
§Panics
The function will panic if the random number generator is not able to generate random numbers.
This function also panics if the provided Alphabet
produces non-ascii characters, but this
never happens unless the alphabet is implemented incorrectly.
§Examples
use nid::Nanoid;
let id: Nanoid = Nanoid::new();
sourcepub fn new_with(rng: impl Rng) -> Self
pub fn new_with(rng: impl Rng) -> Self
Generate a new Nano ID using the provided random number generator.
§Panics
The function will panic if the provided random number generator is not able to generate random numbers.
This function also panics if the provided Alphabet
produces non-ascii characters, but this
never happens unless the alphabet is implemented incorrectly.
§Examples
use nid::Nanoid;
let id: Nanoid = Nanoid::new_with(rand::thread_rng());
sourcepub const fn try_from_str(s: &str) -> Result<Self, ParseError>
pub const fn try_from_str(s: &str) -> Result<Self, ParseError>
Parse a string into a Nanoid
.
§Errors
- If the length of the string is not equal to the expected length, this method returns
ParseError::InvalidLength
. - If the string contains a character that is not in the alphabet, this method returns
ParseError::InvalidCharacter
.
§Examples
use nid::Nanoid;
let id: Nanoid = Nanoid::try_from_str("r9p_QLd_9CD63JqQaGQ9I")?;
sourcepub const fn try_from_bytes(buf: &[u8; N]) -> Result<Self, ParseError>
pub const fn try_from_bytes(buf: &[u8; N]) -> Result<Self, ParseError>
Parse a byte array into a Nanoid
.
§Errors
If the byte array contains a character that is not in the alphabet, this method returns ParseError::InvalidCharacter
.
§Examples
use nid::Nanoid;
let id: Nanoid = Nanoid::try_from_bytes(b"0tY_GxufiwmAxvmHR7G0R")?;
Trait Implementations§
source§impl<'de, const N: usize, A: Alphabet> Deserialize<'de> for Nanoid<N, A>
Available on crate feature serde
only.
impl<'de, const N: usize, A: Alphabet> Deserialize<'de> for Nanoid<N, A>
serde
only.source§fn deserialize<D>(deserializer: D) -> Result<Nanoid<N, A>, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Nanoid<N, A>, D::Error>where
D: Deserializer<'de>,
source§impl<const N: usize, A: Alphabet> Ord for Nanoid<N, A>
impl<const N: usize, A: Alphabet> Ord for Nanoid<N, A>
source§impl<const N: usize, A: Alphabet> PartialEq for Nanoid<N, A>
impl<const N: usize, A: Alphabet> PartialEq for Nanoid<N, A>
source§impl<const N: usize, A: Alphabet> PartialOrd for Nanoid<N, A>
impl<const N: usize, A: Alphabet> PartialOrd for Nanoid<N, A>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<const N: usize, A: Alphabet> Serialize for Nanoid<N, A>
Available on crate feature serde
only.
impl<const N: usize, A: Alphabet> Serialize for Nanoid<N, A>
serde
only.