From f67011eb4299b3aefa3b597294b630efe972bed0 Mon Sep 17 00:00:00 2001 From: P0lunin Date: Wed, 23 Oct 2019 18:36:53 +0300 Subject: [PATCH] added CommandFilter --- .../dispatchers/filter/filters/command.rs | 147 ++++++++++++++++++ .../dispatchers/filter/filters/mod.rs | 8 +- 2 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/dispatching/dispatchers/filter/filters/command.rs diff --git a/src/dispatching/dispatchers/filter/filters/command.rs b/src/dispatching/dispatchers/filter/filters/command.rs new file mode 100644 index 00000000..07a81384 --- /dev/null +++ b/src/dispatching/dispatchers/filter/filters/command.rs @@ -0,0 +1,147 @@ +use crate::dispatching::Filter; +use crate::types::Message; +use std::ops::Add; + +struct CommandFilter { + command: String, +} + +impl Filter for CommandFilter { + fn test(&self, value: &Message) -> bool { + match value.text() { + Some(text) => { + match text.split_whitespace().next() { + Some(command) => self.command == command, + None => false + } + } + None => false + } + } +} + +impl CommandFilter { + pub fn new(command: String) -> Self { + Self { + command: '/'.to_string() + command.as_str() + } + } + pub fn with_start_string(command: String, start_string: String) -> Self { + Self { + command: start_string.add(command.as_str()) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn commands_are_equal() { + let filter = CommandFilter::new("command".to_string()); + let json = r#"{ + "message_id": 199785, + "from": { + "id": 250918540, + "is_bot": false, + "first_name": "Андрей", + "last_name": "Власов", + "username": "aka_dude", + "language_code": "en" + }, + "chat": { + "id": 250918540, + "first_name": "Андрей", + "last_name": "Власов", + "username": "aka_dude", + "type": "private" + }, + "date": 1568289890, + "text": "/command" + }"#; + let message = serde_json::from_str::(json).unwrap(); + assert!(filter.test(&message)); + } + + #[test] + fn commands_are_not_equal() { + let filter = CommandFilter::new("command".to_string()); + let json = r#"{ + "message_id": 199785, + "from": { + "id": 250918540, + "is_bot": false, + "first_name": "Андрей", + "last_name": "Власов", + "username": "aka_dude", + "language_code": "en" + }, + "chat": { + "id": 250918540, + "first_name": "Андрей", + "last_name": "Власов", + "username": "aka_dude", + "type": "private" + }, + "date": 1568289890, + "text": "/command_not_equal" + }"#; + let message = serde_json::from_str::(json).unwrap(); + assert_eq!(filter.test(&message), false); + } + + #[test] + fn command_have_args() { + let filter = CommandFilter::new("command".to_string()); + let json = r#"{ + "message_id": 199785, + "from": { + "id": 250918540, + "is_bot": false, + "first_name": "Андрей", + "last_name": "Власов", + "username": "aka_dude", + "language_code": "en" + }, + "chat": { + "id": 250918540, + "first_name": "Андрей", + "last_name": "Власов", + "username": "aka_dude", + "type": "private" + }, + "date": 1568289890, + "text": "/command arg1 arg2" + }"#; + let message = serde_json::from_str::(json).unwrap(); + assert!(filter.test(&message)); + } + + #[test] + fn message_have_only_whitespace() { + let filter = CommandFilter::new("command".to_string()); + let json = r#"{ + "message_id": 199785, + "from": { + "id": 250918540, + "is_bot": false, + "first_name": "Андрей", + "last_name": "Власов", + "username": "aka_dude", + "language_code": "en" + }, + "chat": { + "id": 250918540, + "first_name": "Андрей", + "last_name": "Власов", + "username": "aka_dude", + "type": "private" + }, + "date": 1568289890, + "text": " " + }"#; + let message = serde_json::from_str::(json).unwrap(); + assert_eq!(filter.test(&message), false); + } +} diff --git a/src/dispatching/dispatchers/filter/filters/mod.rs b/src/dispatching/dispatchers/filter/filters/mod.rs index 45eaf476..4c0f0ae8 100644 --- a/src/dispatching/dispatchers/filter/filters/mod.rs +++ b/src/dispatching/dispatchers/filter/filters/mod.rs @@ -1,3 +1,7 @@ -pub use text::*; +pub use command::*; +pub use message_text::*; +pub use message_caption::*; -mod text; \ No newline at end of file +mod command; +mod message_text; +mod message_caption; \ No newline at end of file