vtcode-commons 0.169.3

Shared traits for paths, telemetry, and error reporting reused across VT Code component extractions
Documentation
#![expect(
    clippy::indexing_slicing,
    reason = "Random indices are generated from the exact lengths of the static slug tables."
)]

//! Human-readable slug generator for plan file names
//!
//! Generates memorable identifiers by combining random adjectives and nouns,
//! producing slugs like "gentle-harbor" or "cosmic-wizard".
//!
//! Based on OpenCode's slug utility pattern for planning workflow file naming.

use rand::RngExt;

/// Adjectives for slug generation (30 options)
const ADJECTIVES: &[&str] = &[
    "brave", "calm", "clever", "cosmic", "crisp", "curious", "eager", "gentle", "glowing", "happy", "hidden", "jolly",
    "kind", "lucky", "mighty", "misty", "neon", "nimble", "playful", "proud", "quick", "quiet", "shiny", "silent",
    "stellar", "sunny", "swift", "tidy", "witty", "bright",
];

/// Nouns for slug generation (32 options)
const NOUNS: &[&str] = &[
    "cabin", "cactus", "canyon", "circuit", "comet", "eagle", "engine", "falcon", "forest", "garden", "harbor",
    "island", "knight", "lagoon", "meadow", "moon", "mountain", "nebula", "orchid", "otter", "panda", "pixel",
    "planet", "river", "rocket", "sailor", "squid", "star", "tiger", "wizard", "wolf", "stream",
];

/// Create a human-readable slug by combining a random adjective with a random noun.
///
/// # Examples
///
/// ```
/// use vtcode_commons::slug;
///
/// let slug = slug::create();
/// // Returns something like "gentle-harbor", "cosmic-wizard", etc.
/// assert!(slug.contains('-'));
/// ```
pub fn create() -> String {
    let mut rng = rand::rng();
    let adj_idx = rng.random_range(0..ADJECTIVES.len());
    let noun_idx = rng.random_range(0..NOUNS.len());

    format!("{}-{}", ADJECTIVES[adj_idx], NOUNS[noun_idx])
}

/// Create a timestamped slug with a human-readable suffix.
///
/// Format: `{timestamp_millis}-{adjective}-{noun}`
///
/// # Examples
///
/// ```
/// use vtcode_commons::slug;
///
/// let slug = slug::create_timestamped();
/// // Returns something like "1768330644696-gentle-harbor"
/// ```
pub fn create_timestamped() -> String {
    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0);

    format!("{}-{}", timestamp, create())
}

/// Create a slug with a custom prefix.
///
/// # Examples
///
/// ```
/// use vtcode_commons::slug;
///
/// let slug = slug::create_with_prefix("plan");
/// // Returns something like "plan-gentle-harbor"
/// ```
pub fn create_with_prefix(prefix: &str) -> String {
    format!("{}-{}", prefix, create())
}

/// Whether a display title looks like a humanized generated codename
/// (`"Jolly Forest"` from `1789108823046-jolly-forest`).
///
/// Generated slugs are always `{adjective}-{noun}` from the fixed tables
/// above, so the humanized form is exactly two Title-Case words drawn from
/// those tables. User titles (`"Release"`, `"Release Notes"`) do not match
/// both tables and pass through as descriptive.
pub fn is_humanized_codename(title: &str) -> bool {
    let words: Vec<&str> = title.split_whitespace().collect();
    if words.len() != 2 {
        return false;
    }
    let adjective = words[0].to_ascii_lowercase();
    let noun = words[1].to_ascii_lowercase();
    ADJECTIVES.contains(&adjective.as_str()) && NOUNS.contains(&noun.as_str())
}

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

    #[test]
    fn test_create_slug() {
        let slug = create();
        assert!(slug.contains('-'));
        let parts: Vec<&str> = slug.split('-').collect();
        assert_eq!(parts.len(), 2);
        assert!(ADJECTIVES.contains(&parts[0]));
        assert!(NOUNS.contains(&parts[1]));
    }

    #[test]
    fn test_create_timestamped() {
        let slug = create_timestamped();
        let parts: Vec<&str> = slug.split('-').collect();
        assert_eq!(parts.len(), 3);
        assert!(parts[0].parse::<u128>().is_ok());
    }

    #[test]
    fn test_create_with_prefix() {
        let slug = create_with_prefix("plan");
        assert!(slug.starts_with("plan-"));
        let parts: Vec<&str> = slug.split('-').collect();
        assert_eq!(parts.len(), 3);
        assert_eq!(parts[0], "plan");
    }

    #[test]
    fn test_uniqueness() {
        let slugs: Vec<String> = (0..100).map(|_| create()).collect();
        let unique_count = slugs.iter().collect::<hashbrown::HashSet<_>>().len();
        assert!(unique_count > 50, "Expected mostly unique slugs");
    }

    #[test]
    fn test_is_humanized_codename_detects_generated_names() {
        assert!(is_humanized_codename("Jolly Forest"));
        assert!(is_humanized_codename("Kind Lagoon"));
        assert!(!is_humanized_codename("Release"));
        assert!(!is_humanized_codename("Release Notes"));
        assert!(!is_humanized_codename("Refine README"));
        assert!(!is_humanized_codename(""));
    }
}