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
#![stable]
//! This module is used to index words and does the heavy lifting of our program.

/// A counted word.
///
/// This struct contains members for storing a word and the number of times it appeared. This
/// struct is intended for use with [`add_word`](fn.add_word.html).
///
/// # Examples
///
/// ```
/// use lib_word_count::word_index;
///
/// let indexed_word = word_index::IndexedWord{
///     word: "Text".to_string(),
///     appeared: 12
/// };
///
/// assert_eq!(indexed_word.word, "Text".to_string());
/// assert_eq!(indexed_word.appeared, 12i64);
/// ```
#[derive(Debug, PartialEq)]
#[stable]
pub struct IndexedWord {
    /// The word that's indexed.
    pub word: String,
    /// The amount of times this word appeared.
    pub appeared: i64
}

/// Add a word to a given index.
///
/// This function prevents duplicates and increments the count of the word appearances
/// automatically. The vector will be modified accordingly.
///
/// # Arguments
///
/// * `word`    A string containing the word to add.
///
/// * `index`   A reference to a vector containing all the indexed words.
///
/// # Examples
///
/// ```
/// use lib_word_count::word_index;
///
/// let mut index = Vec::new();
///
/// word_index::add_word("Hello".to_string(), &mut index);
/// word_index::add_word("hELLO".to_string(), &mut index);
/// word_index::add_word("World".to_string(), &mut index);
/// word_index::add_word("HELLO".to_string(), &mut index);
/// word_index::add_word("PFUDOR".to_string(), &mut index);
///
/// assert_eq!(index[0], word_index::IndexedWord{
///     word: "hello".to_string(),
///     appeared: 3
/// });
/// assert_eq!(index[1], word_index::IndexedWord{
///     word: "world".to_string(),
///     appeared: 1
/// });
/// assert_eq!(index[2], word_index::IndexedWord{
///     word: "pfudor".to_string(),
///     appeared: 1
/// });
/// ```
#[stable]
pub fn add_word(word: String, index: &mut Vec<IndexedWord>) {

    for indexed_word in index.iter_mut() {
        if word.to_lowercase() == indexed_word.word {
            indexed_word.appeared += 1;
            return;
        }
    }

    let new_word = IndexedWord{
        word: word.to_lowercase(),
        appeared: 1
    };

    index.push(new_word);
}