Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ anyhow = "1"
url = "2"
dirs = "6"
uuid = { version = "1", features = ["v4"] }
petname = "3"
humantime = "2"
humantime-serde = "1"
which = "8.0.2"
Expand Down
55 changes: 11 additions & 44 deletions src/name_gen.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,32 @@
//! Random human-friendly workspace name generator (e.g. `"bold_turing"`).
use uuid::Uuid;

const ADJECTIVES: &[&str] = &[
"bold", "bright", "brave", "calm", "clever", "eager", "fair", "gentle", "happy", "keen",
"kind", "lively", "merry", "noble", "proud", "quiet", "swift", "warm", "wise", "free",
];

const NOUNS: &[&str] = &[
"turing",
"neumann",
"lovelace",
"hopper",
"curie",
"darwin",
"euler",
"gauss",
"newton",
"pascal",
"ramanujan",
"knuth",
"dijkstra",
"boole",
"babbage",
"shannon",
"hawking",
"einstein",
"feynman",
"tesla",
];

/// Generate a random workspace name in `adjective_noun` format.
///
/// Uses UUID v4 entropy so each call produces a unique name with high
/// probability. Example outputs: `"bold_turing"`, `"calm_lovelace"`.
/// Backed by the `petname` crate's curated word lists and RNG.
/// Example outputs: `"bold_turing"`, `"calm_lovelace"`.
#[must_use]
pub fn generate_name() -> String {
let id = Uuid::new_v4();
let b = id.as_bytes();
let adj = ADJECTIVES[b[0] as usize % ADJECTIVES.len()];
let noun = NOUNS[b[1] as usize % NOUNS.len()];
format!("{adj}_{noun}")
petname::petname(2, "_").unwrap_or_else(|| "workspace".to_string())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn format_is_adjective_underscore_noun() {
fn format_is_two_words_joined_by_underscore() {
let name = generate_name();
let mut parts = name.splitn(2, '_');
let adj = parts.next().expect("adjective part");
let noun = parts.next().expect("noun part");
assert!(ADJECTIVES.contains(&adj), "unknown adjective: {adj}");
assert!(NOUNS.contains(&noun), "unknown noun: {noun}");
let adj = parts.next().unwrap_or_default();
let noun = parts.next().unwrap_or_default();
assert!(!adj.is_empty(), "missing adjective part");
assert!(!noun.is_empty(), "missing noun part");
}

#[test]
fn generates_distinct_names() {
// 20×20 = 400 possible names; P(all 5 identical) ≈ (1/400)^4 ≈ 0
let names: std::collections::HashSet<_> = (0..5).map(|_| generate_name()).collect();
assert!(names.len() > 1, "5 calls all returned the same name");
// Curated word lists are large; 20 draws matching is effectively impossible.
let names: std::collections::HashSet<_> = (0..20).map(|_| generate_name()).collect();
assert!(names.len() > 1, "20 calls all returned the same name");
}
}
Loading