use console::truncate_str;
use lazy_regex::regex_captures;
pub fn get_dynasty_chapter_identifier(s: &str) -> Option<&str> {
if let Some((chapter_match, ..)) = regex_captures!(r"(ch[\d.]+)", s) {
Some(chapter_match)
} else {
None
}
}
pub fn truncate_dynasty_chapter_title(s: &str, width: usize) -> String {
let identifier = get_dynasty_chapter_identifier(s);
let tail = if let Some(identifier) = identifier {
format!(".. {}", identifier)
} else {
"...".to_string()
};
truncate_str(s, width, &tail).to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_get_dynasty_chapter_identifier() {
let predicates = [
(
"The Skirt Sings at the Landing ch13: A night voyage",
"ch13",
),
("Liar Satsuki Can See Death ch48.5", "ch48.5"),
("Slow Start ch123", "ch123"),
];
for (left, right) in predicates {
assert_eq!(get_dynasty_chapter_identifier(left), Some(right))
}
}
#[test]
fn should_not_get_dynasty_chapter_identifier() {
let predicates = ["Kiss Distance", "A Love Yet to Bloom"];
for predicate in predicates {
assert_eq!(get_dynasty_chapter_identifier(predicate), None)
}
}
}