From 4b9632de5d176e9faec2fb88500d5f8d64de0e58 Mon Sep 17 00:00:00 2001 From: P0lunin Date: Wed, 23 Oct 2019 19:28:01 +0300 Subject: [PATCH] added MessageTextCaptionFilter --- .../filter/filters/message_text_caption.rs | 173 ++++++++++++++++++ .../dispatchers/filter/filters/mod.rs | 4 +- 2 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 src/dispatching/dispatchers/filter/filters/message_text_caption.rs diff --git a/src/dispatching/dispatchers/filter/filters/message_text_caption.rs b/src/dispatching/dispatchers/filter/filters/message_text_caption.rs new file mode 100644 index 00000000..b56aeb28 --- /dev/null +++ b/src/dispatching/dispatchers/filter/filters/message_text_caption.rs @@ -0,0 +1,173 @@ +use crate::dispatching::Filter; +use crate::types::Message; + +/// Filter which compare message text or caption of media with another text. +/// Returns true if the message text or caption of media is equal to another text, otherwise false. +/// +/// NOTE: filter compares text of message or if it is not exists, compares caption of the message! +/// +/// If you want to compare only caption use +/// [MessageCaptionFilter] +/// +/// If you want to compare only text use +/// [MessageTextFilter] +/// +/// [MessageCaptionFilter]: telebofr::dispatching::dispatchers::filter::filters::MessageCaptionFilter +/// [MessageTextFilter]: telebofr::dispatching::dispatchers::filter::filters::MessageTextFilter +pub struct MessageTextCaptionFilter { + text: String, +} + +impl Filter for MessageTextCaptionFilter { + fn test(&self, value: &Message) -> bool { + match value.text() { + Some(text) => self.text == text, + None => { + match value.caption() { + Some(caption) => self.text == caption, + None => false + } + } + } + } +} + +impl MessageTextCaptionFilter { + pub fn new(text: T) -> Self + where + T: Into + { + Self { + text: text.into(), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn texts_are_equal() { + let filter = MessageTextCaptionFilter::new("text"); + 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": "text" + }"#; + let message = serde_json::from_str::(json).unwrap(); + assert!(filter.test(&message)); + } + + #[test] + fn texts_are_not_equal() { + let filter = MessageTextCaptionFilter::new("text"); + 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": "not equal text" + }"#; + let message = serde_json::from_str::(json).unwrap(); + assert_eq!(filter.test(&message), false); + } + + #[test] + fn captions_are_equal() { + let filter = MessageTextCaptionFilter::new("caption".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, + "photo": [ + { + "file_id": "AgADAgAD36sxG-PX0UvQSXIn9rccdw-ACA4ABAEAAwIAA20AAybcBAABFgQ", + "file_size": 18188, + "width": 320, + "height": 239 + } + ], + "caption": "caption" + }"#; + let message = serde_json::from_str::(json).unwrap(); + assert!(filter.test(&message)); + } + + #[test] + fn captions_are_not_equal() { + let filter = MessageTextCaptionFilter::new("text".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, + "photo": [ + { + "file_id": "AgADAgAD36sxG-PX0UvQSXIn9rccdw-ACA4ABAEAAwIAA20AAybcBAABFgQ", + "file_size": 18188, + "width": 320, + "height": 239 + } + ], + "caption": "not equal caption" + }"#; + let message = serde_json::from_str::(json).unwrap(); + assert_eq!(filter.test(&message), false); + } +} \ No newline at end of file diff --git a/src/dispatching/dispatchers/filter/filters/mod.rs b/src/dispatching/dispatchers/filter/filters/mod.rs index 4c0f0ae8..e54376eb 100644 --- a/src/dispatching/dispatchers/filter/filters/mod.rs +++ b/src/dispatching/dispatchers/filter/filters/mod.rs @@ -1,7 +1,9 @@ pub use command::*; pub use message_text::*; pub use message_caption::*; +pub use message_text_caption::*; mod command; mod message_text; -mod message_caption; \ No newline at end of file +mod message_caption; +mod message_text_caption; \ No newline at end of file