#![forbid(unsafe_code)]
pub mod cast;
pub mod midi;
pub mod shell_sidecar;
#[must_use]
pub fn slugify(name: &str) -> String {
let mut out = String::with_capacity(name.len());
let mut prev_dash = false;
for c in name.chars() {
if c.is_ascii_alphanumeric() {
out.push(c.to_ascii_lowercase());
prev_dash = false;
} else if !prev_dash {
out.push('-');
prev_dash = true;
}
}
out.trim_matches('-').to_string()
}
#[must_use]
pub fn safe_filename(name: &str) -> String {
let mut out = String::with_capacity(name.len());
let mut prev_dash = false;
for c in name.chars() {
let reserved = matches!(c, '/' | '\\' | ':' | '<' | '>' | '"' | '|' | '?' | '*')
|| c == '\0'
|| c.is_control();
if reserved {
if !prev_dash {
out.push('-');
prev_dash = true;
}
} else {
out.push(c);
prev_dash = false;
}
}
out.trim_matches(|c: char| c.is_whitespace() || c == '.' || c == '-')
.to_string()
}
#[cfg(test)]
mod slugify_tests {
use super::slugify;
#[test]
fn slugify_basic() {
assert_eq!(slugify("My Plugin"), "my-plugin");
assert_eq!(slugify("Hello!! World"), "hello-world");
assert_eq!(slugify("--leading and trailing--"), "leading-and-trailing");
assert_eq!(slugify("ABC123"), "abc123");
assert_eq!(slugify(""), "");
}
}
#[cfg(test)]
mod safe_filename_tests {
use super::safe_filename;
#[test]
fn replaces_path_separators() {
assert_eq!(safe_filename("Truce Dry/Wet"), "Truce Dry-Wet");
assert_eq!(safe_filename(r"Foo\Bar"), "Foo-Bar");
}
#[test]
fn replaces_windows_reserved() {
assert_eq!(safe_filename(r#"a:b<c>d"e|f?g*h"#), "a-b-c-d-e-f-g-h");
}
#[test]
fn collapses_runs_of_replacements() {
assert_eq!(safe_filename("Dry//Wet"), "Dry-Wet");
assert_eq!(safe_filename("A//B\\\\C"), "A-B-C");
}
#[test]
fn preserves_case_and_spaces() {
assert_eq!(safe_filename("Truce DryWet"), "Truce DryWet");
assert_eq!(safe_filename("ALL CAPS"), "ALL CAPS");
}
#[test]
fn trims_whitespace_and_dots() {
assert_eq!(safe_filename(" Foo "), "Foo");
assert_eq!(safe_filename(".hidden."), "hidden");
assert_eq!(safe_filename(" . . trim . . "), "trim");
}
#[test]
fn empty_in_empty_out() {
assert_eq!(safe_filename(""), "");
assert_eq!(safe_filename("///"), "");
}
}