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
use crate::{
  types::{Rule, TestDefinition, TestResult},
  DisplayLevel::{self, *},
  Phonet,
};
use Reason::*;
use ValidStatus::*;

/// Results from run tests
///
/// Create with `PhonetResults::run()`
pub struct PhonetResults {
  /// List of results of each test
  pub list: Vec<TestResult>,
  /// Amount of failed tests
  pub fail_count: u32,
}

impl PhonetResults {
  /// Run tests, return results
  pub fn run(scheme: &Phonet) -> PhonetResults {
    // No tests
    if scheme.tests.is_empty() {
      return PhonetResults {
        list: Vec::new(),
        fail_count: 0,
      };
    }

    // Builders
    let mut list = vec![];
    let mut fail_count = 0;
    let mut max_word_len = 0;

    // Loop tests
    for test in &scheme.tests {
      match test {
        // Note - simply add to list
        TestDefinition::Note(note) => list.push(TestResult::Note(note.to_string())),

        // Test - Validate test, check validity with intent, create reason for failure
        TestDefinition::Test { intent, word } => {
          // Validate test
          let validity = validate_test(word, &scheme.rules);

          // Check if validity status with test intent
          let pass = !(validity.is_valid() ^ intent);

          // Create reason
          let reason = if !pass {
            // Test failed - Some reason
            Reason::from(validity, &scheme.reasons)
          } else {
            // Test passed - No reason for failure needed
            Passed
          };

          // Increase fail count if failed
          if !pass {
            fail_count += 1;
          }

          // Increase max length if word is longer than current max
          if word.len() > max_word_len {
            max_word_len = word.len();
          }

          // Add test result to list
          list.push(TestResult::Test {
            intent: *intent,
            word: word.to_string(),
            pass,
            reason,
          });
        }
      }
    }

    PhonetResults { list, fail_count }
  }

  /// Get maximum length of all test words
  fn max_word_len(&self, display_level: DisplayLevel) -> usize {
    self
      .list
      .iter()
      .map(|x| match x {
        // Test - Check display level
        TestResult::Test { word, pass, .. } => match display_level {
          // Always include
          ShowAll => word.len(),
          // Only include if failed
          NotesAndFails | JustFails if !pass => word.len(),
          // Don't include
          _ => 0,
        },
        // Note
        _ => 0,
      })
      .max()
      // Default value
      .unwrap_or(10)
  }

  /// Display results to standard output
  ///
  /// This can be implemented manually
  pub fn display(&self, display_level: DisplayLevel, no_color: bool) {
    // No tests
    if self.list.is_empty() {
      if no_color {
        println!("No tests ran.");
      } else {
        println!("\x1b[33mNo tests ran.\x1b[0m");
      }
      return;
    }

    // Get maximum length of all test words
    let max_word_len = self.max_word_len(display_level);

    // Loop result list
    for item in &self.list {
      match item {
        // Display note
        TestResult::Note(note) => match display_level {
          // Always show - Print note
          ShowAll | NotesAndFails => {
            if no_color {
              println!("{note}")
            } else {
              println!("\x1b[34m{note}\x1b[0m")
            }
          }

          // Else skip
          _ => (),
        },

        // Display test
        TestResult::Test {
          intent,
          word,
          pass,
          reason,
        } => {
          // Skip if not required by display level
          if match display_level {
            // Always show
            ShowAll => false,
            // Only show if failed
            NotesAndFails | JustFails if !pass => false,
            // Else skip
            _ => true,
          } {
            continue;
          }

          // Format reason
          let reason = match &reason {
            Passed => "",
            ShouldBeInvalid => {
              if no_color {
                "Valid, but should be invalid"
              } else {
                "\x1b[33mValid, but should be invalid\x1b[0m"
              }
            }
            NoReasonGiven => "No reason given",
            Custom(reason) => reason,
          };

          // Display test status
          if no_color {
            println!(
              " {intent} {word}{space}  {result} {reason}",
              intent = if *intent { "✔" } else { "✗" },
              space = " ".repeat(max_word_len - word.chars().count()),
              result = if *pass { "pass" } else { "FAIL" },
            );
          } else {
            println!(
              "  \x1b[{intent}\x1b[0m {word}{space}  \x1b[1;{result} \x1b[0;3;1m{reason}\x1b[0m",
              intent = if *intent { "36m✔" } else { "35m✗" },
              space = " ".repeat(max_word_len - word.chars().count()),
              result = if *pass { "32mpass" } else { "31mFAIL" },
            );
          }
        }
      }
    }

    // Final print
    if self.fail_count == 0 {
      // All passed
      if no_color {
        println!("All tests pass!");
      } else {
        println!("\x1b[32;1;3mAll tests pass!\x1b[0m");
      }
    } else {
      // Some failed
      if no_color {
        println!(
          "{fails} test{s} failed!",
          fails = self.fail_count,
          s = if self.fail_count == 1 { "" } else { "s" },
        );
      } else {
        println!(
          "\x1b[31;1;3m{fails} test{s} failed!\x1b[0m",
          fails = self.fail_count,
          s = if self.fail_count == 1 { "" } else { "s" },
        );
      }
    }
  }
}

/// Reason for failure variants
pub enum Reason {
  /// Test passed, do not display reason
  Passed,
  /// No reason was given for rule for test failing
  NoReasonGiven,
  /// Test was valid, but should have been invalid
  ShouldBeInvalid,
  /// Custom reason for rule
  Custom(String),
}

impl Reason {
  fn from(validity: ValidStatus, reasons: &[String]) -> Self {
    match validity {
      // Test was valid, but it should have been invalid 
      Valid => ShouldBeInvalid,

      // Test was invalid, but it should have been valid
      Invalid(reason) => match reason {
        // No reason was given for rule
        None => NoReasonGiven,

        // Find rule reason in scheme
        Some(reason) => match reasons.get(reason) {
          // Rule found - Custom reason
          Some(x) => Reason::Custom(x.to_string()),
          // No rule found
          // ? this should not happen ever ?
          None => NoReasonGiven,
        },
      },
    }
  }
}

/// State of rules match of word
///
/// If invalid, reason reference can be provided
pub enum ValidStatus {
  /// String matches
  Valid,
  /// String does not match
  Invalid(Option<usize>),
}

impl ValidStatus {
  /// Returns `true` if state is `Valid`
  pub fn is_valid(&self) -> bool {
    if let Valid = self {
      return true;
    }
    false
  }
}

/// Check if string is valid with rules
pub fn validate_test(word: &str, rules: &Vec<Rule>) -> ValidStatus {
  // Check for match with every rule, if not, return reason
  for Rule {
    intent,
    pattern,
    reason_ref,
  } in rules
  {
    // Check if rule matches, and whether match signifies returning invalid or continuing
    if intent
      ^ pattern
        .is_match(word)
        .expect("Failed checking regex match. This error should NEVER APPEAR!")
    {
      return Invalid(*reason_ref);
    }
  }

  Valid
}