1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
use std::fmt;
use std::borrow::Cow;
use std::collections::HashMap;

use crate::unicode as U;
use crate::noun::endings as E;
use crate::inflection as I;
use crate::decline as D;

use super::{Group, Number, Gender, Case};

#[derive(Clone, Debug)]
pub struct Irregular {
    declensions: HashMap<(Number, Case), Option<Vec<String>>>,
    gender: Gender,
    stem: String,
    group: Group,
    // Second, third, and fourth declension nouns have alternate declensions. 
    // If this flag is set, it will use those alternate declensions (see 
    // Irregular::endings for those alternatives).
    alt_declension: bool,
}

#[derive(Clone, Debug)]
pub enum IrregularError {
    EmptyDeclension((Number, Case)),
    EmptyDeclensionValue((Number, Case)),
}

impl fmt::Display for IrregularError {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match self {
            IrregularError::EmptyDeclension((number, case)) => {
                write!(f, "The declined form for the {} {} is empty", number, case)
            },
            IrregularError::EmptyDeclensionValue((number, case)) => {
                write!(f, "The declined form for the {} {} contains an empty string.", number, case)
            },
        }
    }
}


impl Irregular {
    /// Ensure all of the String values contained in the declensions HashMap
    /// are in NFC form. 
    fn normalize_declensions(declensions: &mut HashMap<(Number, Case), Option<Vec<String>>>) {
        for maybe_words in declensions.values_mut() { 
            if let Some(words) = maybe_words {
                for word in words.iter_mut() {
                    if let Cow::Owned(normalized) = U::normalize_if_needed(word) { // Receiving a Cow::Owned from U::normalize_if_needed indicates that the String wasn't in NFC form. 
                        *word = normalized; // Overwrite the non-NFC form String with the NFC form String. 
                    }
                }
            }
        }
    }

    /// Verifies that the declensions HashMap doesn't contain empty Vecs and 
    /// that the Vecs don't contain empty strings. 
    fn declensions_not_empty(declensions: &HashMap<(Number, Case), Option<Vec<String>>>) -> Result<(), IrregularError> {
        for (key, maybe_declension) in declensions.iter() {
            if let Some(declension) = maybe_declension {
                if declension.is_empty() {
                    return Err(IrregularError::EmptyDeclension(*key))
                } else {
                    for word in declension {
                        if word.is_empty() {
                            return Err(IrregularError::EmptyDeclensionValue(*key))
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Creates an irregular Latin noun.
    /// 
    /// Latin nouns can contain declensions that do not follow the rules of any
    /// regular declension group. These nouns are, not surprisingly, referred 
    /// to as irregular. 
    /// 
    /// This function provides a method for creating irregular nouns. Unlike 
    /// [`Regular::new`], which takes the singular nominative and genitive 
    /// forms, this function takes a HashMap containing all of the irregular 
    /// declined forms, the stem, and the declension group. If a declined form 
    /// isn't present in the HashMap, it is assumed to be regular and will be 
    /// declined according to the regular Latin rules for the passed in 
    /// declension group. 
    /// 
    /// For example, if the HashMap contains values for the singular dative
    /// form but not the singular ablative form, the singular dative form will
    /// be declined as it appears in the HashMap but the singular ablative form
    /// will be declined the same as a noun created using [`Regular::new`].
    /// 
    /// # Warning
    /// 
    /// Third declension nouns have a singular nominative and vocative (and 
    /// accusative in the case of neuter nouns) form that cannot be declined
    /// by combining the stem with an ending. Because of this, calling 
    /// [`Irregular::decline`] on a third declension noun for those cases will
    /// return [`Non`] unless they are added to the declension HashMap.
    /// 
    /// # Warning
    /// 
    /// Third declension nouns can be either consonant-stem or i-stem. Third 
    /// declension nouns created with this function will decline as consonant-
    /// stem. When creating a third declension i-stem noun, use 
    /// [`new_third_i_stem`] instead.
    /// 
    /// # Example
    /// 
    /// vīs, vīs is an irregular noun with an irregular singular genitive form. 
    /// The stem is vīr- but none of the singular cases use. Moreover, the 
    /// plural cases decline like a third declension i-stem. So all six
    /// singular forms must be provided as do the plural genitive and 
    /// accusative forms. The rest of the forms can be determined by combining
    /// the provided stem with the regular third declension endings. 
    /// 
    /// This example shows how to create the HashMap containing the irregular
    /// declined forms and demonstrates two declensions. The first declension
    /// is regular, the second is irregular. 
    /// ```
    /// use std::collections::HashMap;
    /// use verba::noun as N;
    /// use verba::noun::{Noun};
    /// 
    /// let mut declension = HashMap::new();
    /// declension.insert((N::Number::Singular, N::Case::Nominative), Some(vec!["vīs".to_string()]));
    /// declension.insert((N::Number::Singular, N::Case::Genitive), Some(vec!["vīs".to_string()]));
    /// declension.insert((N::Number::Singular, N::Case::Dative), Some(vec!["vī".to_string()]));
    /// declension.insert((N::Number::Singular, N::Case::Accusative), Some(vec!["vim".to_string()]));
    /// declension.insert((N::Number::Singular, N::Case::Ablative), Some(vec!["vī".to_string()]));
    /// declension.insert((N::Number::Singular, N::Case::Vocative), Some(vec!["vīs".to_string()]));
    /// declension.insert((N::Number::Plural, N::Case::Genitive), Some(vec!["vīrium".to_string()]));
    /// declension.insert((N::Number::Plural, N::Case::Accusative), Some(vec!["vīrēs".to_string(), "vīrīs".to_string()]));
    /// 
    /// let noun = N::Irregular::new(declension, N::Gender::Feminine, "vīr".to_string(), N::Group::Third).unwrap();
    /// 
    /// assert_eq!(noun.decline(N::Number::Plural, N::Case::Nominative), Some(vec!["vīrēs".to_string()]));
    /// assert_eq!(noun.decline(N::Number::Plural, N::Case::Genitive), Some(vec!["vīrium".to_string()]));
    /// ```
    pub fn new(mut declensions: HashMap<(Number, Case), Option<Vec<String>>>, gender: Gender, stem: String, group: Group) -> Result<Irregular, IrregularError> {
        Irregular::normalize_declensions(&mut declensions);
        let stem = U::normalize(stem);

        match Irregular::declensions_not_empty(&declensions) {
            Ok(()) => {
                Ok(Irregular {
                    declensions,
                    gender,
                    stem,
                    group,
                    alt_declension: false,
                })
            },
            Err(error) => Err(error),
        }
    }

    /// Creates a new Irregular and sets it alt_declension value to true. 
    fn new_alt_declension(declensions: HashMap<(Number, Case), Option<Vec<String>>>, gender: Gender, stem: String, group: Group) -> Result<Irregular, IrregularError> {
        match Irregular::new(declensions, gender, stem, group) {
            Ok(mut noun) => {
                noun.alt_declension = true;

                Ok(noun)
            },
            Err(error) => Err(error),
        }
    }

    pub fn new_second_ius(declensions: HashMap<(Number, Case), Option<Vec<String>>>, gender: Gender, stem: String) -> Result<Irregular, IrregularError> {
        Irregular::new_alt_declension(declensions, gender, stem, Group::Second)
    }

    pub fn new_third_i_stem(declensions: HashMap<(Number, Case), Option<Vec<String>>>, gender: Gender, stem: String) -> Result<Irregular, IrregularError> {
        Irregular::new_alt_declension(declensions, gender, stem, Group::Third)
    }

    pub fn new_fourth_u(declensions: HashMap<(Number, Case), Option<Vec<String>>>, gender: Gender, stem: String) -> Result<Irregular, IrregularError> {
        Irregular::new_alt_declension(declensions, gender, stem, Group::Fourth)
    }

    fn endings(&self, number: Number, case: Case) -> Option<E::Suffixes> {
        match self.group {
            Group::First => E::first_endings(number, case, self.gender),
            Group::Second if self.alt_declension => E::second_ius_endings(number, case, self.gender),
            Group::Second => E::second_endings(number, case, self.gender),
            Group::Third if self.alt_declension => E::third_i_stem_endings(number, case, self.gender),
            Group::Third => E::third_endings(number, case, self.gender),
            Group::Fourth if self.alt_declension => E::fourth_u_endings(number, case, self.gender),
            Group::Fourth => E::fourth_endings(number, case, self.gender),
            Group::Fifth if D::does_end_with_vowel(&self.stem) => E::fifth_vowel_stem_endings(number, case, self.gender),
            Group::Fifth => E::fifth_endings(number, case, self.gender),
        }
    }
}

impl super::Noun for Irregular {
    fn gender(&self) -> Gender {
        self.gender
    }

    fn stem(&self) -> Option<&str> {
        Some(&self.stem)
    }

    fn group(&self) -> Option<Group> {
        Some(self.group)
    }

    fn decline(&self, number: Number, case: Case) -> Option<Vec<String>> {
        // If self.declensions contains the declension for the passed in number
        // and case, create a clone of it and return the clone. Otherwise, 
        // decline the case as you would a RegularNoun. 
        if let Some(declension) = self.declensions.get(&(number, case)) {
            match declension {
                Some(words) => Some(words.to_vec()),
                None => None,
            }
        } else {
            match self.endings(number, case) {
                Some(suffixes) => {
                    match self.stem() {
                        Some(stem) => Some(I::stem_with_endings(stem, &suffixes)),
                        None => None,
                    }
                },
                None => None,
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    
    use crate::noun::{Noun, Group, Number, Gender, Case};
    use unicode_normalization::{UnicodeNormalization, is_nfc};

    fn verify_normalization(declensions: &HashMap<(Number, Case), Option<Vec<String>>>) {
        for key in declensions.keys() {
            if let Some(Some(words)) = declensions.get(key) {
                for word in words {
                    if is_nfc(word) == false {
                        panic!("Word {} was not in NFC form.", word);
                    }
                }
            }
        }
    }

    /// Takes two Vec<String> arguments. The first is a value returned from 
    /// Noun::decline and the second is the expected values. 
    /// 
    /// First the two arguments are checked to make sure they have the same 
    /// length. If they are, then each element in the first argument's Unicode
    /// normalized form is checked to be composed. If it is, then the function
    /// checks if the element is stored in the second argument. If it is, then
    /// the function returns true. 
    fn verify_declension(declension: &Option<Vec<String>>, correct: &Option<Vec<String>>) {
        match (declension, correct) {
            (Some(declension), Some(correct)) => {
                assert!(declension.len() == correct.len(), "The Vecs `declension` and `correct` are not the same length.");

                for (index, form) in declension.iter().enumerate() {
                    // First check to ensure the element is in normalized composed
                    // form. If it's not, the second check may return false even if the
                    // strings appears equal.
                    assert!(is_nfc(form), "{} is not in normalized composed form.", form);

                    assert!(form == &correct[index], "Element {}, {}, from `declension` is not equal to element {}, {}, from `correct`.", index, form, index, &correct[index]);
                }
            },
            (Some(_), None) => panic!("Vec `declension` contained data but `correct` was None."),
            (None, Some(_)) => panic!("Vec `declension` was None but `correct` contained data."),
            (None, None) => (),
        }
    }

    #[test]
    fn test_normalize_declensions() {
        let mut declensions = HashMap::new();

        // Obviously this isn't a valid declension.
        declensions.insert((Number::Singular, Case::Nominative), Some(vec!["cornū".nfd().collect::<String>()]));
        declensions.insert((Number::Singular, Case::Genitive), Some(vec!["cornūs".nfd().collect::<String>()]));
        declensions.insert((Number::Singular, Case::Dative), Some(vec!["cornū".nfd().collect::<String>(), "cornūs".nfd().collect::<String>()]));
        declensions.insert((Number::Singular, Case::Accusative), Some(vec!["cornū".nfd().collect::<String>()]));
        declensions.insert((Number::Singular, Case::Ablative), Some(vec!["cornū".nfd().collect::<String>()]));
        declensions.insert((Number::Singular, Case::Vocative), Some(vec!["cornū".nfd().collect::<String>()]));
        declensions.insert((Number::Plural, Case::Nominative), Some(vec!["cornū".nfd().collect::<String>()]));
        declensions.insert((Number::Plural, Case::Genitive), Some(vec!["cornūs".nfd().collect::<String>()]));
        declensions.insert((Number::Plural, Case::Dative), Some(vec!["cornū".nfd().collect::<String>(), "cornūs".nfd().collect::<String>()]));
        declensions.insert((Number::Plural, Case::Accusative), Some(vec!["cornū".nfd().collect::<String>()]));
        declensions.insert((Number::Plural, Case::Ablative), Some(vec!["cornū".nfd().collect::<String>()]));
        declensions.insert((Number::Plural, Case::Vocative), Some(vec!["cornū".nfd().collect::<String>()]));

        Irregular::normalize_declensions(&mut declensions);

        verify_normalization(&declensions);
    }

    #[test]
    fn test_irregular_vis() {
        let mut declension = HashMap::new();
        declension.insert((Number::Singular, Case::Nominative), Some(vec!["vīs".to_string()]));
        declension.insert((Number::Singular, Case::Genitive), Some(vec!["vīs".to_string()]));
        declension.insert((Number::Singular, Case::Dative), Some(vec!["vī".to_string()]));
        declension.insert((Number::Singular, Case::Accusative), Some(vec!["vim".to_string()]));
        declension.insert((Number::Singular, Case::Ablative), Some(vec!["vī".to_string()]));
        declension.insert((Number::Singular, Case::Vocative), Some(vec!["vīs".to_string()]));
        declension.insert((Number::Plural, Case::Genitive), Some(vec!["vīrium".to_string()]));
        declension.insert((Number::Plural, Case::Accusative), Some(vec!["vīrēs".to_string(), "vīrīs".to_string()]));

        match Irregular::new(declension, Gender::Feminine, "vīr".to_string(), Group::Third) {
            Ok(noun) => {
                verify_declension(&noun.decline(Number::Singular, Case::Nominative), &Some(vec!["vīs".to_string()]));
                verify_declension(&noun.decline(Number::Singular, Case::Genitive), &Some(vec!["vīs".to_string()]));
                verify_declension(&noun.decline(Number::Singular, Case::Dative), &Some(vec!["vī".to_string()]));
                verify_declension(&noun.decline(Number::Singular, Case::Accusative), &Some(vec!["vim".to_string()]));
                verify_declension(&noun.decline(Number::Singular, Case::Ablative), &Some(vec!["vī".to_string()]));
                verify_declension(&noun.decline(Number::Singular, Case::Vocative), &Some(vec!["vīs".to_string()]));

                verify_declension(&noun.decline(Number::Plural, Case::Nominative), &Some(vec!["vīrēs".to_string()]));
                verify_declension(&noun.decline(Number::Plural, Case::Genitive), &Some(vec!["vīrium".to_string()]));
                verify_declension(&noun.decline(Number::Plural, Case::Dative), &Some(vec!["vīribus".to_string()]));
                verify_declension(&noun.decline(Number::Plural, Case::Accusative), &Some(vec!["vīrēs".to_string(), "vīrīs".to_string()]));
                verify_declension(&noun.decline(Number::Plural, Case::Ablative), &Some(vec!["vīribus".to_string()]));
                verify_declension(&noun.decline(Number::Plural, Case::Vocative), &Some(vec!["vīrēs".to_string()]));
            },
            Err(_) => panic!("Failed to create irregular noun vīs, vīs"),
        }
    }

    #[test]
    fn test_irregular_i_stem_vis() {
        let mut declension = HashMap::new();
        declension.insert((Number::Singular, Case::Nominative), Some(vec!["vīs".to_string()]));
        declension.insert((Number::Singular, Case::Genitive), Some(vec!["vīs".to_string()]));
        declension.insert((Number::Singular, Case::Dative), Some(vec!["vī".to_string()]));
        declension.insert((Number::Singular, Case::Accusative), Some(vec!["vim".to_string()]));
        declension.insert((Number::Singular, Case::Ablative), Some(vec!["vī".to_string()]));
        declension.insert((Number::Singular, Case::Vocative), Some(vec!["vīs".to_string()]));
        declension.insert((Number::Plural, Case::Genitive), Some(vec!["vīrium".to_string()]));
        declension.insert((Number::Plural, Case::Accusative), Some(vec!["vīrēs".to_string(), "vīrīs".to_string()]));

        match Irregular::new_third_i_stem(declension, Gender::Feminine, "vīr".to_string()) {
            Ok(noun) => {
                verify_declension(&noun.decline(Number::Singular, Case::Nominative), &Some(vec!["vīs".to_string()]));
                verify_declension(&noun.decline(Number::Singular, Case::Genitive), &Some(vec!["vīs".to_string()]));
                verify_declension(&noun.decline(Number::Singular, Case::Dative), &Some(vec!["vī".to_string()]));
                verify_declension(&noun.decline(Number::Singular, Case::Accusative), &Some(vec!["vim".to_string()]));
                verify_declension(&noun.decline(Number::Singular, Case::Ablative), &Some(vec!["vī".to_string()]));
                verify_declension(&noun.decline(Number::Singular, Case::Vocative), &Some(vec!["vīs".to_string()]));

                verify_declension(&noun.decline(Number::Plural, Case::Nominative), &Some(vec!["vīrēs".to_string()]));
                verify_declension(&noun.decline(Number::Plural, Case::Genitive), &Some(vec!["vīrium".to_string()]));
                verify_declension(&noun.decline(Number::Plural, Case::Dative), &Some(vec!["vīribus".to_string()]));
                verify_declension(&noun.decline(Number::Plural, Case::Accusative), &Some(vec!["vīrēs".to_string(), "vīrīs".to_string()]));
                verify_declension(&noun.decline(Number::Plural, Case::Ablative), &Some(vec!["vīribus".to_string()]));
                verify_declension(&noun.decline(Number::Plural, Case::Vocative), &Some(vec!["vīrēs".to_string()]));
            },
            Err(_) => panic!("Failed to create irregular noun vīs, vīs"),
        }
    }

}