lewa/tools/src/letters.rs

94 lines
2.2 KiB
Rust
Raw Normal View History

use crate::Letter;
use anyhow::{anyhow, Result};
fn letter2ipa(l: &String) -> Result<String> {
let l = l.as_str();
// /d f g h j k l m n p q s t w ʃ ʒ ʔ ʙ̥/
// /a ɛ i o u/
match l {
"a" => Ok("a".into()),
"b" => Ok("ʙ̥".into()),
"d" => Ok("d".into()),
"e" => Ok("ɛ".into()),
"f" => Ok("f".into()),
"g" => Ok("g".into()),
"h" => Ok("h".into()),
"i" => Ok("i".into()),
"j" => Ok("j".into()),
"k" => Ok("k".into()),
"l" => Ok("l".into()),
"m" => Ok("m".into()),
"n" => Ok("n".into()),
"o" => Ok("o".into()),
"p" => Ok("p".into()),
"q" => Ok("q".into()),
"r" => Ok("r".into()),
"s" => Ok("s".into()),
"t" => Ok("t".into()),
"u" => Ok("u".into()),
"w" => Ok("w".into()),
"x" => Ok("ʃ".into()),
"z" => Ok("ʒ".into()),
"'" => Ok("ʔ".into()),
_ => Err(anyhow!("didn't want {}", l)),
}
}
fn is_vowel(l: &String) -> bool {
let l = l.as_str();
match l {
"a" | "e" | "i" | "o" | "u" => true,
_ => false,
}
}
fn is_stop(l: &String) -> bool {
let l = l.as_str();
match l {
"p" | "t" | "d" | "k" | "g" | "q" | "'" => true,
_ => false,
}
}
impl Letter {
fn new(l: String) -> Result<Letter> {
let ipa = letter2ipa(&l)?;
let is_vowel = is_vowel(&l);
let is_stop = is_stop(&l);
Ok(Letter {
latin: l,
ipa: ipa,
is_vowel: is_vowel,
is_stop: is_stop,
})
}
}
#[cfg(test)]
mod tests {
#[test]
fn alphabet() {
let alphabet = vec![
"a", "b", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "w", "x", "z", "'",
];
for l in alphabet {
let l = l.to_string();
super::Letter::new(l).unwrap();
}
}
#[test]
fn invalid_letters() {
let invalid = vec!["treason", "y"];
for l in invalid {
let l = l.to_string();
assert!(super::Letter::new(l).is_err());
}
}
}