Skip to main content

truce_utils/
lib.rs

1#![forbid(unsafe_code)]
2
3//! Dependency-free utilities shared across the truce workspace.
4//!
5//! - [`cast`] — numeric-cast helpers for the audio-plugin → host FFI
6//!   boundary (`usize` ↔ `u32` length casts, host `f64` ↔ DSP `f32`,
7//!   discrete-index ↔ normalized).
8//! - [`midi`] — MIDI value-domain normalize / denormalize between
9//!   wire-native integers and `f32` ranges, plus the spec's MIDI 1.0
10//!   ↔ MIDI 2.0 bit-replication bridges.
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 modules above so consumers that pull
19//! `truce-core` don't need a second dependency. Crates that want to
20//! avoid `truce-core`'s `truce-params` chain (notably `cargo-truce`)
21//! depend on `truce-utils` directly.
22
23pub mod cast;
24pub mod midi;
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}