use alloc::vec::Vec;
pub struct PinyinInputMethod;
const DICT: &[(&str, &str)] = &[
("zhong", "中种終重種眾"),
("guo", "果国裏菓國過"),
("ai", "愛"),
];
impl PinyinInputMethod {
pub fn candidates(&self, input: &str) -> Option<Vec<char>> {
if input.is_empty() {
return None;
}
if matches!(input.chars().next(), Some('i' | 'u' | 'v' | ' ')) {
return None;
}
for &(py, chars) in DICT {
if py.starts_with(input) {
return Some(chars.chars().collect());
}
}
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn candidates_basic_and_prefix() {
let ime = PinyinInputMethod;
let result_full = ime.candidates("zhong").unwrap();
assert_eq!(result_full[0], '中');
let result_prefix = ime.candidates("zho").unwrap();
assert_eq!(result_prefix, result_full);
let single = ime.candidates("g").unwrap();
assert_eq!(single[0], '果');
}
#[test]
fn unknown_returns_none() {
let ime = PinyinInputMethod;
assert!(ime.candidates("foobar").is_none());
}
}