Expand description
Crockford
This library is intended to provide an easy way to encode and decode identifiers (large integers) as Crockford-encoded strings. If you want to encode or decode arbitrary data, another library is probably a better choice.
Encoding
Use the encode function to encode u64
values into Crockford Base32-encoded strings. This
operation cannot fail, so you will always get back a string rather than any kind of result
value.
let x = crockford::encode(5111);
assert_eq!("4ZQ", &*x);
Faster encoding (aka “Plan B”)
Because this is Rust, particular focus is given to runtime efficiency–or, at least, allowing the user to achieve runtime efficiency. As a result, we provide a second, more complicated encoding option.
// The longest possible representation of u64 is 13 digits.
let mut buf = Vec::with_capacity(13);
crockford::encode_into(5111, &mut buf);
let result = std::str::from_utf8(&buf)?;
assert_eq!("4ZQ", result);
This encode_into
method also accepts &mut String
, if you prefer.
Decoding
Use the decode function to decode Crockford Base32-encoded strings. This operation can fail; if it does, you’ll get a reasonably useful error instead of a number.
let x = crockford::decode("4zq");
let y = crockford::decode("4ZQ");
assert_eq!(5111, x?);
assert_eq!(5111, y?);
Enums
- Represents an error in decoding.
Traits
- Represents writable buffer capable of receiving encoded data.
Functions
- Attempts to decode a Crockford Base32-encoded string into a
u64
value. - Encodes a
u64
value as a Crockford Base32-encoded string. - Encodes a
u64
value as Crockford Base32 and writes it to the provided output.