TypeID Prefix
A Rust library that implements robust validation and sanitization for the prefix component of TypeIDs, strictly adhering to the rules defined in the official TypeID Specification.
TypeIDs are globally unique, type-prefixed identifiers. This crate focuses only on the prefix part, ensuring it conforms to the specification's requirements for length, character set, and structure.
It can be combined with crates handling the suffix part (like the TypeIdSuffix crate) to build complete TypeID solutions, such as the mti (Magic Type Id) crate. For a holistic implementation of the TypeID specification, consider using the mti crate.
Conformance to TypeID Specification
This crate ensures that TypeIdPrefix instances adhere to the following rules for prefixes as outlined in the TypeID Specification:
- Maximum Length: Prefixes must be no more than 63 characters long.
- Character Set: Prefixes must consist of lowercase ASCII letters (
a-z). This crate also permits underscores (_) as internal separators, but ensures they do not violate start/end rules. - Structure:
- Must start with a lowercase ASCII letter.
- Must end with a lowercase ASCII letter.
- Therefore, prefixes cannot start or end with an underscore.
- Non-Empty: An empty string is not a valid prefix.
The validation and sanitization logic within this crate is designed to enforce these rules rigorously.
Features
- Type-safe: Ensures that
TypeIDprefixes conform to the specification. - Validation: Provides robust validation for
TypeIDprefixes against specification rules. - Sanitization: Offers methods to clean and sanitize input strings into valid
TypeIDprefixes. - Zero-cost abstractions: Designed to have minimal runtime overhead.
- Optional tracing: Integrates with the
tracingcrate for logging (optional feature).
Installation
Add this to your Cargo.toml:
[]
= "1.0.0" # Replace with the latest version
To enable tracing support, add:
[]
= { = "1.0.0", = ["instrument"] } # Replace with the latest version
Usage
Basic Usage
use ; // Assuming Sanitize is re-exported or use PrefixFactory
use TryFrom;
// If using PrefixFactory directly:
// use typeid_prefix::PrefixFactory;
Validation
The TypeIdPrefix type ensures that all instances conform to the TypeID specification rules mentioned in the "Conformance" section.
use TypeIdPrefix;
use TryFrom;
Sanitization
The PrefixFactory trait (implemented for string types) provides create_prefix_sanitized() to clean and attempt to create a valid TypeIdPrefix.
use PrefixFactory;
Optional Tracing
When the instrument feature is enabled, the crate will log validation errors using the tracing crate:
[]
= { = "1.0.0", = ["instrument"] } # Replace with the latest version
use PrefixFactory;
Use Cases
- Database Systems: Use
TypeIdPrefixto ensure consistent and valid type prefixes for database schemas or ORM mappings, aligning with TypeID standards. - API Development: Validate and sanitize user input for API endpoints that require TypeID prefixes, ensuring adherence to the specification.
- Code Generation: Generate valid TypeID prefixes for code generation tools or macros.
- Configuration Management: Ensure configuration keys or identifiers that act as TypeID prefixes conform to a consistent format.
Safety and Correctness
This crate has been thoroughly tested and verified:
- Comprehensive unit tests
- Property-based testing with
proptest - Fuzz testing
- Formal verification with Kani (if applicable, confirm this is still accurate)
These measures ensure that the crate behaves correctly according to the TypeID prefix specification and aims to prevent panics under normal usage.
Minimum Supported Rust Version (MSRV)
This crate is guaranteed to compile on Rust 1.60.0 and later.
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Credits
This crate implements the prefix validation and sanitization rules from the TypeID Specification created by Jetpack.io.