Crates.io | protected-id-derive |
lib.rs | protected-id-derive |
version | 0.1.1 |
source | src |
created_at | 2021-12-02 12:04:22.844004 |
updated_at | 2024-02-20 11:35:58.716801 |
description | Macro for deriving type-safe Id values |
homepage | |
repository | |
max_upload_size | |
id | 491177 |
size | 4,561 |
Protected Id is a macro for creating type-checked Id strings. This is intended to prevent errors caused by storing Ids as strings, and to provide a readonly interface to Ids.
#[macro_use]
extern crate protected_id_derive;
#[derive(ProtectedId)]
struct SomeIdType {
#[protected_value]
id: String
}
#[derive(ProtectedId)]
struct SomeOtherIdType {
#[protected_value]
id: String
}
fn do_something (id: &SomeIdType) {
// do something here
}
// Compiles
let id = SomeIdType::new();
do_something(&id);
// Does not compile
let id = SomeOtherIdType::new();
do_something(&id);
The stored Id can be retrieved as a String
by calling id.unprotect()
.
#[macro_use]
extern crate protected_id_derive;
#[derive(ProtectedId)]
struct SomeIdType {
#[protected_value]
id: String
}
let id = SomeIdType::new();
let stored_id: String = id.unprotect();
A protected Id can also be constructed from an existing String
(e.g. reading data from a database into a struct) by using the new_from(id: String)
associated function.
Any crate that uses protected_id must depend on the uuid crate