This commit is contained in:
Temirkhan Myrzamadi 2019-10-29 01:28:38 +06:00
parent f0526d06f1
commit 9d2b188462
7 changed files with 115 additions and 111 deletions

View file

@ -8,7 +8,7 @@ use crate::network::download_file_stream;
use crate::{bot::Bot, network::download_file, DownloadError};
impl Bot {
/// Download file from telegram into `destination`.
/// Download a file from Telegram into `destination`.
/// `path` can be obtained from [`get_file`] method.
///
/// For downloading as Stream of Chunks see [`download_file_stream`].
@ -43,9 +43,9 @@ impl Bot {
download_file(&self.client, &self.token, path, destination).await
}
/// Download file from telegram.
/// Download a file from Telegram.
///
/// `path` can be obtained from [`get_file`] method.
/// `path` can be obtained from the [`get_file`] method.
///
/// For downloading into [`AsyncWrite`] (e.g. [`tokio::fs::File`])
/// see [`download_file`].

View file

@ -1,5 +1,4 @@
use crate::dispatching::Filter;
use crate::types::Message;
use crate::{dispatching::Filter, types::Message};
pub struct CommandFilter {
command: String,
@ -8,13 +7,11 @@ pub struct CommandFilter {
impl Filter<Message> 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
Some(text) => match text.split_whitespace().next() {
Some(command) => self.command == command,
None => false,
},
None => false,
}
}
}
@ -25,7 +22,7 @@ impl CommandFilter {
T: Into<String>,
{
Self {
command: '/'.to_string() + &command.into()
command: '/'.to_string() + &command.into(),
}
}
pub fn with_prefix<T>(command: T, prefix: T) -> Self
@ -33,7 +30,7 @@ impl CommandFilter {
T: Into<String>,
{
Self {
command: prefix.into() + &command.into()
command: prefix.into() + &command.into(),
}
}
}
@ -41,7 +38,9 @@ impl CommandFilter {
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{Chat, ChatKind, MessageKind, Sender, User, ForwardKind, MediaKind};
use crate::types::{
Chat, ChatKind, ForwardKind, MediaKind, MessageKind, Sender, User,
};
#[test]
fn commands_are_equal() {
@ -53,14 +52,16 @@ mod tests {
#[test]
fn commands_are_not_equal() {
let filter = CommandFilter::new("command".to_string());
let message = create_message_with_text("/not_equal_command".to_string());
let message =
create_message_with_text("/not_equal_command".to_string());
assert_eq!(filter.test(&message), false);
}
#[test]
fn command_have_args() {
let filter = CommandFilter::new("command".to_string());
let message = create_message_with_text("/command arg1 arg2".to_string());
let message =
create_message_with_text("/command arg1 arg2".to_string());
assert!(filter.test(&message));
}
@ -81,9 +82,9 @@ mod tests {
type_: (),
username: None,
first_name: None,
last_name: None
last_name: None,
},
photo: None
photo: None,
},
kind: MessageKind::Common {
from: Sender::User(User {
@ -92,18 +93,18 @@ mod tests {
first_name: "".to_string(),
last_name: None,
username: None,
language_code: None
language_code: None,
}),
forward_kind: ForwardKind::Origin {
reply_to_message: None
reply_to_message: None,
},
edit_date: None,
media_kind: MediaKind::Text {
text,
entities: vec![]
entities: vec![],
},
reply_markup: None
}
reply_markup: None,
},
}
}
}

View file

@ -60,9 +60,9 @@ impl<A, B> And<A, B> {
}
impl<T, A, B> Filter<T> for And<A, B>
where
A: Filter<T>,
B: Filter<T>,
where
A: Filter<T>,
B: Filter<T>,
{
fn test(&self, value: &T) -> bool {
self.0.test(value) && self.1.test(value)
@ -111,9 +111,9 @@ impl<A, B> Or<A, B> {
}
impl<T, A, B> Filter<T> for Or<A, B>
where
A: Filter<T>,
B: Filter<T>,
where
A: Filter<T>,
B: Filter<T>,
{
fn test(&self, value: &T) -> bool {
self.0.test(value) || self.1.test(value)
@ -157,8 +157,8 @@ impl<A> Not<A> {
}
impl<T, A> Filter<T> for Not<A>
where
A: Filter<T>,
where
A: Filter<T>,
{
fn test(&self, value: &T) -> bool {
!self.0.test(value)
@ -283,8 +283,8 @@ pub fn f<A>(a: A) -> F<A> {
}
impl<T, A> Filter<T> for F<A>
where
A: Filter<T>,
where
A: Filter<T>,
{
fn test(&self, value: &T) -> bool {
self.0.test(value)
@ -324,8 +324,8 @@ pub trait FilterExt<T> {
///
/// [`Not::new`]: crate::dispatching::filter::Not::new
fn not(self) -> Not<Self>
where
Self: Sized,
where
Self: Sized,
{
Not::new(self)
}
@ -346,8 +346,8 @@ pub trait FilterExt<T> {
///
/// [`Not::new`]: crate::dispatching::filter::And::new
fn and<B>(self, other: B) -> And<Self, B>
where
Self: Sized,
where
Self: Sized,
{
And::new(self, other)
}
@ -368,8 +368,8 @@ pub trait FilterExt<T> {
///
/// [`Not::new`]: crate::dispatching::filter::Or::new
fn or<B>(self, other: B) -> Or<Self, B>
where
Self: Sized,
where
Self: Sized,
{
Or::new(self, other)
}

View file

@ -1,10 +1,11 @@
use crate::dispatching::Filter;
use crate::types::Message;
use crate::{dispatching::Filter, types::Message};
/// Filter which compare caption of media with another text.
/// Returns true if the caption of media is equal to another text, otherwise false.
/// Returns true if the caption of media is equal to another text, otherwise
/// false.
///
/// NOTE: filter compares only caption of media, does not compare text of message!
/// NOTE: filter compares only caption of media, does not compare text of
/// message!
///
/// If you want to compare text of message use
/// [MessageTextFilter]
@ -13,7 +14,8 @@ use crate::types::Message;
/// [MessageTextCaptionFilter]
///
/// [MessageTextFilter]: telebofr::dispatching::filters::MessageTextFilter
/// [MessageTextCaptionFilter]: telebofr::dispatching::filters::MessageTextCaptionFilter
/// [MessageTextCaptionFilter]:
/// telebofr::dispatching::filters::MessageTextCaptionFilter
pub struct MessageCaptionFilter {
text: String,
}
@ -22,7 +24,7 @@ impl Filter<Message> for MessageCaptionFilter {
fn test(&self, value: &Message) -> bool {
match value.caption() {
Some(caption) => self.text == caption,
None => false
None => false,
}
}
}
@ -32,16 +34,16 @@ impl MessageCaptionFilter {
where
T: Into<String>,
{
Self {
text: text.into(),
}
Self { text: text.into() }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{Chat, Sender, ChatKind, MessageKind, ForwardKind, User, MediaKind};
use crate::types::{
Chat, ChatKind, ForwardKind, MediaKind, MessageKind, Sender, User,
};
#[test]
fn captions_are_equal() {
@ -53,7 +55,8 @@ mod tests {
#[test]
fn captions_are_not_equal() {
let filter = MessageCaptionFilter::new("caption".to_string());
let message = create_message_with_caption("not equal caption".to_string());
let message =
create_message_with_caption("not equal caption".to_string());
assert_eq!(filter.test(&message), false);
}
@ -67,9 +70,9 @@ mod tests {
type_: (),
username: None,
first_name: None,
last_name: None
last_name: None,
},
photo: None
photo: None,
},
kind: MessageKind::Common {
from: Sender::User(User {
@ -78,20 +81,20 @@ mod tests {
first_name: "".to_string(),
last_name: None,
username: None,
language_code: None
language_code: None,
}),
forward_kind: ForwardKind::Origin {
reply_to_message: None
reply_to_message: None,
},
edit_date: None,
media_kind: MediaKind::Photo {
photo: vec![],
caption: Some(caption),
caption_entities: vec![],
media_group_id: None
media_group_id: None,
},
reply_markup: None
}
reply_markup: None,
},
}
}
}
}

View file

@ -1,5 +1,4 @@
use crate::dispatching::Filter;
use crate::types::Message;
use crate::{dispatching::Filter, types::Message};
/// Filter which compare message text with another text.
/// Returns true if the message text is equal to another text, otherwise false.
@ -13,7 +12,8 @@ use crate::types::Message;
/// [MessageTextCaptionFilter]
///
/// [MessageCaptionFilter]: telebofr::dispatching::filters::MessageCaptionFilter
/// [MessageTextCaptionFilter]: telebofr::dispatching::filters::MessageTextCaptionFilter
/// [MessageTextCaptionFilter]:
/// telebofr::dispatching::filters::MessageTextCaptionFilter
pub struct MessageTextFilter {
text: String,
}
@ -22,7 +22,7 @@ impl Filter<Message> for MessageTextFilter {
fn test(&self, value: &Message) -> bool {
match value.text() {
Some(text) => self.text == text,
None => false
None => false,
}
}
}
@ -30,18 +30,18 @@ impl Filter<Message> for MessageTextFilter {
impl MessageTextFilter {
pub fn new<T>(text: T) -> Self
where
T: Into<String>
T: Into<String>,
{
Self {
text: text.into(),
}
Self { text: text.into() }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{Chat, Sender, ChatKind, MessageKind, ForwardKind, User, MediaKind};
use crate::types::{
Chat, ChatKind, ForwardKind, MediaKind, MessageKind, Sender, User,
};
#[test]
fn texts_are_equal() {
@ -67,9 +67,9 @@ mod tests {
type_: (),
username: None,
first_name: None,
last_name: None
last_name: None,
},
photo: None
photo: None,
},
kind: MessageKind::Common {
from: Sender::User(User {
@ -78,18 +78,18 @@ mod tests {
first_name: "".to_string(),
last_name: None,
username: None,
language_code: None
language_code: None,
}),
forward_kind: ForwardKind::Origin {
reply_to_message: None
reply_to_message: None,
},
edit_date: None,
media_kind: MediaKind::Text {
text,
entities: vec![]
entities: vec![],
},
reply_markup: None
}
reply_markup: None,
},
}
}
}
}

View file

@ -1,10 +1,11 @@
use crate::dispatching::Filter;
use crate::types::Message;
use crate::{dispatching::Filter, 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.
/// 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!
/// 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]
@ -22,31 +23,29 @@ impl Filter<Message> 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
}
}
None => match value.caption() {
Some(caption) => self.text == caption,
None => false,
},
}
}
}
impl MessageTextCaptionFilter {
pub fn new<T>(text: T) -> Self
where
T: Into<String>
where
T: Into<String>,
{
Self {
text: text.into(),
}
Self { text: text.into() }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{Chat, Sender, ChatKind, MessageKind, ForwardKind, User, MediaKind};
use crate::types::{
Chat, ChatKind, ForwardKind, MediaKind, MessageKind, Sender, User,
};
#[test]
fn texts_are_equal() {
@ -72,9 +71,9 @@ mod tests {
type_: (),
username: None,
first_name: None,
last_name: None
last_name: None,
},
photo: None
photo: None,
},
kind: MessageKind::Common {
from: Sender::User(User {
@ -83,18 +82,18 @@ mod tests {
first_name: "".to_string(),
last_name: None,
username: None,
language_code: None
language_code: None,
}),
forward_kind: ForwardKind::Origin {
reply_to_message: None
reply_to_message: None,
},
edit_date: None,
media_kind: MediaKind::Text {
text,
entities: vec![]
entities: vec![],
},
reply_markup: None
}
reply_markup: None,
},
}
}
@ -108,7 +107,8 @@ mod tests {
#[test]
fn captions_are_not_equal() {
let filter = MessageTextCaptionFilter::new("caption".to_string());
let message = create_message_with_caption("not equal caption".to_string());
let message =
create_message_with_caption("not equal caption".to_string());
assert_eq!(filter.test(&message), false);
}
@ -122,9 +122,9 @@ mod tests {
type_: (),
username: None,
first_name: None,
last_name: None
last_name: None,
},
photo: None
photo: None,
},
kind: MessageKind::Common {
from: Sender::User(User {
@ -133,20 +133,20 @@ mod tests {
first_name: "".to_string(),
last_name: None,
username: None,
language_code: None
language_code: None,
}),
forward_kind: ForwardKind::Origin {
reply_to_message: None
reply_to_message: None,
},
edit_date: None,
media_kind: MediaKind::Photo {
photo: vec![],
caption: Some(caption),
caption_entities: vec![],
media_group_id: None
media_group_id: None,
},
reply_markup: None
}
reply_markup: None,
},
}
}
}
}

View file

@ -1,13 +1,13 @@
pub use main::*;
pub use command::*;
pub use message_text::*;
pub use message_caption::*;
pub use message_text::*;
pub use message_text_caption::*;
mod main;
mod command;
mod message_text;
mod message_caption;
mod message_text_caption;
mod message_text;
mod message_text_caption;