use std::str::Chars;
pub struct Word {
pub word: &'static str,
pub accent: Option<usize>,
pub part_of_speech: PartOfSpeech,
pub meanings: &'static [&'static str],
}
pub enum PartOfSpeech {
Adjective,
Adverb,
Conjunction,
Interjection,
Noun,
Preposition,
Pronoun,
Verb,
}
pub struct AccentedWord<'a> {
chars: Chars<'a>,
accent: Option<usize>,
current: usize,
}
impl Word {
pub const fn adj(word: &'static str, meanings: &'static [&'static str]) -> Self {
Self {
word,
accent: None,
part_of_speech: PartOfSpeech::Adjective,
meanings,
}
}
pub const fn adv(word: &'static str, meanings: &'static [&'static str]) -> Self {
Self {
word,
accent: None,
part_of_speech: PartOfSpeech::Adverb,
meanings,
}
}
pub const fn conj(word: &'static str, meanings: &'static [&'static str]) -> Self {
Self {
word,
accent: None,
part_of_speech: PartOfSpeech::Conjunction,
meanings,
}
}
pub const fn interj(word: &'static str, meanings: &'static [&'static str]) -> Self {
Self {
word,
accent: None,
part_of_speech: PartOfSpeech::Interjection,
meanings,
}
}
pub const fn noun(word: &'static str, meanings: &'static [&'static str]) -> Self {
Self {
word,
accent: None,
part_of_speech: PartOfSpeech::Noun,
meanings,
}
}
pub const fn prep(word: &'static str, meanings: &'static [&'static str]) -> Self {
Self {
word,
accent: None,
part_of_speech: PartOfSpeech::Preposition,
meanings,
}
}
pub const fn pronoun(word: &'static str, meanings: &'static [&'static str]) -> Self {
Self {
word,
accent: None,
part_of_speech: PartOfSpeech::Preposition,
meanings,
}
}
pub const fn verb(word: &'static str, meanings: &'static [&'static str]) -> Self {
Self {
word,
accent: None,
part_of_speech: PartOfSpeech::Preposition,
meanings,
}
}
pub const fn with_accent(mut self, accent: usize) -> Self {
self.accent = Some(accent);
self
}
pub fn accented(&self) -> AccentedWord {
AccentedWord {
chars: self.word.chars(),
accent: self.accent,
current: 0,
}
}
}
impl Iterator for AccentedWord<'_> {
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
if let Some(accent) = self.accent {
let next = if self.current == accent + 1 {
Some('\u{301}')
} else {
self.chars.next()
};
self.current += 1;
next
} else {
self.chars.next()
}
}
}