use hyphenation::{Hyphenator, Language, Load, Standard};
use std::sync::OnceLock;
fn dictionary() -> Option<&'static Standard> {
static DICT: OnceLock<Option<Standard>> = OnceLock::new();
DICT.get_or_init(|| Standard::from_embedded(Language::EnglishUS).ok())
.as_ref()
}
pub fn break_points(word: &str) -> Vec<usize> {
if word.len() < 5 {
return Vec::new();
}
if !word.chars().all(|c| c.is_alphabetic()) {
return Vec::new();
}
let Some(dict) = dictionary() else {
return Vec::new();
};
let hyphenated = dict.hyphenate(word);
hyphenated.breaks
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hyphenation_returns_break_points_for_long_word() {
let breaks = break_points("hyphenation");
assert!(!breaks.is_empty(), "expected at least one break point");
}
#[test]
fn short_words_have_no_break_points() {
assert!(break_points("cat").is_empty());
assert!(break_points("dog").is_empty());
assert!(break_points("home").is_empty());
}
#[test]
fn words_with_digits_have_no_break_points() {
assert!(break_points("ab12cd").is_empty());
}
#[test]
fn breaks_are_within_word_length() {
let word = "extraordinary";
for &b in &break_points(word) {
assert!(b > 0 && b < word.len(), "break {} out of range", b);
}
}
}