rust/chat_sanitizer/src/letter_repeat.rs
changeset 14505 ba29aa03db87
parent 14503 831ecafd74c6
--- a/rust/chat_sanitizer/src/letter_repeat.rs	Fri Dec 28 03:10:05 2018 +0300
+++ b/rust/chat_sanitizer/src/letter_repeat.rs	Fri Dec 28 22:47:17 2018 +0100
@@ -1,9 +1,45 @@
 use crate::{MessageChecker, Severity};
 
-struct LetterRepeatChecker {}
+use itertools::Itertools;
+use std::marker::PhantomData;
+
+struct LetterRepeatChecker<T> {
+    threshold: usize,
+    player_id_type: PhantomData<T>,
+}
 
-impl<T> MessageChecker<T> for LetterRepeatChecker {
-    fn check(&self, player_id: T, message: &str) -> Severity {
+impl<T> LetterRepeatChecker<T> {
+    pub fn new(threshold: usize) -> Self {
+        Self {
+            threshold,
+            player_id_type: PhantomData,
+        }
+    }
+}
+
+impl<T> MessageChecker<T> for LetterRepeatChecker<T> {
+    fn check(&self, _player_id: T, message: &str) -> Severity {
+        for (_key, group) in &message.chars().into_iter().group_by(|c| *c) {
+            if group.count() >= self.threshold {
+                return Severity::Warn;
+            }
+        }
+
         Severity::Pass
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    #[test]
+    fn it_works() {
+        let checker = LetterRepeatChecker::new(3);
+        assert_eq!(checker.check(0, "Hello world!"), Severity::Pass);
+        assert_eq!(checker.check(0, "ooops"), Severity::Warn);
+        assert_eq!(
+            checker.check(0, "жираф - длинношеее животное"),
+            Severity::Warn
+        );
+    }
+}