//! The slug a NEW project is born with — minted from its name, ONCE. The port //! of `packages/core/src/engine/projectSlug.ts`. //! //! The slug is the briefing's stable address for a project (`get_project`, //! `brief`, and any link somebody kept), so it is minted at birth and never //! moves again: a rename that carried the slug with it would point old anchors //! at a stranger. use std::collections::BTreeSet; use std::sync::LazyLock; use regex::Regex; /// The column's own ceiling (`length(slug) BETWEEN 1 AND 64`). const MAX_LENGTH: usize = 64; /// What a name with nothing addressable left in it becomes. const FALLBACK: &str = "project"; /// Names the app owns as a PATH — a literal route beside `/projects/[slug]` /// that the router prefers, so a project minted at that word would have a page /// nothing could open. /// /// `global` was the one such word until the Global page was retired; the /// list is EMPTY today and the mechanism stays for the next literal route. pub const RESERVED_PROJECT_SLUGS: &[&str] = &[]; /// True when `slug` is a name the app must refuse to mint a project at. pub fn is_reserved_project_slug(slug: &str) -> bool { RESERVED_PROJECT_SLUGS.contains(&slug) } /// Name → the column's alphabet, with both edges clean. /// /// No `NFKD` here, unlike `mint_memory_slug`: `Café Meri` becomes `caf-meri`, /// not `cafe-meri`. That is the TypeScript's behaviour and it is pinned by /// test, because the two slug minters sitting a directory apart with one of /// them normalising is exactly the kind of difference that gets "tidied" into a /// silent address change. fn normalize(name: &str) -> String { static NON_SLUG: LazyLock = LazyLock::new(|| Regex::new(r"[^a-z0-9]+").expect("a valid pattern")); static EDGE_DASHES: LazyLock = LazyLock::new(|| Regex::new(r"^-+|-+$").expect("a valid pattern")); let lowered = name.to_lowercase(); let kebab = NON_SLUG.replace_all(&lowered, "-"); EDGE_DASHES.replace_all(&kebab, "").into_owned() } fn trim_trailing_dashes(text: &str) -> String { static TRAILING: LazyLock = LazyLock::new(|| Regex::new(r"-+$").expect("a valid pattern")); TRAILING.replace(text, "").into_owned() } /// `String.prototype.slice(0, n)` — UTF-16 code units, as the TypeScript cuts. fn utf16_slice(text: &str, end: usize) -> String { let units: Vec = text.encode_utf16().collect(); String::from_utf16_lossy(&units[..end.min(units.len())]) } /// Mint the slug for a new project. /// /// The shape the COLUMN enforces: 1–64 characters of lowercase `a-z0-9` and /// `-`, and the FIRST character must be `a-z0-9` — a leading dash is refused, a /// trailing one is not. Anything outside that becomes a separator, and a name /// that leaves nothing behind falls back to `project` rather than minting an /// empty address the CHECK would refuse. **The trailing-dash trim is ours, not /// the column's**: `meri-` is a legal slug and a shabby address. /// /// The column also holds `UNIQUE (workspace_id, slug)`, which is why `taken` is /// not optional — a second project called "Meri" is an ordinary thing to want, /// and minting the address that already exists turns it into a constraint /// failure the person reads as a toast with nothing to do about it. The suffix /// (`-2`, `-3`, …) stays inside the 64-char cap by shortening the head. /// /// A RESERVED slug is treated as TAKEN, not refused: refusing would mean /// telling someone they may not call a project "Global". The collision loop /// already knows how to keep a name and change its address. /// /// `taken` is every slug already used in this workspace — archived projects /// included, since UNIQUE does not care about status. pub fn mint_project_slug(name: &str, taken: &BTreeSet) -> String { let unavailable = |slug: &str| taken.contains(slug) || is_reserved_project_slug(slug); let base = trim_trailing_dashes(&utf16_slice(&normalize(name), MAX_LENGTH)); let base = if base.is_empty() { FALLBACK.to_string() } else { base }; if !unavailable(&base) { return base; } // Terminates: every candidate is distinct (the number differs), so a finite // `taken` plus a finite reserved list runs out. The head is re-trimmed // after shortening, because the cut that makes room for the suffix can land // on a separator. let mut n = 2u32; loop { let suffix = format!("-{n}"); let head = trim_trailing_dashes(&utf16_slice(&base, MAX_LENGTH - suffix.len())); let head = if head.is_empty() { FALLBACK } else { &head }; let candidate = format!("{head}{suffix}"); if !unavailable(&candidate) { return candidate; } n += 1; } } #[cfg(test)] #[path = "project_slug_tests.rs"] mod tests;