Agent Skill

Embed the Rust library when code produces slugs; run afslug slugify for a one-off with explicit configuration, and afslug validate (or validate_slug) to check a value that already exists. The CLI exposes every SlugConfig field except transliteration, whose static map only the library can hold.

Do not invent presets or silently change an existing identifier scheme; make every rule that affects compatibility explicit.

Decide the target first

  • One call makes one path segment. Never pass a whole filesystem path or URL.
  • Local filesystem path segment (LocalPathSegment, --validation local-path): refuses empty, ., .., /, \, Unicode whitespace and control characters. Replace dots unless the caller wants them in filenames; choose ASCII-only when the name must survive ASCII-only tools.
  • URL path segment (UrlPathSegment, --validation url-path): the local rules plus ?, # and %. Preserve decimal dots if version numbers matter.
  • Both checks are structural. They do not know Windows reserved names (CON), :, trailing dots some filesystems strip, or byte limits. A generated slug stays portable because of its character set, delimiter and dot policy.
  • The slug is raw UTF-8 and never percent-encoded. URL validation only refuses characters that end or split a segment or start an escape; everything else, non-ASCII included, still goes through a URL library’s path-segment encoder. Never concatenate a slug into a URL by hand.

Library workflow

cargo add agent-first-slug --no-default-features

SlugConfig::default() keeps Unicode letters and numbers, lowercases, replaces every other run (dots included) with -, validates nothing, and keeps an empty slug. Use it only when that matches the target:

use agent_first_slug::{slugify, SlugConfig};

assert_eq!(slugify("Hello, 世界!", &SlugConfig::default())?.slug, "hello-世界");
assert_eq!(slugify("!!!", &SlugConfig::default())?.slug, "");
# Ok::<(), agent_first_slug::SlugError>(())

For anything stored or routed, write every field at the call site:

use agent_first_slug::{
    slugify, AllowedCharacterSet, DotHandlingPolicy, EmptyOutputPolicy,
    SlugConfig, SlugValidationPolicy, TransliterationPolicy,
};

let config = SlugConfig {
    replacement_delimiter: '-',
    lowercase_enabled: true,
    max_slug_chars: Some(80),
    allowed_character_set: AllowedCharacterSet::UnicodeLettersAndDecimalDigits,
    dot_handling_policy: DotHandlingPolicy::PreserveDotsBetweenDecimalDigits,
    transliteration_policy: TransliterationPolicy::None,
    validation_policy: SlugValidationPolicy::UrlPathSegment,
    empty_output_policy: EmptyOutputPolicy::UseFallbackSlug("item".into()),
};
assert_eq!(slugify("Ubuntu 16.04", &config)?.slug, "ubuntu-16.04");
assert_eq!(slugify("!!!", &config)?.slug, "item");
# Ok::<(), agent_first_slug::SlugError>(())
  • Character set. For scripts that write vowels, viramas or tones as combining marks (Thai, Lao, Devanagari, Tamil, …) choose UnicodeLettersMarksAndDecimalDigits: नमस्ते ไม่ใช่ stays नमस्ते-ไม่ใช่. UnicodeLettersAndDecimalDigits drops every mark, and the default UnicodeAlphanumericCharacters keeps most vowel signs but splits at viramas and tone marks (नमस-ते-ไม-ใช).
  • Dots. PreserveDotsBetweenDecimalDigits keeps a dot only between two digits the character set keeps (T.U.S.F.G.E.3.0.8 → t-u-s-f-g-e-3.0.8). PreserveAllDots can leave a trailing dot.
  • Truncation. max_slug_chars counts Unicode scalars, never splits a character from its combining marks, and strips a delimiter or decimal dot the cut exposes. Filesystem and URL limits usually count bytes; size the cap for that unit.
  • Delimiter. It must be a character the configuration filters out. One the character set keeps, a dot a dot policy preserves, or a character lowercasing changes is refused before any input is read, because two different inputs could otherwise collapse into one slug.
  • Fallback. UseFallbackSlug must be a value this configuration could have produced (character set, delimiter, dots, case, length). UseVerbatimFallbackSlug is only for a value that must match something already stored; it checks the target surface and nothing else. Both are checked before any input is read, so a broken or empty fallback fails on every call.
  • Transliteration is a caller-owned static map applied before lowercasing; the longest pattern wins, and empty or repeated patterns (even with the same replacement) are refused for every input. The library never guesses a language or legacy mode.

CLI workflow

cargo install agent-first-slug   # or: brew install agentfirstkit/tap/afslug

Each flag sets one SlugConfig field; an omitted flag means the default (--delimiter -, lowercase, no cap, --charset unicode-alphanumeric, --dots replace, --validation none, keep an empty slug). Pass every flag the target contract needs rather than post-processing the slug.

afslug slugify "Hello, 世界!"
# {"kind":"result","result":{"changed_from_input":true,"code":"slugify","slug":"hello-世界"},"trace":{}}

afslug slugify "Ubuntu 16.04" --charset unicode-letters-digits --dots preserve-between-digits --validation url-path
# {"kind":"result","result":{"changed_from_input":true,"code":"slugify","slug":"ubuntu-16.04"},"trace":{}}

afslug slugify "!!!" --validation local-path --fallback untitled --output plain
# kind=result result.changed_from_input=true result.code=slugify result.slug=untitled trace={}

For input beginning with -, put all options before -- and supply the input after it, as in afslug slugify --output plain -- "-x". A cap of --max-chars 0 forces the empty-output policy. A nonempty strict fallback cannot fit that cap, while a verbatim fallback still follows the selected target-surface policy.

--fallback and --fallback-verbatim are separate shapes; passing both is cli_unregistered_combination. Discovery is one call: afslug slugify --help lists every shape with its closed value sets and defaults.

Branch on the exit status and error.code, never on the message:

  • Exit 1: the slug or value failed its surface — empty_path_segment, path_segment_separator, path_segment_whitespace, path_segment_control_character, path_segment_dot_value, url_path_segment_reserved_character.
  • Exit 2: the call is wrong and retrying it unchanged cannot help — a refused configuration (ambiguous_replacement_delimiter, fallback_violates_config, empty_fallback_slug) or a cli_* argument rejection. Read the hint.
  • Errors go to stderr under the default --output-to split; --output-to stdout gives one ordered stream, except that a cli_* rejection is decided before the routing flag is read and stays on stderr.

Validate an existing value

use agent_first_slug::{validate_slug, SlugError, SlugValidationPolicy};

assert_eq!(validate_slug("a?b", SlugValidationPolicy::LocalPathSegment), Ok(()));
assert_eq!(
    validate_slug("a?b", SlugValidationPolicy::UrlPathSegment),
    Err(SlugError::UrlPathSegmentReservedCharacter { character: '?' })
);
afslug validate "a?b" --policy url-path
# {"error":{"code":"url_path_segment_reserved_character","message":"URL path segment must not contain reserved character `?`","retryable":false},"kind":"error","trace":{}}

--policy defaults to local-path, so pass --policy url-path for a route. A valid value is a result with valid: true; an invalid one is an exit-1 error event, never a valid: false result. Validation checks the surface only, not whether a configuration could have produced the value.

Stable identifiers

  • A slug is not an identifier. It carries no uniqueness, authorization or atomic-creation guarantee: different inputs legitimately produce the same slug, and the default configuration can produce an empty one. When uniqueness matters, allocate an authoritative ID and treat the slug as a readable suffix; resolve collisions where the thing is created.
  • Normalization is the caller’s. Nothing here normalizes, so a precomposed é and e plus a combining accent give different slugs. Normalize before calling if stability across input methods matters, and record which form and Unicode version stored slugs were generated under.
  • Changing delimiter, case, transliteration, dots, character set or truncation changes stable identifiers, and so can an upgrade. Report migration impact before changing stored or public slugs, and diff the old and new output over the stored corpus.

Verification

  • Test unchanged input, punctuation-only input, Unicode with combining marks, dots, empty output, truncation boundaries, fallbacks, and every selected validation policy.
  • For stable identifiers, add golden tests before refactoring a config.
  • For CLI changes, verify JSON/YAML/plain success, per-command help, structured errors and exit codes through the repository test script.

Reporting Problems

If afslug itself is wrong — a crash, an internal error, or behavior that contradicts its docs, not a configuration or credential problem — or the person wants a missing feature, offer to open an issue at https://github.com/agentfirstkit/agent-first-slug/issues. Check for duplicates first. Include afslug --version, the command, error.code, and expected versus actual, with placeholder data only: never the person’s data, paths, or secrets. File it with gh issue create only after the person approves the draft.