Crates.io | typeid_suffix |
lib.rs | typeid_suffix |
version | |
source | src |
created_at | 2024-07-11 09:47:09.362657+00 |
updated_at | 2025-05-07 14:43:42.983652+00 |
description | A Rust library that implements the UUID suffix part of the `TypeId`Specification |
homepage | |
repository | https://github.com/GovCraft/typeid_suffix |
max_upload_size | |
id | 1299357 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
TypeId
SuffixA Rust library that implements the suffix portion of the TypeID Specification. It provides functionality to work with TypeId
suffixes, which are base32-encoded representations of UUIDs used in the TypeId
system.
Combined with the TypeIdPrefix crate to comprise the mti (Magic Type Id) crate.
Use the mti (Magic Type Id) crate for a holistic implementation of the TypeID specification.
UUIDv7
and other UUID versions.TypeId
suffixes.TypeId
suffixes and UUIDs.tracing
crate for logging (optional feature instrument
).serde
(optional feature serde
).Add this to your Cargo.toml
:
[dependencies]
typeid_suffix = "1.2.0"
To enable optional features:
[dependencies]
typeid_suffix = { version = "1.2.0", features = ["instrument", "serde"] }
# Or select specific features, e.g., just serde:
# typeid_suffix = { version = "1.2.0", features = ["serde"] }
use std::str::FromStr;
use typeid_suffix::prelude::*;
use uuid::Uuid;
fn main() {
// Create a `TypeIdSuffix` from a `UUIDv7` (default)
let suffix_v7 = TypeIdSuffix::default();
println!("TypeID suffix (v7 default): {}", suffix_v7);
// Create a `TypeIdSuffix` from a specific UUID
let uuid_v4 = Uuid::new_v4();
let suffix_v4: TypeIdSuffix = uuid_v4.into();
println!("TypeID suffix (from v4): {}", suffix_v4);
// Parse a `TypeIdSuffix` from a string
let parsed_suffix = TypeIdSuffix::from_str("01h455vb4pex5vsknk084sn02q").expect("Valid suffix");
// Convert back to a UUID
let recovered_uuid: Uuid = parsed_suffix.try_into().expect("Valid UUID");
// Note: We don't know the original UUID version from the suffix alone without context,
// but we can recover the UUID bytes.
println!("Recovered UUID from parsed suffix: {}", recovered_uuid);
}
use typeid_suffix::prelude::*;
use uuid::Uuid;
fn main() {
// Creating a new suffix for a specific version (e.g., V4)
let suffix_v4 = TypeIdSuffix::new::<V4>();
println!("TypeID suffix for new UUIDv4: {}", suffix_v4);
let uuid_v1 = Uuid::new_v1([1,2,3,4,5,6]); // Example, requires v1 feature on uuid crate
let suffix_v1: TypeIdSuffix = uuid_v1.into();
println!("TypeID suffix for UUIDv1: {}", suffix_v1);
}
The crate provides detailed error types for various failure cases:
use typeid_suffix::prelude::*;
use std::str::FromStr;
fn main() {
let result = TypeIdSuffix::from_str("invalid_suffix"); // Invalid length and characters
match result {
Ok(_) => println!("Valid suffix"),
Err(e) => println!("Invalid suffix: {}", e), // e.g., InvalidSuffix(InvalidLength)
}
let result_bad_first_char = TypeIdSuffix::from_str("81h455vb4pex5vsknk084sn02q"); // First char > '7'
match result_bad_first_char {
Ok(_) => println!("Valid suffix"),
Err(e) => println!("Invalid suffix: {}", e), // e.g., InvalidSuffix(InvalidFirstCharacter)
}
}
instrument
)When the instrument
feature is enabled, the crate will log operations using the tracing
crate:
[dependencies]
typeid_suffix = { version = "1.2.0", features = ["instrument"] }
serde
)When the serde
feature is enabled, TypeIdSuffix
implements serde::Serialize
and serde::Deserialize
. This allows TypeIdSuffix
instances to be easily serialized to and deserialized from various formats like JSON, YAML, CBOR, etc., that are supported by Serde.
TypeIdSuffix
is serialized as its string representation and deserialized from a string.
To enable this feature:
[dependencies]
typeid_suffix = { version = "1.2.0", features = ["serde"] }
Example:
# #[cfg(feature = "serde")] {
use typeid_suffix::prelude::*;
// Note: serde_json is used here as an example and would be a separate dependency.
// Add `serde_json = "1.0"` to your [dependencies] or [dev-dependencies] in Cargo.toml.
use serde_json;
fn main() -> Result<(), serde_json::Error> {
let suffix = TypeIdSuffix::default();
// Serialize
let json_string = serde_json::to_string(&suffix)?;
println!("Serialized suffix: {}", json_string); // e.g., "\"01h455vb4pex5vsknk084sn02q\""
// Deserialize
let deserialized_suffix: TypeIdSuffix = serde_json::from_str(&json_string)?;
assert_eq!(suffix, deserialized_suffix);
println!("Deserialized suffix: {}", deserialized_suffix);
// Example of deserialization error
let invalid_json = "\"invalid_suffix_string\"";
let result: Result<TypeIdSuffix, _> = serde_json::from_str(invalid_json);
assert!(result.is_err());
if let Err(e) = result {
println!("Error deserializing invalid suffix: {}", e);
}
Ok(())
}
# }
TypeId
suffixes as part of API responses or for resource identification.UUIDv7
-based TypeId
suffixes for time-ordered data.This crate has been thoroughly tested and verified:
proptest
These measures ensure that the crate behaves correctly and safely under various inputs and conditions.
This crate is guaranteed to compile on Rust 1.60.0 and later.
This project is licensed under either of
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.
This crate implements a portion of the TypeID Specification created by Jetpack.io.