base64id/
lib.rs

1//! This crate allows for fixed length 64, 32 and 16 bit integers to be represented as [base64url](https://datatracker.ietf.org/doc/html/rfc4648#section-5) encoded strings.
2//! This is useful for exchanging unique identifiers in a web based contexts; eg. sending an SQL primary key to a client with as few character as possible.
3//!
4//! This crate is `#![no_std]`.
5//!
6//! ## Quick Start
7//! Add the following to your `Cargo.toml` file.
8//! ```toml
9//! [dependencies]
10//! base64id = "0.4"
11//! ```
12//!
13//! ### Encoding
14//! You can convert signed or unsigned integers to a Base64Id struct as follows:
15//! ```rust
16//! use base64id::Base64Id;
17//!
18//! #[derive(Base64Id)]
19//! struct MyId(i64);
20//!
21//! fn main() {
22//!     let int: i64 = 1;
23//!     let id = MyId::from(int);
24//!
25//!     println!("{id}"); // AAAAAAAAAAE
26//! }
27//! ```
28//!
29//! ### Decoding
30//! You can use `FromStr` and `From<{integer}>` to convert a `String` to a Base64Id struct and then into an `i64` as follows:
31//! ```rust
32//! use base64id::{Base64Id, Error};
33//! use std::str::FromStr;
34//!
35//! #[derive(Base64Id)]
36//! struct MyId(i64);
37//!
38//! fn main() -> Result<(), Error> {
39//!     let id_str = MyId::from_str("PDFehCFVGqA")?;
40//!     let id_int = i64::from(id_str);
41//!
42//!     println!("{}", id_int); // 4337351837722417824
43//!
44//!     Ok(())
45//! }
46//! ```
47//!
48//! Refer to the [Error] enum regarding decode errors.
49//!
50//! ## Serde
51//! Support for [Serde](https://serde.rs/) is possible through the use of the `base64id` derive macro helper attribute.
52//!
53//! ```rust
54//! use base64id::Base64Id;
55//! use serde_json::Result;
56//!
57//! #[derive(Base64Id)]
58//! #[base64id(Serialize, Deserialize)]
59//! struct MyId(i32);
60//!
61//! fn main() -> Result<()> {
62//!     let id = MyId(897100256);
63//!
64//!     println!("{}", serde_json::to_string(&id)?); // "NXip4A"
65//!
66//!     Ok(())
67//! }
68//! ```
69
70#[doc(hidden)]
71pub use base64id_core::base64;
72
73pub use base64id_core::Error;
74
75pub use base64id_derive::Base64Id;