Skip to content

Commit

Permalink
Add docs for some filters in filters module
Browse files Browse the repository at this point in the history
  • Loading branch information
Desiders committed Sep 16, 2023
1 parent 2595888 commit c545be9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
39 changes: 38 additions & 1 deletion src/filters.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
//! Filters are main part of the library used to filter incoming updates and allow call handlers by their data (text, chat, user, command, etc.),
//! [`context`] (state, db, etc.) and other conditions.
//!
//! [`Filter`] is a trait that accepts [`bot`], [`update`] and [`context`] and returns `true` if the filter passes, otherwise `false`.
//! You can use [`Filter`] trait to create your own filters or use one of the ready-made implementations.
//! Most likely you will have to write your filters, so we recommend you to check out the [`examples/uppercase_filter`] to see how to create your own filters
//! and check ready-made implementations.
//!
//! Filters can be combined with logical operators [`And`] and [`Or`] and inverted with [`Invert`].
//! Each filter has a method [`Filter::invert`], [`Filter::and`] and [`Filter::or`] to create [`Invert`], [`And`] and [`Or`] filters respectively.
//!
//! Ready-made implementations:
//! * [`ChatType`]:
//! Filter for checking the type of chat.
//! Usually used with [`ChatTypeEnum`] (or its string representation) to check the type of chat.
//! Creates with `one` or `many` methods.
//! * [`Command`]:
//! This filter checks if the message is a command.
//! Filter accepts [`PatternType`] that represents a command pattern type for verification,
//! for example, text, [`BotCommand`] (just alias to text of command) or [`Regex`].
//! You can create a filter with `new` method with transferring all necessary data at once, or use [`CommandBuilder`] to create a filter step by step.
//! Instead of [`CommandBuilder`] you can use [`Command`] `one`, `one_with_prefix`, `many`, `many_with_prefix` methods.
//! * [`ContentType`]:
//! Filter for checking the type of content.
//! Usually used with [`ContentTypeEnum`] (or its string representation) to check the type of content.
//! Creates with `one` or `many` methods.
//! * [`State`]:
//! Filter for checking the state of the user/chat/etc.
//! Filter accepts [`StateType`] that represents a state type for verification,
//! for example, equal, any or none.
//! You can create a filter with `one` or `many` if you want to check the state with the exact value
//! or use `any` or `none` if you want to check the state with any value or without state, respectively.
//!
//! [`context`]: crate::context::Context
//! [`ChatTypeEnum`]: crate::enums::ChatType
//! [`ContentTypeEnum`]: crate::enums::ContentType
pub mod base;
pub mod chat_type;
pub mod command;
Expand All @@ -9,7 +46,7 @@ pub mod user;

pub use base::Filter;
pub use chat_type::ChatType;
pub use command::{Command, CommandObject};
pub use command::{Command, CommandBuilder, CommandObject};
pub use content_type::ContentType;
pub use logical::{And, Invert, Or};
pub use state::{State, StateType};
Expand Down
7 changes: 7 additions & 0 deletions src/filters/chat_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ use crate::{client::Bot, context::Context, enums::ChatType as ChatTypeEnum, type

use async_trait::async_trait;

/// Filter for checking the type of chat
#[derive(Debug, Clone)]
pub struct ChatType {
chat_types: Vec<ChatTypeEnum>,
}

impl ChatType {
/// Creates a new [`ChatType`] filter with one allowed chat type.
/// # Notes
/// You can use [`ChatTypeEnum`] or its string representation.
pub fn one(chat_type: impl Into<ChatTypeEnum>) -> Self {
Self {
chat_types: vec![chat_type.into()],
}
}

/// Creates a new [`ChatType`] filter with many allowed chat types.
/// # Notes
/// You can use [`ChatTypeEnum`] or its string representation.
pub fn many<T, I>(chat_types: I) -> Self
where
T: Into<ChatTypeEnum>,
Expand Down
6 changes: 5 additions & 1 deletion src/filters/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ impl From<Regex> for PatternType<'_> {
}
}

/// This filter checks if the message is a command
/// This filter checks if the message is a command.
///
/// Filter accepts [`PatternType`] that represents a command pattern type for verification,
/// for example, text, [`BotCommand`] or [`Regex`].
///
/// # Notes
/// You can use parsed command using [`CommandObject`] struct in handler arguments,
/// or get it from [`Context`] by `command` key.
#[derive(Debug, Clone)]
Expand Down
7 changes: 7 additions & 0 deletions src/filters/content_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@ use crate::{client::Bot, context::Context, enums::ContentType as ContentTypeEnum

use async_trait::async_trait;

/// Filter for checking the type of content
#[derive(Debug, Clone)]
pub struct ContentType {
content_types: Vec<ContentTypeEnum>,
}

impl ContentType {
/// Creates a new [`ContentType`] filter with one allowed content type
/// # Notes
/// You can use [`ContentTypeEnum`] or its string representation
#[must_use]
pub fn one(content_type: impl Into<ContentTypeEnum>) -> Self {
Self {
content_types: vec![content_type.into()],
}
}

/// Creates a new [`ContentType`] filter with many allowed content types
/// # Notes
/// You can use [`ContentTypeEnum`] or its string representation
#[must_use]
pub fn many<T, I>(content_types: I) -> Self
where
Expand Down

0 comments on commit c545be9

Please sign in to comment.