Crates.io | typeid_prefix |
lib.rs | typeid_prefix |
version | 1.1.1-beta.1 |
source | src |
created_at | 2024-07-10 20:39:56.018041 |
updated_at | 2024-07-13 04:31:30.314064 |
description | A Rust library that implements a type-safe version of the TypePrefix section of the `TypeID` Specification |
homepage | |
repository | https://github.com/GovCraft/typeid_prefix |
max_upload_size | |
id | 1298730 |
size | 40,248 |
TypeID
PrefixA Rust library that implements a type-safe version of the TypePrefix section of the TypeID Specification.
Combined with the TypeIdSuffix crate to comprise the mti (Magic Type Id) crate.
Use the mti (Magic Type Id) crate for a holistic implementation of the TypeID specification.
TypeID
prefixes conform to the specification.TypeID
prefixes.TypeID
prefixes.tracing
crate for logging (optional feature).Add this to your Cargo.toml
:
[dependencies]
typeid_prefix = "1.1.1-beta.1"
To enable tracing support, add:
[dependencies]
typeid_prefix = { version = "1.1.1-beta.1", features = ["instrument"] }
use typeid_prefix::{TypeIdPrefix, Sanitize};
use std::convert::TryFrom;
fn main() {
// Create a TypeIdPrefix from a valid string
let prefix = TypeIdPrefix::try_from("user").unwrap();
println!("Valid prefix: {}", prefix);
// Attempt to create from an invalid string
let result = TypeIdPrefix::try_from("Invalid_Prefix");
assert!(result.is_err());
// Sanitize an invalid string
let sanitized = "Invalid_Prefix123".sanitize_and_create();
println!("Sanitized prefix: {}", sanitized);
}
The TypeIdPrefix
type ensures that all instances conform to the TypeID
specification:
use typeid_prefix::TypeIdPrefix;
use std::convert::TryFrom;
fn validate_prefix(input: &str) {
match TypeIdPrefix::try_from(input) {
Ok(prefix) => println!("Valid prefix: {}", prefix),
Err(e) => println!("Invalid prefix: {}", e),
}
}
fn main() {
validate_prefix("valid_prefix");
validate_prefix("Invalid_Prefix");
validate_prefix("_invalid");
validate_prefix("toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong");
}
The Sanitize
trait provides a method to clean and create a valid TypeIdPrefix
from any string:
use typeid_prefix::Sanitize;
fn main() {
let sanitized = "Invalid String 123!@#".sanitize_and_create();
println!("Sanitized: {}", sanitized); // Outputs: invalidstring
}
When the instrument
feature is enabled, the crate will log validation errors using the tracing
crate:
[dependencies]
typeid_prefix = { version = "1.0.0", features = ["instrument"] }
use typeid_prefix::Sanitize;
fn main() {
// Set up your tracing subscriber here
let _sanitized = "Invalid_Prefix!@#".sanitize_and_create();
// Validation errors will be logged via tracing
}
TypeIdPrefix
to ensure consistent and valid type prefixes for database schemas or ORM mappings.This crate has been thoroughly tested and verified:
proptest
These measures ensure that the crate behaves correctly and never panics under normal usage.
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.