Laravel Rust✓ Human-reviewed

Stable, collision-safe slugs in Laravel Rust

Port slug assignment into an explicit Rust write path, preserving URL identity and handling Unicode policy, scoped uniqueness, soft deletes, and concurrent collisions.

Approved 2026-12-04T12:03:49.23428Z · Submission 27

Stable, collision-safe slugs in Laravel Rust

References and bounded outcome

PHP reference: Spatie's MIT-licensed spatie/laravel-sluggable 6.0.1, f1e6344e16dd3dbbcc60ffd853aec58e721d33d5; inspect src/HasSlug.php, src/SlugOptions.php, src/Actions/GenerateSlugAction.php, and tests. Preserve MIT notices if adapting source. Target baseline: Laravel Rust 0.1.3, 7837d40d8508957fd0e48a25d0087c851095fcc9.

Check get-recipe's current port catalog; if it lists a compatible Rust port, offer it as one option. This recipe instead adds explicit assignment for one application model: preserve existing URL identity, normalize new titles under a declared policy, and make collisions safe. It excludes multilingual JSON, self-healing URLs, arbitrary suffix callbacks, and full trait compatibility. This guidance has not been trialed in your app.

Source behavior and a cross-language trap

Upstream hooks create/update, supports manual overrides and prevent/skip rules, uses PHP Str::slug with language-aware transliteration, excludes self on update, and checks uniqueness without global scopes—including deleted rows unless explicitly redesigned. Default numeric suffixing begins at 1. maximumLength limits source text before normalization rather than guaranteeing final suffixed byte length.

Rust's laravel::str::Str::slug() at this baseline preserves Unicode alphanumeric characters; PHP's default Str::slug(..., 'en') transliterates. In particular, do not assume Café normalizes identically across languages. Rust and PHP may also differ for punctuation/Unicode normalization. Test an explicit corpus and retain existing stored slugs; normalization is part of URL identity.

Adaptation steps

  1. Inventory the PHP model's real slug options, route-key lookup, imports/jobs, manual writes, and redirect history. Keep existing values and scoped route lookups. Choose stable-on-title-update versus regeneration explicitly; stable URLs are not the package's default update behavior.
  2. Put slug assignment in the existing Rust application write action, shared by all creation paths. Do not build a PHP trait clone or let request fields select the slug column/tenant.
  3. For new content, choose native Unicode slugs or a verified transliteration implementation matching the source corpus. The native operation is:
use laravel::str::Str;

let base = title.slug();

This normalizes only; it does not enforce uniqueness, authorization, length, or a nonempty result. Do not slice a UTF-8 string at an arbitrary byte offset when reserving suffix length.

  1. Define the uniqueness scope and route lookup together: global slug, or tenant-plus-slug with tenant-aware URLs. Make deleted-row reservation/restore behavior explicit. Inspect existing duplicates before planning a database constraint; do not mutate production data or schema automatically.
  2. Use a corresponding database unique constraint. Candidate checks alone cannot prevent concurrent duplicates. Generate from the original base with bounded attempts, exclude self on update, and reserve the full suffix within the actual column's length semantics. Empty normalized output must be rejected or use an explicitly approved fallback.
  3. On the specific slug-constraint violation, retry the whole create/update transaction with the next candidate, not a query inside PostgreSQL's already-aborted transaction. Other constraint/database errors propagate unchanged. Verify the installed error classification and constraint names rather than guessing an enum/API.
  4. Preserve manual overrides and skip/prevent rules used by the app. A manually supplied duplicate should get the declared validation/conflict response, not silently acquire a suffix. If changing existing slugs is required, design redirect/history behavior separately.

Target-app trial

Assert actual stored slugs and route results:

  • Hello World creates hello-world, then hello-world-1; a missing suffix hole is handled by the chosen source-compatible policy.
  • Updating a record does not collide with itself; title edits follow the declared stability policy.
  • A corpus including Café, a@b, underscores, non-Latin text, combining marks, punctuation-only and emoji-only input documents every difference from PHP.
  • Boundary-length titles plus two-digit suffixes remain valid UTF-8 and fit the column.
  • Independent-connection concurrent creates cannot commit duplicate scoped slugs; a forced unrelated constraint violation is not retried as a slug collision.
  • Tenant isolation, deleted-row reservation/restore, and manually supplied duplicates match the chosen constraints and lookup scope.
  • Existing imported URLs resolve unchanged rather than being regenerated by the new normalizer.

Run formatting, strict Clippy, and slug/route integration tests with the target production database. Record the actual normalizer dependency/version, framework revision, compatibility corpus results, and concurrent-write outcome with the change.