feat(quotesdb): add generate_id() using UUID v4 — WASM-compatible ID generation

- Add uuid = { version = "1", features = ["v4", "serde"] } to [dependencies]
- Add getrandom = { version = "0.4", features = ["wasm_js"] } under wasm32 cfg
- Implement generate_id() in src/lib.rs with rustdoc and doctest
- Add unit tests for length, hyphen count, and uniqueness

Closes ticket 7a0d9f

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
main
Elijah Voigt 3 months ago
parent 57322d865b
commit 66e5302d9b

@ -15,6 +15,10 @@ path = "src/bin/ui/main.rs"
[dependencies] [dependencies]
common = { path = "../common" } common = { path = "../common" }
uuid = { version = "1", features = ["v4", "serde"] }
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.4", features = ["wasm_js"] }
[dev-dependencies] [dev-dependencies]

@ -6,3 +6,48 @@
//! //!
//! Use `#[cfg(not(target_arch = "wasm32"))]` for host-only items //! Use `#[cfg(not(target_arch = "wasm32"))]` for host-only items
//! and `#[cfg(target_arch = "wasm32")]` for wasm-only items. //! and `#[cfg(target_arch = "wasm32")]` for wasm-only items.
/// Generates a new UUID v4 string for use as a database primary key.
///
/// Returns a 36-character hyphenated UUID string. Compatible with both
/// native and `wasm32-unknown-unknown` targets (uses Web Crypto API via
/// `getrandom/wasm_js` on WASM).
///
/// # Examples
///
/// ```
/// let id = quotesdb::generate_id();
/// assert_eq!(id.len(), 36);
/// assert_eq!(id.chars().filter(|&c| c == '-').count(), 4);
/// ```
pub fn generate_id() -> String {
uuid::Uuid::new_v4().to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_generate_id_length() {
let id = generate_id();
assert_eq!(id.len(), 36, "UUID v4 must be 36 characters");
}
#[test]
fn test_generate_id_hyphen_count() {
let id = generate_id();
assert_eq!(
id.chars().filter(|&c| c == '-').count(),
4,
"UUID v4 must contain exactly 4 hyphens"
);
}
#[test]
fn test_generate_id_uniqueness() {
let id1 = generate_id();
let id2 = generate_id();
assert_ne!(id1, id2, "Two generated IDs must be different");
}
}

Loading…
Cancel
Save