truce_utils/lib.rs
1#![forbid(unsafe_code)]
2
3//! Dependency-free utilities shared across the truce workspace.
4//!
5//! Three unrelated families of helpers live here so neither has to pull
6//! in a heavier crate's transitive chain:
7//!
8//! - [`cast`] — numeric-cast helpers for the audio-plugin → host FFI
9//! boundary (MIDI byte encodes, `usize` ↔ `u32` length casts, host
10//! `f64` ↔ DSP `f32`, discrete-index ↔ normalized).
11//! - [`shell_sidecar`] — sidecar-file path resolution shared by
12//! `cargo-truce` (writes the sidecar at install-time) and the
13//! `truce::plugin!` macro (reads it at runtime to locate the logic
14//! dylib for hot-reload).
15//! - [`slugify`] — ASCII-safe filesystem / IRI slug used by the LV2
16//! staging path and runtime bundle-name derivation.
17//!
18//! `truce-core` re-exports the [`cast`] module and [`slugify`] for
19//! backwards compatibility with workspace call sites that already
20//! import `truce_core::cast::*` and `truce_core::slugify`. Crates that
21//! want to avoid `truce-core`'s `truce-params` chain (notably
22//! `cargo-truce`) depend on `truce-utils` directly.
23
24pub mod cast;
25pub mod shell_sidecar;
26
27/// Slug a plugin's display name into a lowercase, hyphenated,
28/// ASCII-safe identifier suitable for filesystem paths, LV2 bundle
29/// names, and IRI components.
30///
31/// Rules: ASCII alphanumerics pass through lowercased; every other
32/// character (including runs of them) collapses to a single `-`;
33/// leading and trailing dashes are trimmed.
34#[must_use]
35pub fn slugify(name: &str) -> String {
36 let mut out = String::with_capacity(name.len());
37 let mut prev_dash = false;
38 for c in name.chars() {
39 if c.is_ascii_alphanumeric() {
40 out.push(c.to_ascii_lowercase());
41 prev_dash = false;
42 } else if !prev_dash {
43 out.push('-');
44 prev_dash = true;
45 }
46 }
47 out.trim_matches('-').to_string()
48}
49
50#[cfg(test)]
51mod slugify_tests {
52 use super::slugify;
53
54 #[test]
55 fn slugify_basic() {
56 assert_eq!(slugify("My Plugin"), "my-plugin");
57 assert_eq!(slugify("Hello!! World"), "hello-world");
58 assert_eq!(slugify("--leading and trailing--"), "leading-and-trailing");
59 assert_eq!(slugify("ABC123"), "abc123");
60 assert_eq!(slugify(""), "");
61 }
62}