Skip to content

Commit

Permalink
refactor: update filter check methods to use references
Browse files Browse the repository at this point in the history
- Changed `check` method signatures in `Filter` trait and its implementations to use references for `Client` and `Update`.
- Updated `Context`, `Dispatcher`, `And`, `Command`, `Not`, `Or`, and `Handler` to reflect these changes.
  • Loading branch information
AndrielFR committed Jan 29, 2025
1 parent 41ee144 commit f100857
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 24 deletions.
6 changes: 1 addition & 5 deletions lib/ferogram/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,11 +849,7 @@ impl Context {
) -> Result<Update, crate::Error> {
loop {
if let Some(update) = self.wait_for_update(timeout).await {
if filter
.check(self.client.clone(), update.clone())
.await
.is_continue()
{
if filter.check(&self.client, &update).await.is_continue() {
return Ok(update);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/ferogram/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ mod tests {
Dispatcher::default()
.router(|router| router)
.router(|router| {
router.handler(handler::then(|_: Client, _: Update| async { Ok(()) }))
router.register(handler::then(|_: Client, _: Update| async { Ok(()) }))
});
}
}
12 changes: 6 additions & 6 deletions lib/ferogram/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::Flow;
#[async_trait]
pub trait Filter: CloneFilter + Send + Sync + 'static {
/// Checks if the update should be handled.
async fn check(&mut self, client: Client, update: Update) -> Flow;
async fn check(&mut self, client: &Client, update: &Update) -> Flow;

/// Wrappes `self` and `second` into [`And`] filter.
fn and<S: Filter>(self, second: S) -> And
Expand Down Expand Up @@ -67,12 +67,12 @@ pub trait Filter: CloneFilter + Send + Sync + 'static {
#[async_trait]
impl<T: Clone, F, O> Filter for T
where
T: Fn(Client, Update) -> F + Send + Sync + 'static,
T: FnMut(Client, Update) -> F + Send + Sync + 'static,
F: Future<Output = O> + Send + Sync + 'static,
O: Into<Flow>,
{
async fn check(&mut self, client: Client, update: Update) -> Flow {
self(client, update).await.into()
async fn check(&mut self, client: &Client, update: &Update) -> Flow {
self(client.clone(), update.clone()).await.into()
}
}

Expand All @@ -83,8 +83,8 @@ where
F: Future<Output = O> + Send + Sync + 'static,
O: Into<Flow>,
{
async fn check(&mut self, client: Client, update: Update) -> Flow {
self(client, update).await.into()
async fn check(&mut self, client: &Client, update: &Update) -> Flow {
self(client.clone(), update.clone()).await.into()
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/ferogram/src/filters/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub struct And {

#[async_trait]
impl Filter for And {
async fn check(&mut self, client: Client, update: Update) -> Flow {
let mut first_flow = self.first.check(client.clone(), update.clone()).await;
async fn check(&mut self, client: &Client, update: &Update) -> Flow {
let mut first_flow = self.first.check(client, update).await;

if first_flow.is_continue() {
let second_flow = self.second.check(client, update).await;
Expand Down
2 changes: 1 addition & 1 deletion lib/ferogram/src/filters/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Command {

#[async_trait]
impl Filter for Command {
async fn check(&mut self, client: Client, update: Update) -> Flow {
async fn check(&mut self, client: &Client, update: &Update) -> Flow {
let command = self.command.clone();
let splitted = command.split_whitespace().collect::<Vec<_>>();

Expand Down
8 changes: 2 additions & 6 deletions lib/ferogram/src/filters/not.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ pub struct Not {

#[async_trait]
impl Filter for Not {
async fn check(&mut self, client: Client, update: Update) -> Flow {
self.filter
.check(client.clone(), update.clone())
.await
.is_break()
.into()
async fn check(&mut self, client: &Client, update: &Update) -> Flow {
self.filter.check(client, update).await.is_break().into()
}
}
4 changes: 2 additions & 2 deletions lib/ferogram/src/filters/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub struct Or {

#[async_trait]
impl Filter for Or {
async fn check(&mut self, client: Client, update: Update) -> Flow {
let first_flow = self.first.check(client.clone(), update.clone()).await;
async fn check(&mut self, client: &Client, update: &Update) -> Flow {
let first_flow = self.first.check(client, update).await;

if first_flow.is_continue() {
first_flow
Expand Down
2 changes: 1 addition & 1 deletion lib/ferogram/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl Handler {
pub(crate) async fn check(&mut self, client: &Client, update: &Update) -> Flow {
if self.update_type == *update {
if let Some(ref mut filter) = self.filter {
filter.check(client.clone(), update.clone()).await
filter.check(client, update).await
} else {
flow::continue_now()
}
Expand Down

0 comments on commit f100857

Please sign in to comment.